First: do not do too much in one statement. If you have a huge number of dereferencing operations on one line, it will be much more difficult for you to find the culprit. Demeter’s law also helps with this - if you have something like order.SalesClerk.Manager.Address.Street.Lengththat, then you have many opportunities to get through when you get an exception. (I am not dogmatic about the Law of Demeter, but all in moderation ...)
Secondly: prefer casting using asunless it is not valid so that the object is of a different type, which usually includes a zero check right after that. So here:
string text = foo as string;
int length = text.Length;
NullReferenceException , , text null, , , foo null . , string, :
string text = (string) foo;
.
-: , - , API. Noda Time, , . , ( Period):
internal LocalInstant AddTo(LocalInstant localInstant,
CalendarSystem calendar, int scalar)
{
Preconditions.CheckNotNull(calendar, "calendar");
...
}
, null.