C# Constants and Literals types

Constants and Literals, seek advice from constant values that this system may not adjust during its execution. These constant values are also called literals. Constants can be of any of the primary statistics sorts like an integer steady.

A floating steady, a individual regular, or a string literal. There are also enumeration constants as properly.

The constants are handled just like regular variables besides that their values can not be changed after their definition.

Integer Literals

An integer literal may be a decimal, or hexadecimal consistent. A prefix specifies the bottom or radix: 0x or 0X for hexadecimal, and there’s no prefix identity for decimal.

An integer literal can also have a suffix that is a aggregate of U and L, for unsigned and lengthy, respectively. The suffix can be uppercase or lowercase and may be in any order.

Here are a few examples of integer literals

212         /* Legal */
215u        /* Legal */
0xFeeL      /* Legal */

Following are other examples of various types of Integer literals −

85         /* decimal */
0x4b       /* hexadecimal */
30         /* int */
30u        /* unsigned int */
30l        /* long */
30ul       /* unsigned long */

Floating-factor Literals

A floating-factor literal has an integer element, a decimal point, a fractional element, and an exponent part. You can represent floating point literals either in decimal shape or exponential shape.

Here are a few examples of floating-factor literals

3.14159       /* Legal */
314159E-5F    /* Legal */
510E          /* Illegal: incomplete exponent */
210f          /* Illegal: no decimal or exponent */
.e55          /* Illegal: missing integer or fraction */

While representing in decimal shape, you have to consist of the decimal point, the exponent, or both; and while representing using exponential shape you must consist of the integer element, the fractional element, or each. The signed exponent is added by way of e or E.

Character Constants

Character literals are enclosed in unmarried quotes. For example, ‘x’ and may be stored in a simple variable of char type.

A character literal can be a plain character (along with ‘x’), an break out sequence (inclusive of ‘t’), or a ordinary character (inclusive of ‘u02C0’).

There are certain characters in C# whilst they are preceded with the aid of a backslash.

They have special meaning and they may be used to symbolize like newline (n) or tab (t).

Here, is a listing of a number of such get away series codes

Escape sequence Meaning
\\ \ character
\’ ‘ character
\” ” character
\? ? character
\a Alert or bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\xhh . . . Hexadecimal number of one or more digits

Following is the example to show few escape sequence characters

using System;

namespace EscapeChar {
   class Program {
      static void Main(string[] args) {
         Console.WriteLine("Hello\tWorld\n\n");
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Hello World

Constants and Literals, String Literals

String literals or constants are enclosed in double costs “” or with @””. A string contains characters which are much like person literals: undeniable characters, break out sequences, and popular characters.

You can ruin an extended line into a couple of lines using string literals and keeping apart the components the use of whitespaces.

Here are some examples of string literals. All the 3 bureaucracy are identical strings.

“hello, dear” “hello, \ dear” “hello, ” “d” “ear” @”hello dear”

Constants and Literals, Defining Constants

Constants are described using the const keyword. Syntax for defining a regular is

const <data_type> <constant_name> = value;

The following application demonstrates defining and using a consistent for your program

using System;

namespace DeclaringConstants {
   class Program {
      static void Main(string[] args) {
         const double pi = 3.14159;   
            
         // constant declaration 
         double r;
         Console.WriteLine("Enter Radius: ");
         r = Convert.ToDouble(Console.ReadLine());
            
         double areaCircle = pi * r * r;
         Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result

Enter Radius: 3 Radius: 3, Area: 28.27431