English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Constants

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

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

Integer constant

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

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

Here are some examples of integer constants:

212         /* Valid */
215u        /* Valid */
0xFeeL      /* Valid */
078         /* Invalid:8 Is not an octal digit */
032UU       /* Invalid: Cannot repeat suffix */

Here 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 constant

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

Here are some examples of floating-point constants:

3.14159       /* Valid */
314159E-5L    /* Valid */
510E          /* Invalid: Incomplete exponent */
210f          /* Invalid: No decimal or exponent */
.e55          /* Invalid: Missing integer or decimal */

When expressed in decimal form, it must contain a decimal point, exponent, or both. When expressed in exponential form, it must contain an integer part, a fractional part, or both. The signed exponent is represented by e or E.

Character literal

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

There are some specific characters in C# that have special meanings when preceded by a backslash, which can be used to represent a newline (\n) or tab (\t). Here, some escape sequence codes are listed:

Escape sequenceMeaning
\\\\ character
\'' character
\"" character
\?? character
\aAlert or bell
\bBackspace key (Backspace)
\fForm feed (Form feed)
\nNewline character (Newline)
\rCarriage return
\tHorizontal tab tab
\vVertical tab tab
\oooAn octal number with one to three digits
\xhh . . .A hexadecimal number consisting of one or more digits

The following 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 will produce the following result:

Hello	World

String Literals

String literals are enclosed in double quotes "" or enclosed in @"". The characters contained in string literals are similar to character literals and can be: ordinary characters, escape sequences, and universal characters

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

Here are some examples of string literals. The various forms listed represent the same string.

string a = "hello, world";                  // hello, world
string b = @"hello, world";               // hello, world
string c = "hello 	 world";               // hello		world
string d = @"hello 	 world";               // hello 	 world
string e = "Joe said \      // 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
two
three";
string j = @"one
two
three";

Define Constants

Constants are defined using const 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:

Online 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}
        Console.WriteLine("c1 = {0}, c2 = {1} 
                          SampleClass.c1, SampleClass.c2);
    }
}

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

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