Easy Tutorial
❮ Csharp Hashtable Csharp Anonymous Methods ❯

C# Constants

Constants are fixed values that do not change during program execution. Constants can be any of the basic data types, such as integer constants, floating-point constants, character constants, or string constants, as well as enumeration constants.

Constants can be treated as regular variables, except their values cannot be modified after they are defined.

Integer Constants

Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the base: 0x or 0X for hexadecimal, 0 for octal, and no prefix for decimal.

Integer constants can also have suffixes, which are a combination of U and L, where U and L represent unsigned and long, respectively. Suffixes can be uppercase or lowercase, and multiple suffixes can be combined in any order.

Here are some examples of integer constants:

212         /* Legal */
215u        /* Legal */
0xFeeL      /* Legal */
078         /* Illegal: 8 is not an octal digit */
032UU       /* Illegal: cannot repeat suffix */

The following are examples of various types of integer constants:

85         /* Decimal */
0213       /* Octal */
0x4b       /* Hexadecimal */
30         /* int */
30u        /* unsigned int */
30l        /* long */
30ul       /* unsigned long */

Floating-Point Constants

A floating-point constant consists of an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating-point constants using decimal form or exponential form.

Here are some examples of floating-point constants:

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

When using decimal form, you must include a decimal point, an exponent, or both. When using exponential form, you must include the integer part, the fractional part, or both. A signed exponent is indicated by e or E.

Character Constants

Character constants are enclosed in single quotes, such as 'x', and can be stored in a simple character type variable. A character constant can be a regular character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

In C#, certain characters have special meanings when preceded by a backslash and can be used to represent newline (\n) or tab (\t). Here is a list of some escape sequence 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
\ooo One to three octal digits
\xhh . . . One or more hexadecimal digits

Here are some examples of escape sequence characters:

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

String Constants

String constants are enclosed in double quotes "" or @"". String constants contain characters similar to character constants: regular characters, escape sequences, and universal characters.

When using string constants, you can break a long line into multiple lines and use spaces to separate the parts.

Here are some examples of string constants. The following forms represent the same string:

string a = "hello, world";                  // hello, world
string b = @"hello, world";               // hello, world
string c = "hello \t world";               // hello     world
string d = @"hello \t world";               // hello \t world
string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me

string f = @"Joe said ""Hello"" to me"; // Joe said "Hello" to me string g = "\\server\share\file.txt"; // \server\share\file.txt string h = @"\server\share\file.txt"; // \server\share\file.txt string i = "one\r\ntwo\r\nthree"; string j = @"one two three";


## Defining Constants

Constants are defined using the **const** keyword. The syntax for defining a constant is as follows:

const <data_type> <constant_name> = value;


The following code demonstrates how to define and use constants in a program:

## Example

using System;

public class ConstTest { class SampleClass { public int x; public int y; public const int c1 = 5; public const int c2 = c1 + 5;

    public SampleClass(int p1, int p2) 
    {
        x = p1; 
        y = p2;
    }
}

static void Main()
{
    SampleClass mC = new SampleClass(11, 22);
    Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);
    Console.WriteLine("c1 = {0}, c2 = {1}", 
                      SampleClass.c1, SampleClass.c2);
}

}


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

x = 11, y = 22 c1 = 5, c2 = 10 ```

❮ Csharp Hashtable Csharp Anonymous Methods ❯