Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

In Entity Framework's source code (link) I found this line:

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

The [param: NotNull] part looks very strange to me. Any idea what kind of a C# syntax is this? I'm familiar with attributes and param but not this combination.

The definition of NotNull is this:

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

Which I expected to be used simply as [NotNull] but what is param doing here?

like image 894
el_shayan Avatar asked Sep 11 '15 16:09

el_shayan


People also ask

What is the meaning of Param null?

When the parameter has no value, SQL interprets it as null in your code. Null means no value. You can fix this problem by adding a code to fix the null case.

What is NotNull in C#?

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.


1 Answers

When you mark method with NotNull it means, that method returns not null object:

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

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

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

Equals to:

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

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

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

Equals to:

public virtual void set_Transaction([NotNull] IRelationalTransaction value) { ... } 
like image 99
Backs Avatar answered Oct 16 '22 02:10

Backs