Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The C#constant


May 11, 2021 C#


Table of contents


The constant of C#

Constants are fixed values and do 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 enumeration constants.

Constants can be treated as regular variables, but their values cannot be modified after definition.

Integer constant

An integer constant can be a heteth, octal, or heteen constant. The prefix specifies a base: 0x or 0X for hex, 0 for octal, and no prefix for hex.

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 capital or small, with multiple suffixes combined in any order.

Here are some examples of integer constants:

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

The following are examples of various types of integer constants:

85         /* 十进制 */
0213       /* 八进制 */
0x4b       /* 十六进制 */
30         /* int */
30u        /* 无符号 int */
30l        /* long */
30ul       /* 无符号 long */

Floating-point constant

A floating-point constant is made up of an integer part, a small number, a small part, and an exponential part. You can use the form of a small number or an exponent to represent floating-point constants.

Here are some examples of floating-point constants:

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

When represented in a small number, you must include a number of points, an exponent, or both. W hen represented in exponential form, you must include an integer part, a small part, or both. The signed exponent is represented by e or E.

The character constant

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 (such as 'x'), an escape sequence (such as ''t'), or a generic character (e.g. ''u02C0').

There are specific characters in C# that have a special meaning when they have backslashes in front of them, which can be used to represent line breaks or tab tabs. Here, list some escape sequence codes:

Escape sequence meaning
\\ \ Characters
\' 'Characters
\" "Character
\? ? Character
\a Alert or Bell
\b Backspace
\f Change page (FORM feed)
\n Removal (Newline)
\r Bus stop
\t Horizontal tab Tab
\v Vertical tab Tab
\ooo One to three binary numbers
\xhh . . . One or more numbers of hexadecimal numbers

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

The character constant is enclosed in double quotes , or in the word " . String constants contain characters that are similar to character constants and can be normal characters, escape sequences, and generic characters

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

Here are some instances of string constants. The various forms listed below represent the same string.

"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
@"hello dear"

Define the constant

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:

using System;

namespace DeclaringConstants
{
    class Program
    {
        static void Main(string[] args)
        {
            const double pi = 3.14159; // 常量声明
            double r;
            Console.WriteLine("Enter Radius: ");
            r = Convert.ToDouble(Console.ReadLine());
            double areaCircle = pi * r * r;
            Console.WriteLine("Radius: {0}, Area: {1}", r, areaCircle);
            Console.ReadLine();
        }
    }
}

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

Enter Radius: 
3
Radius: 3, Area: 28.27431