TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
C# Coding Standard

Rule:  5@117

Synopsis:Properties, methods and arguments representing strings or collections should never be null
Severity Level:4
Category:Object lifecycle


With the introduction of LINQ in C# 3.0, it has become much simpler to write code that manipulates collections and strings (lists, dictionaries, etc.). For this code to work well, and stay simple, it is important that collections and strings are always defined, and not equal to null.

So, as a general rule for C#, we propose the following:

C# members (methods, properties) and arguments that return a collection (such as a list or a dictionary) or string should never return null. Instead of returning null when there is nothing more useful to return, they should always return the empty list, the empty dictionary, the empty string, etc. That way, the caller of such members never has to check for null, and can simply work on the returned collections or strings - whether they are empty or not.

Also, members should document this behavior, i.e., they should document that, indeed, they return an empty collection or string instead of null if nothing more useful can be returned.

Literature References:
Aviva AV1135