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
andTimeSpan
are not-nullable since they arestruct
s rather thanclass
es.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.