Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Underscore usage inside loop

Today, I encountered this new syntax in a C# program:

var sb = new StringBuilder();
foreach (var ns in this.Namespaces)
{
    _ = sb.AppendFormat(CultureInfo.InvariantCulture, " {0}", ns.Value);
}

The underscore inside the loop is never defined, and the code above seems to compile just fine.
So I think the underscore is a syntactic sugar of some kind. But seems that my Google skill failed me this time (I found perl and python information about underscore, or a JS library named underscore).

So it’s not clear to me the meaning of the underscore in the code snippet. Can someone clarify it to me?
The code is taken from this library :
https://www.nuget.org/packages/OpenGraph-Net/4.0.2-alpha.0.6

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A Net Core 6.0 library written in C#

>Solution :

The term you need to search for is discard variables – the underscore indicates that the variable is intentionally unused.

See here for more details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading