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, string constants, and enumeration constants.

Constants can be treated like regular variables, except that their values cannot be modified after definition.

Integer constants

Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the radix: 0x or 0X indicates hexadecimal, 0 indicates octal, and no prefix indicates 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. The suffix can be uppercase or lowercase, and multiple suffixes can be combined in any order.

Here are some examples of integer constants:

212         /* 合法 */
215u        /* 合法 */
0xFeeL      /* 合法 */
078         /* 非法:8 不是一个八进制数字 */
032UU       /* 非法:不能重复后缀 */

The following are examples of integer constants of various types:

85         /* 十进制 */
0213       /* 八进制 */
0x4b       /* 十六进制 */
30         /* int */
30u        /* 无符号 int */
30l        /* long */
30ul       /* 无符号 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 express floating-point constants in decimal form or exponential form.

Here are some examples of floating-point constants:

3.14159       /* 合法 */
314159E-5L    /* 合法 */
510E          /* 非法:不完全指数 */
210f          /* 非法:没有小数或指数 */
.e55          /* 非法:缺少整数或小数 */

When expressed in floating-point form, it must include a decimal point, an exponent, or both. When expressed in exponential form, it must include an integer part, a fractional part, or both. A signed exponent is represented with e or E.

Character constants

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

In C#, there are certain characters that have special meaning when preceded by a backslash. They can be used to represent a newline (\n) or a tab (\t). Here, some escape sequence codes are listed:

Escape sequencesMeaning
\\\ character
\'' character
\"" character
\?? character
\aAlert or bell
\bBackspace key
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab (tab)
\vVertical tab (tab)
\oooOctal number of one to three digits
\xhh . . .Hexadecimal number 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 produces the following results:

Hello   World

String constants

String constants are enclosed in double quotes""inside, or enclosed in@""Inside. String constants contain characters similar to character constants, which can be: ordinary characters, escape sequences, and universal characters.

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

Here are some examples of string constants. The various forms listed below 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";

Define constants

Constants are usedconstdefined by keywords. 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 results:

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