SoFunction
Updated on 2025-03-07

Quickly understand c# constants

Constants are fixed values ​​and will not change during program execution. Constants can be any basic data type, such as integer constants, floating point constants, character constants or string constants, and enum constants.

Constants can be treated as 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 cardinality: 0x or 0X means hexadecimal, 0 means octal, and without a prefix means 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 may be uppercase or lowercase, and multiple suffixes are combined in any order.

Here are some examples of integer constants:

212        /* Legal */
215u        /* Legal */
0xFeeL     /* Legal */
078         /* Illegal: 8 is not an octal number */
032UU       /* Illegal: The suffix cannot be repeated */

Here are examples of integer constants of various types:

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 decimal part and an exponent part. You can use decimal or exponential form to represent floating point constants.

Here are some examples of floating point constants:

3.14159       /* Legal */
314159E-5L    /* Legal */
510E            /* Illegal: Incomplete Index */
210f           /* Illegal: No decimal or exponent */
.e55            /* Illegal: Missing integers or decimals */

When decimals are expressed, it must include a decimal point, an exponent, or both. When expressed in exponential form, it must include integer parts, decimal parts, or both. Signed indexes are represented by e or E.

Character constants

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

There are some specific characters in C# that have special meaning when they are preceded by backslashes and can be used to represent line breaks (\n) or tab tab (\t). Here, list some escape sequence codes:

Escape sequence meaning
\\ \ Characters
\' ' Character
\" " Character
\? ? Characters
\a Alert or bell
\b Backspace
\f Form feed
\n Newline character
\r Enter
\t Horizontal tab
\v Vertical tab
\ooo One to three digits octal number
\xhh . . . Hexadecimal number of one or more numbers

Here are some examples of escape sequence characters:

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

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

Hello   World

String constants

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

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

Here are some instances 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\\";  // \\server\share\
string h = @"\\server\share\";   // \\server\share\
string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";

Define constants

Const is 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:

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);
    ("x = {0}, y = {1}", , );
    ("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

The above is a quick understanding of the detailed content of c# constants. For more information about c# constants, please follow my other related articles!