-->

08 April 2020

C# Literals

  Asp.Net CS By Example       08 April 2020
 C# Literals 

 In this post, we are learning more about the C# Literals. We have already used or using this in our program source code. But We do not known what we call them. In our program we used the Primitive datatypes and value also assign. Which value assigned to primitive data types are known as Literals.

 Below example will make this clearer:

 bool isFound = false;
 char Gender = 'M';
 byte age = 28;
 short salary = 20000;
 int i = 300000;

 In the above example, literals are false,'M', 28, 20000 and 300000. They are variable values set directly in the program source code.

Literals Types:
 There are several types of literals in C# language.
 - Boolean
 - Integer
 - Real
 - Character
 - String
 - Object literal null

C# Literals

Boolean Literals:
 Boolean literals are:
 - true
 - false.
  When we declare a variable of boolean type then we can assign the only one of the true or false value.

Boolean Literals – Example
  Below is an example of a boolean type variable declaration and assigning a value.In this represention the boolean literal is false.

 bool isFound = false;

Integer Literals:
  Integer literals are sequences of digits, a sign (+, -), suffixes and prefixes. By using prefixes or suffixes we can specified the integer type in program source is long or decimal or hexadecimal format.

  In the integer literals the following prefixes and suffixes may used:
  - "0x" and "0X" as prefix used indicates hexadecimal values, for example 0xAF;
  - 'l' and 'L' as suffix used indicates long type data, for example 37L.
  - 'u' and 'U' as suffix used indicates uint or data type, for example 12u.

  If we suffix not used then by default integer literal type is int.

Integer Literals – Example
  Here are some examples of using integer literals.

 // The following variables are initialized with the same value
 int numberInDec = 16;
 int numberInHex = 0x10;
 // This will cause an error, because the value 234L is not int
 int longInt = 234L;

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.

  In the real literals the following exponent and suffixes may used:
  - 'f' and 'F' as suffixes used indicates float data type.
  - 'd' and 'D' as suffixes used indicates double data type.
  - 'm' and 'm' as suffixes used indicates decimal data type.
  - 'e' is an exponent, for example, "e-5" means the integer part multiplied by 10-5.

  If we suffix not used then by default real literal type is double.

Real Literals – Example
  Here are some examples of using Real literals.

 // The following is the correct way of assigning a value
 float realNumber = 12.5f;
// This is the same value in exponential format
float realNumber = 1.25e+1f;
// The following causes an error, because 12.5 is double
float realNumber = 12.5;

Character Literals:
  Character literals are single characters enclosed in single quotes (apostrophes). We use them to set the char type value.

  The value of a character literal can be:
  - a character, for example 'A'.
  - a character code, for example '\u0065'.
  - an escaping sequence.

  What is Escaping Sequences?.
   When we working with characters then we need to use 'New Line','Tab' or 'Single Quote' in our Text. But it is directly not typed in text. To used this in text we need to take help of Escaping Sequences. this is also known as 'Special Characters'.

   Escaping sequences are literals. They are a special characters sequence, which describe a character that cannot be written directly in program source code.

  Here are the most frequently used escaping sequences:
  - \' – Single Quote
  - \" – Double Quotes
  - \\ – Backslash
  - \n – New Line
  - \t – Offset (Tab)
  - \uXXXX – Char specified by its Unicode number, for example \u03A7.

 The character \ (backslash) is also called an 'Escaping Character' because it allows the display on screen or output device of characters that have special meaning or effect and cannot be represented directly in the source code.

Escaping Sequences – Example
  Here are some examples of Escaping Sequences.

 // An ordinary character
char character = 'a';
Console.WriteLine(character);
// Unicode character code in a hexadecimal format
character = '\u003A';
Console.WriteLine(character);
// Assigning the single quotiation character (escaped as \')
character = '\'';
Console.WriteLine(character);
// Assigning the backslash character (escaped as \\)<
character = '\\';
Console.WriteLine(character);

// Console output: // a // : // ' // \

String Literals:
  String literals are used to store data of type string. String literals are sequence of characters enclosed in Double Quotation Marks. All the escaping rules for the char type discussed above are also valid for string literals. If we want to used what we store in string type we want to used without appling escaping rules then String literal are preceded by the @ character. This string is called as verbatim string. Quoted strings are often used for the file system paths naming.

String Literals – Example
  Here are few examples for string literals usage.

 string quotation = "\"Hello, Ram\", he said.";
 Console.WriteLine(quotation);
 string path = "C:\\Windows\\Notepad.exe";
 Console.WriteLine(path);
 string verbatim = @"The \ is not escaped
     as \\.\nI am at a new line.";
 Console.WriteLine(verbatim);
 
// Console output: // "Hello, Ram", he said. // C:\Windows\Notepad.exe // The \ is not escaped as \\. // I am at a new line.

logoblog

Thanks for reading C# Literals

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.