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

Visually in C# code what is the string literal?

I’m confused as to what exactly is a string literal in code.
In looking at the following code: "Apple"

Is Apple the string literal? (the pair of double quotes IS NOT considered part of a string literal)

Or is "Apple" the string literal? (the pair of double quotes IS considered part of a string literal)

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

>Solution :

Have a look at the formal grammar here:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#tokens

It is clear that string literal is defined as:

string_literal
    : regular_string_literal
    | verbatim_string_literal
    ;
regular_string_literal
    : '"' regular_string_literal_character* '"'
    ;
....
verbatim_string_literal
    : '@"' verbatim_string_literal_character* '"'
    ;
....

Or simply speaking, string literal is any token of either type "String" or @"String", so answering your questions – quotes are part of the literal.

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