C # Constants
Constants are fixed values that 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, as well as enumeration constants.
Constants can be used as regular variables, except that their values cannot be modified after they are defined.
Integer constants
An integer constant can be a decimal, octal, or hexadecimal constant. 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 unsigned and long respectively. 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 is not an octal digit * /
032UU / * Illegal: Suffix cannot be repeated * /
The following are examples of the 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 consists of an integral part, a decimal point, a fractional part, and an exponential 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 integer or decimal * /
When you use decimal notation, you must include a decimal point, an exponent, or both. When used as an exponential representation, you must include an integer part, a fractional part, or both. The signed exponent is denoted 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 (for example, ' X '), an escape sequence (such as ' \ t '), or a universal character (such as ' \u02c0 ').
There are certain characters in C # that have a special meaning when preceded by a backslash, which can be used to represent a newline character (\ n) or Tab tab (\ t). Here, some escape sequence codes are listed:
Escape sequences
Meaning
\ \ \ Character
\ ' ' character
\ "" Character
\? ? Character
\a Alert or Bell
\b Backspace (Backspace)
\f page Break (Form feed)
\ n line break (Newline)
\ r Enter
\ t Horizontal tab tab
\v Vertical Tab tab
\ooo octal number from one to three bits
\xhh ... 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 constants
Character constants are enclosed in double quotes "", or are enclosed in @ "". String constants contain characters similar to character constants, which can be: ordinary characters, escape sequences, and universal characters
When you use string constants, you can split a very long line into multiple rows, and you can separate sections with spaces.
Here are some examples of string constants. The various forms listed below represent the same string.
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
@"hello dear"
Defining constants
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; // constant declaration
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:3radius:3, area:28.27431
The above is the "C # tutorial" C # constant content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!