Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[NotNull] attribute for parameters.

I'm writing couple methods that take objects as parameters, currently I'm doing a manual if(obj==null) throw exception/return error. but I've noticed in some built in methods (Encoding.ASCII.GetBytes for example), the parameter have a [NotNull] attribute, any idea how I can get that into my code? tried applying the attribute but can't find it.
Have a cool signature? hit me up and I might use it.
Not sure about that attribute, but you can do something similar with code contracts: [Image: f58329c9b4eebc2f03f1c2fad4d55f74.png]

https://msdn.microsoft.com/en-us/library....110).aspx
(Jun 26, 2016 06:37 AM)ubbelol Wrote:  Not sure about that attribute, but you can do something similar with code contracts: [Image: f58329c9b4eebc2f03f1c2fad4d55f74.png]

https://msdn.microsoft.com/en-us/library....110).aspx

A fast reading shows that it's what I want (validate argument isn't null), I'll be taking deeper look at it soon.
Thanks for the help :D
Have a cool signature? hit me up and I might use it.
The attributes are merely hints to the caller that the method has a certain requirement (NotNull or CanBeNull). Presumably, these attributes assist Microsoft and the CLR team in enforcing code contracts and doing unit tests.

In C#6 and roslyn i believe these attributes are internal-only, so they've been deprecated.

Some extensions / libraries have their own NotNull attributes - such as Resharper. If you include the JetBrains.Annotations library, you get [notnull] and [canbenull] and many other attributes, and if the consumer of the library has Resharper installed, they will get visual cues if they try to pass in a potential null value as a [NotNull] method parameter.

In summary, these attributes are for code contracts, and are not an excuse to skip doing null checks in your methods.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by LynxNov 27, 2020 08:23 PM 9 433 Nov 27, 2020 08:23 PM
Last Post: Ayyyther
[Python] by ChanceSep 18, 2019 11:07 PM 2 151 Sep 18, 2019 11:07 PM
Last Post: magicparty
[C] by gigajewAug 27, 2019 12:50 AM 3 208 Aug 27, 2019 12:50 AM
Last Post: int main()