c#不能作为ref传递

本文关键字:ref 传递 不能 | 更新日期: 2023-09-27 18:05:57

当我遇到这个错误时,我正在尝试在c#中使用EMGUCV编程:

错误1不能传递'currentFrameBlob'作为ref/out参数,因为它是一个'foreach迭代变量'

错误2不能传递'currentFrameBlob'作为ref/out参数,因为它是一个'foreach迭代变量'

它位于这个代码块中。

public void matchCurrentFrameBlobsToExistingBlobs(ref List<Blob> existingBlobs, ref List<Blob> currentFrameBlobs)
{
    foreach (Blob existingBlob in existingBlobs)
    {
        existingBlob.blnCurrentMatchFoundOrNewBlob = false;
        existingBlob.predictNextPosition();
    }
    foreach (Blob currentFrameBlob in currentFrameBlobs)
    {
        int intIndexOfLeastDistance = 0;
        double dblLeastDistance = 1000000.0;
        for (int i = 0; i <= existingBlobs.Count() - 1; i++)
        {
            if ((existingBlobs[i].blnStillBeingTracked == true))
            {
                double dblDistance = distanceBetweenPoints(currentFrameBlob.centerPositions.Last(), existingBlobs[i].predictedNextPosition);
                if ((dblDistance < dblLeastDistance))
                {
                    dblLeastDistance = dblDistance;
                    intIndexOfLeastDistance = i;
                }
            }
        }
        if ((dblLeastDistance < currentFrameBlob.dblCurrentDiagonalSize * 0.5))
        {
            addBlobToExistingBlobs(ref currentFrameBlob, ref existingBlobs, ref intIndexOfLeastDistance);
        }
        else
        {
            addNewBlob(ref currentFrameBlob, ref existingBlobs);
        }
    }

我读到这个错误,在我看来,这是c#的一个怪癖。处理这类错误最有效的方法是什么?现在我很害怕做出改变,因为我不知道如何在不让事情变得更糟的情况下解决它。

c#不能作为ref传递

最简单的修改:

 foreach (Blob currentFrameBlob2 in currentFrameBlobs)
 {
   Blob currentFrameBlob = currentFrameBlob2;
   //....