C# constant


Release date:2023-08-23 Update date:2023-10-13 Editor:admin View counts:346

Label:

C# constant

The constant is a fixed value and does not change during program execution. Constants can be 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 cardinality: 0x or 0X for hexadecimal, 0 for octal, and no prefix for decimal.

Integer constants can also have suffixes, which can be a combination of U and L, where U and L represent respectively unsigned and long . Suffixes can 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 Not an octal number */
032UU       /* Illegal: cannot duplicate suffix */

The following are examples of various types of integer constants:

85         /* decimalism */
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 decimal part, and an exponential part. You can represent floating-point constants in either decimal or exponential form.

Here are some examples of floating point constants:

3.14159       /* legal */
314159E-5L    /* legal */
510E          /* Illegal: incomplete index */
210f          /* Illegal: No decimals or indices */
.e55          /* Illegal: missing integer or decimal */

When represented in floating-point form, you must include a decimal point, an exponent, or both. When expressed in exponential form, it must contain an integer part, a decimal part, or both. Signed exponents are represented bye or E.

Character constant

Character constants are enclosed in single quotation marks, such as 'x' can be stored in a simple character type variable, a character constant can be a normal character ( 'x' ), an escape sequence(for example, '\t' ) or a common character (for example '\u02C0' ).

There are specific characters in C # that have a special meaning when preceded by a backslash and can be used to indicate a newline character (n) or a tab character tab (t). Here, list some escape sequence codes:

Escape sequence

Meaning

\

character

'

‘character

"

“character

?

? Character

a

Alert or bell

b

Backspace key (Backspace)

f

Feed character (Form feed)

n

Newline character (Newline)

r

Enter

t

Horizontal Tab tab

v

Vertical Tab tab

ooo

Octal numbers of one to three digits

xhh…

The hexadecimal number of one or more 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 results:

Hello   World

String constant

String constants are enclosed in double quotation marks "", or included in @"". String constants contain characters similar to character constants, which can be: ordinary characters, escape sequences, and general characters

When using string constants, you can split a long line into multiple lines, and you can separate parts with spaces.

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 constant

Constant is to use the const keyword to define it. 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

Powered by TorCMS (https://github.com/bukun/TorCMS).