0

I wrote code like this

using (var reader = new StreamReader("SomeFilePath"))
{
    while(reader.ReadLine() is string currentLine)
    {}
}

Then My IDE Rider suggested me below with comment "Use null check pattern"

using (var reader = new StreamReader("SomeFilePath"))
{
    while(reader.ReadLine() is {} currentLine)
    {}
}

I thought that would make syntax error, but it didn't

That Line Of Code does her job nicely.

So my question is what is {} in while(reader.ReadLine is {} currentLine)

maybe it's kind of Record Expression?

Also I could not figure out why {} currentLine is better than string currentLine

Thank you for your help

  • What version of C# are you using? – Dai Sep 27 '22 at 01:25
  • 2
    https://stackoverflow.com/questions/71849590/how-should-i-interpret-the-null-check-pattern-in-c-sharp – Mufaka Sep 27 '22 at 01:27
  • Does this answer your question? [What does "is { }" mean?](https://stackoverflow.com/questions/60417114/what-does-is-mean) – Visual Studio Sep 27 '22 at 03:31
  • I personally perfer the first version - in the second version you need to know the type returned by `ReadLine()` to know the type of `currentLine`. Arguably you should know that, but in terms of readability putting `string` makes it clearer. I apply the same argument to using `while (reader.ReadLine() is var currentLine)` – Matthew Watson Sep 27 '22 at 08:44
  • It depends if you care about the type. That is a context dependent question IMO. Rider is more generally opinionated. It would be nice if you could write `reader.ReadLine() is not null currentLine` but that is invalid syntax. – Jodrell Sep 27 '22 at 09:42
  • can you try this on your Rider `reader?.ReadLine() is string currentLine` this will null check the `reader` and return a `boolean` right away, to avoid `null` pass to `currentLine`. I believe Rider will stop warn you. – tomdinh Sep 27 '22 at 11:49

2 Answers2

2

is {} is used to match an expression against a pattern.It uses the is operator (For C# versions >= 7.0 )

So basically this line of code:

while(reader.ReadLine() is {} currentLine)

checks if the output of reader.Readline() matches any pattern(i.e. not null). If it does, then assign the output to the variable currentLine .

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
0

TLDR: { } is for pattern matching or Null checking, shorthand for If else statement.

Swift have the guard statement. Microsoft.Toolkit.Diagnostics have Guard Class, that is to highlight the important of null checking. The { } can also understand like a guard

So my question is what is {} in while(reader.ReadLine is {} currentLine)

The is operator checks if the result of an expression is compatible with a given type. your IDE Rider over reacting of comparing reader.ReadLine() with string both can be null, VS2022 give it a pass because ReadLine() is read null if the end of the input stream is reached.

Also I could not figure out why {} currentLine is better than string currentLine

Working with Null in .NET 6 and C# 10 I believe Rider want to make sure all cases are covered with { }, in most case with Reference type the IDE will give you warning of Null checking, to bypass this instead cheking with If else you just need { }

tomdinh
  • 178
  • 8
  • In the very unlikely event the return type of `ReadLine` were to change from `string` to perhaps `ReadOnlySpan` the null check would continue to work. Is that good or bad? Rider has an opinion. – Jodrell Sep 27 '22 at 09:37
  • `byte` is not reference type, it will act differently, IDE not allow you to have a null value type, like `int = null`, but now after C# 6 it is allow `int? = null`. so in most case with Reference type the IDE will give you warning of `Null` checking, to bypass this instead of type `IF ELSE` you just need `{ }` – tomdinh Sep 27 '22 at 10:08
  • @tom301 My bad but, I don't think the return of `ReadLine` will be changing. Your comment is right. However my point was, who should decide if checking the type is important, in the general case? – Jodrell Sep 27 '22 at 10:30
  • `string` is reference type that is why IDE warn. In most case, **coder should**, when dealing with `null`, this is not new in **Swift** they have the `guard statement`. Now, in VS2022 Microsoft.Toolkit.Diagnostics have `Guard Class`, that is to highlight the important of `null` checking – tomdinh Sep 27 '22 at 11:20