What is the difference between using single quotes and double quotation marks in C #?

10

In PHP, when we use single quotes or double quotation marks, both forms have the function of declaring a string . There is only one small difference.

I'm now starting a C # study. When I tried to declare a string with single quotation marks, an error was generated.

 [Table('ProductSuppliers')]
  Too many characters in character literal

In PHP I had no problem using single quotes in cases like the one above.

Since the error is being pointed out, it makes me think that single quotes do not have the purpose I was thinking.

  • What is the purpose of Simple Quotes in C #?

  • What's the difference between it and Double Quotes?

  • Why did the error described above occur when I tried to use the 'ProductSupplier' statement?

asked by anonymous 28.05.2016 / 21:33

3 answers

10

It is different from PHP where they are almost interchangeable.

Single quotes or apostrophes are only used to delimit a single character (type char - UTF-16).

Double quotes serve to delimit a string , that is, an immutable collection of chars with a specific format.

The error indicates that it has more than one character. As it is a text and not just a character, just put double quotation marks.

strings can be defined with some prefixes to facilitate certain operations:

28.05.2016 / 21:36
9

Unlike PHP, in C #, they represent two different types.

Simple quotes serve to delimit a single character (type char ).

Double quotes serve to delimit a string (which is an immutable collection of chars ).

The error, in your case, occurs because there is more than one character between single quotes. What you intend to use there is a string , so just change the single quotes to double.

Example:

char meuChar = 'A';
char[] meuCharArray = { 'O', 'L', 'Á' };
string minhaString = "OLÁ";

Some other languages also work that way.

    
30.05.2016 / 15:42
8

The single quotes represent a char, so they should be used ONLY to assign characters. Ex:

char sexo = 'M';

Double quotes represent a string (a collection of char). Ex:

string sexo = "masculino";

Note that a String (string, or array of char) may contain only one character, but a NEVER character may contain more than one character. Ex:

string sexo = "M"; // Correto, uma cadeia de caracteres pode conter apenas um caractere.
char sex = 'sexo'; // Erro de compilação, um char não pode conter mais que um caractere.
    
30.05.2016 / 16:22