I'm looking at asp.net vnext engineering guideline and have noticed that they recommend to use NotNull
attribute instead of explicit checking of input argument for null and throwing ArgumentNullException
. The thing that confused me is that base on this guideline it is just enough to declare this attribute and the checking code will be generated at compile time into the method body. I've tried to do this in my test project however the magic didn't happen i.e. it threw the exception System.NullReferenceException
instead of System.ArgumentNullException
. How this is supposed to work? Are they going to use some AOP library to inject the checking code at compile time?
NotNull: A nullable field, parameter, property, or return value will never be null. MaybeNullWhen: A non-nullable argument may be null when the method returns the specified bool value. NotNullWhen: A nullable argument won't be null when the method returns the specified bool value.
In general, a null attribute value is the equivalent of nothing. However, it's important to be precise in our terminology because there are many ways to represent nothing: An attribute has a value that indicates nothingness (null) An attribute exists but has no value (empty) An attribute doesn't exist (missing)
Advertisements. An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. You can add declarative information to a program by using an attribute.
The nullable annotation context determines the compiler's behavior. There are four values for the nullable annotation context: disable: The compiler behaves similarly to C# 7.3 and earlier: Nullable warnings are disabled. All reference type variables are nullable reference types.
The NotNullAttribute
is gone. It was replaced with conditionally throwing ArgumentNullException
and subsequently removed by the ASP.NET team. As of Jan 12, 2016 there's no plan to bring it back. (At that time, I was working on the ASP.NET team.)
The attribute will be replaced through a pre-compilation step, using Roslyn, by code that does the actual check.
However, the feature is not yet ready as of Jun 17, 2015. It will come in a later version. So far, it is just an empty internal attribute that should be implemented in each project again:
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)] internal sealed class NotNullAttribute : Attribute { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With