vb.net – 每个循环都没有集合 – 如何处理它?

vb.net – 每个循环都没有集合 – 如何处理它?,第1张

概述当集合什么都没有时我如何处理每个循环,我认为它会跳过但我得到一个例外? 我是否需要将foreach循环包装在if中以检查什么,只有当它不是什么都没有时,然后输入for each循环? For Each item As String In MyStringList 'do something with each item but something myStringList will b 当集合什么都没有时我如何处理每个循环,我认为它会跳过但我得到一个例外?

我是否需要将foreach循环包装在if中以检查什么,只有当它不是什么都没有时,然后输入for each循环?

For Each item As String In MyStringList     'do something with each item but something myStringList will be nothing?Next
解决方法

Do I need to wrap the foreach loop in a if to check for nothing and only if it is not nothing then enter in the for each loop?

是.

If MyStringList IsNot nothing Then    For Each item As String In MyStringList        'do something ...    NextEnd If

Microsoft说这是设计:

I think that most foreach loops are written with the intent of iterating a non-null collection. If you try iterating through null you should get your exception,so that you can fix your code. Foreach is basically a syntactic convenIEnce. As such,it should not be “magical” and do unexpected things under the hood. I agree with the post that proposed the use of empty collections rather than null. (They can typically be reused quite a bit using singleton techniques).

总结

以上是内存溢出为你收集整理的vb.net – 每个循环都没有集合 – 如何处理它?全部内容,希望文章能够帮你解决vb.net – 每个循环都没有集合 – 如何处理它?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1212913.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存