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

6
  • What version of C# are you using? Commented Sep 27, 2022 at 1:25
  • 2
    stackoverflow.com/questions/71849590/… Commented Sep 27, 2022 at 1:27
  • Does this answer your question? What does "is { }" mean? Commented Sep 27, 2022 at 3: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) Commented Sep 27, 2022 at 8: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. Commented Sep 27, 2022 at 9:42

2 Answers 2

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 .

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I have update it in the answer.
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 { }

4 Comments

In the very unlikely event the return type of ReadLine were to change from string to perhaps ReadOnlySpan<byte> the null check would continue to work. Is that good or bad? Rider has an opinion.
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 { }
@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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.