58

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?

2 Answers 2

62

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) { ... }
4
  • 2
    the setter does not return value. what do you mean by "setter returns no null" in you answer Sep 11, 2015 at 16:42
  • 7
    @HakamFostok stroingly, it returns Void, and you can mark any method with NotNull, even if it returns "nothing" (void).
    – Backs
    Sep 11, 2015 at 16:44
  • Isn't the generated setter method protected?
    – chi
    Sep 12, 2015 at 11:35
  • @chi maybe, i don't remember exactly, but I rink, it's not important for this question
    – Backs
    Sep 13, 2015 at 3:28
41

param: is the attribute target. See: Attribute specification

The attribute target can be one of these:

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

So [param: NotNull] means that the NotNullAttribute applies to the value parameter of the setter. It is necessary to specify the target here, since the value parameter does not appear as a method parameter explicitly.


A common use of the attribute-target is to specify the InternalsVisibleToAttribute in order to make types and members declared as internal visible to unit test projects.

[assembly:InternalsVisibleTo("UnitTestProject")]

The assembly does not appear as a language construct, therefore the attribute-target assembly is the only way to specify an attribute for the assembly. Btw: It can be specified in any source code file.

4
  • 4
    I think this was supposed to be the accepted answer, but anyway.
    – Tarik
    Sep 16, 2015 at 1:13
  • Is there a version of that link for something more recent than .NET 1.1? Sep 18, 2015 at 2:58
  • Unfortunately the C# Language Specification is only avaialable as download, so I cannot give you a direct link. Sep 19, 2015 at 17:23
  • 1
    UPDATE: Now the language specification and reference is available here: C# Reference Sep 9, 2019 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.