Note that there are some explanatory texts on larger screens.

plurals
  1. POC#: How to Implement and use a NotNull and CanBeNull attribute
    primarykey
    data
    text
    <p>I want to let programmers and myself know that a method does not want <code>null</code> and if you do send <code>null</code> to it anyways, the result will not be pretty. </p> <p>There is a <code>NotNullAttribute</code> and a <code>CanBeNullAttribute</code> in <a href="http://abdullin.com/shared-libraries/" rel="noreferrer">Lokad Shared Libraries</a>, in the <code>Lokad.Quality</code> namespace.</p> <p>But how does that work? I looked at the source-code of those two attributes, and it looks like this:</p> <pre><code>[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Delegate | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] [NoCodeCoverage] public sealed class NotNullAttribute : Attribute { } [AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Delegate | AttributeTargets.Field, AllowMultiple = false, Inherited = true)] [NoCodeCoverage] public sealed class CanBeNullAttribute : Attribute { } </code></pre> <p>Two empty classes inheriting from <code>Attribute</code>. How are they used? Do you have to look up xml-documentation and know that it is there? Cause I tried to both make my own copy of the attribute and to use the Lokad version, but when I tried to send a null directly in, I got no message. Neither from ReSharper nor from VS. Which I kind of expected actually. But how are they used? Can I somehow make VS generate warnings for me if I try to send something that is null in there? Or is it just used in some kind of testing framework? Or?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload