Why can't iterator methods take either 'ref' or 'out' parameters?

前端 未结 5 1366
鱼传尺愫
鱼传尺愫 2021-01-31 08:36

I tried this earlier today:

public interface IFoo
{
    IEnumerable GetItems_A( ref int somethingElse );
    IEnumerable GetItems_B( ref in         


        
5条回答
  •  自闭症患者
    2021-01-31 08:40

    Others have explained why your iterator can't have a ref parameter. Here's a simple alternative:

    public interface IFoo
    {
        IEnumerable GetItems( int[] box );
        ...
    }
    
    public class Bar : IFoo
    {
        public IEnumerable GetItems( int[] box )
        {
            int value = box[0];
            // use and change value and yield to your heart's content
            box[0] = value;
        }
    }
    

    If you have several items to pass in and out, define a class to hold them.

提交回复
热议问题