C# – Understanding Not Nullable Types

c++

Is there a way to create a non nullable type in C# (like DateTime or TimeSpan).?

Also is there a way (an attribute maybe) to enforce that not null arguments wouldn't be passed to methods and properties without adding

if(arg1 == null)
{
   throw new ArgumentNullException("this attribute is null")
}

Best Answer

DateTime and TimeSpan are not-nullable since they are structs rather than classes.

As for your second question, there is no standard way you can do this in C#. You can do this using PostSharp, which is an AOP framework, or with Spec#, which is a whole new language (an extension of C#) which allows for some of desired behavior.

Related Question