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

前端 未结 5 1414
鱼传尺愫
鱼传尺愫 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:48

    At a highish level, A ref variable can point to many locations including to value types that are on the stack. The time at which the iterator is initially created by calling the iterator method and when the ref variable would be assigned are two very different times. It is not possible to guarantee that the variable which originally was passed by reference is still around when the iterator actually executes. Hence it is not allowed (or verifiable)

提交回复
热议问题