3

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...
    }
}
Ellbar
  • 3,984
  • 6
  • 24
  • 36
  • Maybe this will help: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#77-scopes – Ibrennan208 Sep 13 '22 at 05:57
  • The curly braces are a way to indicate scope, and show what is encapsulated within another member. – Ibrennan208 Sep 13 '22 at 05:58
  • You may also see them used in [String Interpolation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated) – Ibrennan208 Sep 13 '22 at 06:00
  • You might also see them in an array or list declaration: `new int[] { 1, 2, 3 };` – Ibrennan208 Sep 13 '22 at 06:01
  • Otherwise you may be asking a duplicate of this question: https://stackoverflow.com/questions/62139886/meaning-of-curly-braces-after-the-is-operator – Ibrennan208 Sep 13 '22 at 06:02
  • Does this answer your question? [Meaning of curly braces after the "is" operator](https://stackoverflow.com/questions/62139886/meaning-of-curly-braces-after-the-is-operator) – mkrieger1 Sep 13 '22 at 08:39

2 Answers2

5

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

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • 1
    [This reference](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/patterns#property-pattern) has the exact `(s is {} x)` pattern seen in the question, and its equivalent statements. Admittedly, it is one of the weirdest constructs in the language.. – NPras Sep 13 '22 at 06:09
  • So what is the difference between (s is {} x) and (s is not {} x)? – Ellbar Sep 13 '22 at 06:18
  • not negates the pattern. the difference between `is` and `is not` should be self-explainatory :-) – Piglet Sep 13 '22 at 09:12
1

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

MD. RAKIB HASAN
  • 3,670
  • 4
  • 22
  • 35