1. What are literals in C#? Examples of literals.

Literals – are the constant values that are presented in convenient form.

An examples of literals:

250 - literal of type int
's' - literal of type char
'@' - literal of type char
11.235 - literal of type double
"Text" - literal of type string

2. What types can be literals?

Literals may take any of base types. Representation of any literal depends on from the specified type.

Depending from value the literals can take the following types:

– integer types – int, uint, long, ulong;

– floating point types – float, double, decimal;

– character type – char;

string type string.

3. How are represented the integer literals?

By default, all integer literals are represented as int type:

An examples of literals of type int:


In order to literal had the type “long” you need to add the suffix ‘l‘ or ‘L‘. An examples of literals of long type:


In order to literal was the unassigned integer type, you need to add to it the suffix ‘u‘ or ‘U‘. Examples of literals of type uint:


Examples of literals of type ulong:


4. Can the integer literal be assigned to the variables of type “byte“, “sbyte“, “short“, “ushort“?

It is possible, but assuming that the assigned value may be represented by an integer type.


byte b;
sbyte sb;
short s;
ushort us;

b = 20;
sb = 30;
s = -30;
us = 100;

// Overflow error: "Constant value cannot be converted to 'sbyte'"
// sb = 130;

5. How did represented literals belonging to the types of floating-point?

By default, the literals of floating-point type are represented as the “double” type. In addition, to the double liberals can be added the suffix ‘d‘ or ‘D‘ (it does not change anything).

Examples of literals of type “double“:


If it is needed, to assign the type “float” for the literal, you need to add the suffix ‘f‘ of ‘F‘.

Examples of literals of type “float“:


If it is needed to have a decimal literal, you need to add the suffix ‘m‘ or ‘M‘. Examples of literals of type “decimal“.



6. How to assign the value of literals to the variables of types float and decimal?

Example of definition and assigning the literals value for variables of floating point types:

float f;
double d;
decimal x;

f = -600.321235F;
d = 1e9; // d = 1000000000
d = -2.2E-5; // d = -0.000022
x = 3049595882M;

7. How to represent the values of integer literals in hexadecimal?

C# allows to represent the integer literals in hexadecimal. Hexadecimal literals must begin with the characters “0x“.

In these literals are used numbers from 0 to 9, and also letters from ‘A‘ to ‘F‘.

Examples of represented literals in hexadecimal:


8. How are represented the character literals?

Character literals belong to the “char” type. Character literals are taken in single quotes.

Examples of character literals:


The character literals also include a character literal escape sequences, which are also called constants backslash.

Displaying such symbols causes some difficulties.

Examples of escape sequences:

\a - sound signal
\b - backspace
\f - new page
\n - new line
\r - carriage return
\t - horizontal tabulation
\v - vertical tabulation
\0 - empty symbol
\' - single quotation mark
\" - double quotation mark
\\ - backslash

An example of using the escape sequences in the program:

char symbol;
symbol = '\t'; // horizontal tabulation
symbol = '\\'; // backslash

9. How are presented string literals?

In C#, the string literal is a character set, taken in double quotes. Examples of string literals:

"This is a string literal"

If you want specify the following path with the help of literal:


it will as follows:


Example of definition the variable of string literal, that sets path to the file:

string path = "c:\\programs\\c_sharp\\myfile.txt";

10. What view takes the verbatim string literal?

The verbatim string literal begins with the @ symbol followed by a quoted string.

The contents of the literal string literal is accepted without change and may be extended to two or more lines.

For example:

string str = @"Verbatim

In a verbatim string literal you can include the control characters (tab, new line, etc.) without using the escape sequences.