Latest web development tutorials

C # Constants

A constant is a fixed value does not change during program execution. Constants can be any of the basic data types, such as integer constants, floating point constants, character constants or string constants, and enumeration constants.

Constants can be used as a conventional variables, but their value can not be modified after the definition.

Integer constant

Integer constants can be decimal, octal or hexadecimal constants. Prefix specified radix: 0x or 0X for hexadecimal, 0 for octal, decimal representation is not prefixed.

Integer constants can have the suffix, it may be a combination of U and L, wherein, U and L, respectively, and unsigned long. Suffix can be uppercase or lowercase, multiple suffixes 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: can not duplicate suffix * /

The following are examples of various types of integer constants:

85 / * decimal * /
0213 / * octal * /
0x4b / * Hex * /
30 / * int * /
30u / * unsigned int * /
30l / * long * /
30ul / * unsigned long * /

Floating-point constants

A floating-point constant is an integer part, a decimal point, and the fractional part of the index components. You can use a 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 fractional or index * /
.e55 / * Illegal: missing integer or fractional * /

When using the decimal representation, it must contain a decimal point, index, or both. When using the index form, it must contain the integer part, fractional part, or both. Signed exponent e or E is represented.

Character constant

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

In C # there is some specific characters have special meaning when in front of them with a backslash can be used to represent a newline character (\ n) or tab tab (\ t). Here are some escape sequences code:

转义序列含义
\\\ 字符
\'' 字符
\"" 字符
\?? 字符
\aAlert 或 bell
\b退格键(Backspace)
\f换页符(Form feed)
\n换行符(Newline)
\r回车
\t水平制表符 tab
\v垂直制表符 tab
\ooo一到三位的八进制数
\xhh . . .一个或多个数字的十六进制数

Here are some examples of character escape sequences:

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

Character constants are enclosed in double quotes, "" where, or enclosed in! "" Inside. Similar characters and character constants string constants contained, can be: ordinary characters, escape sequences and general character

When using a string constant, can be a very long line is split into multiple lines, you can use spaces to separate parts.

Here are some examples of string constants. Various forms listed below represent the same string.

"Hello, dear"
"Hello, \
dear "
"Hello," "d" "ear"
@ "Hello dear"

Defining Constants

Constants are defined using theconst keyword.Define a constant the following syntax:

const <data_type> <constant_name> = value;

The following code shows how to define and use constants in the program:

using System;

namespace DeclaringConstants
{
    class Program
    {
        static void Main (string [] args)
        {
            const double pi = 3.14159; // constant declarations 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