locked
Difference Between Single and Double Quotes RRS feed

  • Question

  • I'll often declare a new string and intitialize it's value this way:

    string pad = "MyString";

     

    However, the same sort of declaration will not work for a char:

    char pad = "A"//throws an error

     

    Instead, a char must be declared this way:

    char pad = 'A';

     

    So now I'm wondering what the practical difference is between a single quote and a double quote in C#.

    What do they mean and how are these meanings different from each other.

    I can remember to declare things this way, but I want to understand the why.

    Friday, February 1, 2008 6:19 PM

Answers

  • I don't understand your question. The single quote is used to define a character literal. The double quote is used to define a string literal. Neither of them have any other purpose in the C# language.

     

    Friday, February 1, 2008 8:31 PM

All replies

  • You answered your own question. String literals are defined with " and char literals are defined with '

     

    If you don't understand the difference between a string and a char, it's simple: A string is an array of chars.

     

    Friday, February 1, 2008 6:23 PM
  • If you are correct then the single quote would have no other meaning anywhere else in C# code.

    Is that the case?

    Friday, February 1, 2008 8:15 PM
  • I don't understand your question. The single quote is used to define a character literal. The double quote is used to define a string literal. Neither of them have any other purpose in the C# language.

     

    Friday, February 1, 2008 8:31 PM
  • You obviously understood the question...

    Monday, June 13, 2016 2:35 PM