What does [param: NotNull] mean in C #?

In the source code of the Framework Entity Framework ( link ) I found this line:

public virtual IRelationalTransaction Transaction { get; [param: NotNull] protected set; } 

The [param: NotNull] looks very strange to me. Any idea what C # syntax is? I am familiar with attributes and parameter, but not with this combination.

The definition of NotNull is as follows:

 [AttributeUsage( AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Delegate | AttributeTargets.Field)] internal sealed class NotNullAttribute : Attribute { } 

What I expected to use simply as [NotNull] , but what does param do here?

+57
c #
Sep 11 '15 at 16:32
source share
2 answers

When you mark a method with NotNull , this means that this method returns a non-null object:

 [NotNull] public object Get() { return null; //error } 

When you mark setter, it does the same - setter returns non-null (because .net converts properties to get and set methods).

 public virtual IRelationalTransaction Transaction { get; [NotNull] protected set; } 

Equality:

 [NotNull] public virtual void set_Transaction(IRelationalTransaction value) { ... } 

So, you need to add param: to the point that "i mean - the setter parameter is not null, not the result of the set-method":

 public virtual IRelationalTransaction Transaction { get; [param: NotNull] protected set; } 

Equality:

 public virtual void set_Transaction([NotNull] IRelationalTransaction value) { ... } 
+59
Sep 11 '15 at 16:39
source share

param: is the target of the attribute. See: Attribute Specification

The target attribute can be one of the following:

assembly, module, field, event, method, param, property, return, type

Thus, [param: NotNull] means that NotNullAttribute is applied to the value parameter of the installer. You must specify a goal here, since the value parameter is not explicitly displayed as a method parameter.




Typically, the target attribute is used to specify InternalsVisibleToAttribute so that types and members declared as internal are visible to unit test projects.

 [assembly:InternalsVisibleTo("UnitTestProject")] 

An assembly is not displayed as a language construct, so the assembly attribute is the only way to specify an attribute for the assembly. By the way: this can be specified in any source code file.

+40
Sep 11 '15 at 16:41
source share



All Articles