C#到VB .NET产生返回转换
发布时间:2020-12-15 19:30:58 所属栏目:百科 来源:网络整理
导读:这个片段在VB .NET中的翻译是什么? public static IEnumerableTSource WhereTSource(this IEnumerableTSource source,FuncTSource,Boolean predicate){ foreach (TSource element in source) { if (predicate(element)) yield return element; }} 解决方法
这个片段在VB .NET中的翻译是什么?
public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source,Func<TSource,Boolean> predicate) { foreach (TSource element in source) { if (predicate(element)) yield return element; } } 解决方法
这里的问题不是转换扩展方法 – 它正在转换迭代器块(该方法使用yield return.VB没有任何等效的语言构造 – 你必须创建自己的IEnumerable< T>实现了过滤,然后从扩展方法返回类的实例.
这正是C#编译器所做的,但它隐藏在幕后. 需要注意的一点是,否则可能并不明显:IEnumerator< T>实现IDisposable,foreach循环在最后处理迭代器.这可能非常重要 – 所以如果你确实创建了自己的实现(我建议你不要这样做,坦率地说)你需要在你自己的source.GetEnumerator()返回的迭代器上调用Dispose.处理方法. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |