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
ReadLine()to know the type ofcurrentLine. Arguably you should know that, but in terms of readability puttingstringmakes it clearer. I apply the same argument to usingwhile (reader.ReadLine() is var currentLine)reader.ReadLine() is not null currentLinebut that is invalid syntax.