Ellbar
Ellbar

Reputation: 4054

C# { } operator

I cannot find it anywhere. Could you tell me what the expression { } means in c# or give me a link to documentation.

Here is example usage which I have found in my project:

Method(IProfileDocument profileDocument)
{
    if(profileDocument.documentId is not { } documentId
    || string.IsNullOrEmpty(documentId))
    { 
       do something...
    }
}

Upvotes: 3

Views: 167

Answers (2)

MD. RAKIB HASAN
MD. RAKIB HASAN

Reputation: 3956

Property Pattern: A property pattern checks that the input value is not null and recursively matches values extracted by the use of accessible properties or fields.

                string s =null;
                if (s is not { } documentId)
                {

                }
                else
                {
                    string mj = documentId;
                }

Declaration Pattern: The declaration_pattern both tests that an expression is of a given type and casts it to that type if the test succeeds. This may introduce a local variable of the given type named by the given identifier, if the designation is a single_variable_designation.

Here first check profileDocument.documentId is null or not. after that if not null then value asign to new a variable documentId.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/patterns#property-pattern

Upvotes: 1

Piglet
Piglet

Reputation: 28950

{ } when used with is is an empty property pattern.

{ } is basically equivalent to != null

Read https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns#property-pattern

Upvotes: 5

Related Questions