Using [NotNull] for a method's parameters
The only attribute that can cause the compiler to generate an error is the ObsoleteAttribute
. It is because this attribute's behavior is hard-coded into the compiler.
Attributes like the NotNull
attribute are generally meant for tools (like ReSharper) to generate warnings or errors while writing code. Please read about this particular attribute here.
You can also use tools like PostSharp to issue additional build-time errors.
If you wish to move Null checks to be implemented by aspects and not need to be done by hand. The clear solution is to use Fody the open source build weaver. Specifically you want to leverage the NullGuard Fody
PM> Install-Package NullGuard.Fody
Should be everything required to get set up to use Fody with null guards. The documentation shows how you can have fine grain control if you desire.
2019 Update
C# 8 and .NET Core 3.0 permanently eliminate null reference exceptions.
Tutorial: Migrate existing code with nullable reference types
Tutorial: Express your design intent more clearly with nullable and non-nullable reference types