Literals

Primitive types, which we already met, are special data types built into the C# language. Their values specified in the source code of the program are called literals. One example will make this clearer:

Types of Literals

In C# language, there are several types of literals:

·         Boolean

·         Integer

·         Real

·         Character

·         String

·         Object literal null

Boolean Literals

Boolean literals are:

·         true

·         false

When we assign a value to a variable of type bool we can use only one of these two values or a Boolean expression (which is calculated to true or false).

Boolean Literals – Example

Here is an example of a declaration of a variable of type bool and assigning a value, which represents the Boolean literal true:

Integer Literals

Integer literals are sequences of digits, a sign (+, -), suffixes and prefixes. Using prefixes we can present integers in the program source in decimal or hexadecimal format. More information about the different numeral systems we can find in the chapter “Numeral Systems”. In the integer literals the following prefixes and suffixes may take part:

·         “0x” and “0X” as prefix indicates hexadecimal values, for example 0xA8F1;

·         ‘l’ and ‘L’ as suffix indicates long type data, for example 357L.

·         ‘u’ and ‘U’ as suffix indicates uint or ulong data type, for example 112u.

By default (if no suffix is used) the integer literals are of type int.

Integer Literals – Examples

Here are some examples of using integer literals:

Real Literals

Real literals are a sequence of digits, a sign (+, -), suffixes and the decimal point character. We use them for values of type float, double and decimal. Real literals can be represented in exponential format. They also use the following indications:

Related Posts

Comments are closed.

© 2024 Engineering Basics - Theme by WPEnjoy · Powered by WordPress