开发者

What to do if null exception occur in C#?

See ,I have drop down control and i have keep the LIST as datasource. But if list is null Then it throw nul开发者_开发知识库l exception. So what is the standard way to handle this situation?


If you can, keep the list as an empty list rather than a null list. It's easy to confuse the usage of an empty list with a null list (what do each of these mean), and if you use empty lists consistently then you'll reduce the opportunity for null pointer exceptions.


Bind the list to the dropdown after checking whether the list is null or not.

if (list != null) {
   dd.DataSource = list;
}
else {
   dd.DataSource = new List<ObjType>();
}

Note: ObjType is the type of list items that you are using in the dropdown, for example string, if the list that you are using is a list of strings.

Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜