Does C# has some rule of thumb or a coding convention contract, which handles the possible null argument?
As an example, I’m writing a custom method, which retrieves a byte[] data parameter.
public static string ConvertDataToMyOwnAndCustomString(byte[] data) { ... }
Now – what should I do if the passed data is null?
Should I leave it as it is so that a possible NullReferenceException occurs? Or should I write a check and do something like this:
if (data == null) return null;
The generally accepted pattern is to throw an
ArgumentNullExceptionif it is indeed an error for clients to be passing
nullto your method. These days, you can even enforce the requirement through a Code Contract:Of course, it might very well be the case that
nullis not an error for your method. That’s for you to decide though, not us. But don’t returnnullto indicate to your client that an error occurred.