unity yield return 非 null
yield return 非 null 是指在使用 yield return 语句时,返回的对象不为 null。yield return 语句用于在迭代器中返回序列中的下一个元素,并暂停迭代器的执行,直到下一个元素被请求。
在 C# 中,yield return 语句可以返回任何对象,包括 null。如果在使用 yield return 语句时返回 null,则会抛出 NullReferenceException 异常。因此,为了避免这种异常,应确保在使用 yield return 语句时返回非 null 的对象。
例如,以下示例代码演示了如何在迭代器中使用 yield return 非 null:
public IEnumerable<int> GetNumbers()
{
yield return 1;
yield return 2;
yield return 3;
}
foreach (int number in GetNumbers())
{
Console.WriteLine(number);
}
在上面的代码中,GetNumbers 方法返回一个实现 IEnumerable
原文地址: https://www.cveoy.top/t/topic/ATK 著作权归作者所有。请勿转载和采集!