Home > Software engineering >  Visually in C# code what is the string literal?
Visually in C# code what is the string literal?

Time:12-26

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)

CodePudding user response:

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 form "String" or @"String", so answering your questions - quotes are part of the literal.

  • Related