Home > database >  C# { } operator
C# { } operator

Time:09-13

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...
    }
}

CodePudding user response:

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

{ } is basically equivalent to != null

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

CodePudding user response:

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://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/patterns#property-pattern

  • Related