Csharp 简明教程
C
常量是指程序在执行期间可能不会更改的固定值。这些固定值也称为字面量。常量可以是任何基本数据类型,例如整型常量、浮点常量、字符常量或字符串字面量,还可以是枚举常量。
The constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are also enumeration constants as well.
常量与普通变量一样对待,只是它们的定义后不能再修改其值。
The constants are treated just like regular variables except that their values cannot be modified after their definition.
Integer Literals
整型字面量可以是十进制常量或十六进制常量。前缀指定基数或进制:十进制为 0x 或 0X,无前缀表示十进制。
An integer literal can be a decimal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, and there is no prefix id for decimal.
整数文本还可以有一个后缀,它是 U 和 L 的组合,分别代表无符号和长。后缀可以是大写或小写,并且可以按任何顺序排列。
An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
以下是整数文本的一些示例:
Here are some examples of integer literals −
212 /* Legal */
215u /* Legal */
0xFeeL /* Legal */
以下是一些不同类型整数文本的其他示例:
Following are other examples of various types of Integer literals −
85 /* decimal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
Floating-point Literals
浮点文本具有整数部分、小数点、小数部分和指数部分。你可以以十进制形式或指数形式表示浮点数文本。
A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.
以下是浮点数文本的一些示例:
Here are some examples of floating-point literals −
3.14159 /* Legal */
314159E-5F /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
以十进制形式表示时,必须包括小数点、指数或两者;以指数形式表示时,必须包括整数部分、小数部分或两者。带符号的指数由 e 或 E 引入。
While representing in decimal form, you must include the decimal point, the exponent, or both; and while representing using exponential form you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.
Character Constants
字符字面量用单引号括起来。例如,“x” 存储在 char 类型的一个简单变量中。字符字面量可以是普通字符(例如 “x”)、转义序列(例如“\t”)或通用字符(例如“\u02C0”)。
Character literals are enclosed in single quotes. For example, 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').
C# 中当某些字符前加反斜杠时,它们具有特殊含义,并且用于表示换行符 (\n) 或制表符 (\t)。以下列出了部分这样的转义序列代码 -
There are certain characters in C# when they are preceded by a backslash. They have special meaning and they are used to represent like newline (\n) or tab (\t). Here, is a list of some of such escape sequence codes −
Escape sequence |
Meaning |
|\ character |
\' |
' character |
\" |
" character |
\? |
? character |
\a |
Alert or bell |
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
\xhh . . . |
以下示例演示了一些转义序列字符 -
Following is the example to show few escape sequence characters −
using System;
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 result −
Hello World
String Literals
字符串字面量或常量用双引号 "" 或 @"" 括起来。字符串包含与字符字面量类似的字符:普通字符、转义序列和通用字符。
String literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
可以使用字符串字面量并使用空格分隔各部分,将长代码行分隔为多行。
You can break a long line into multiple lines using string literals and separating the parts using whitespaces.
以下是字符串文本的一些示例。所有这三种形式都是相同的字符串。
Here are some examples of string literals. All the three forms are identical strings.
"hello, dear"
"hello, \
dear"
"hello, " "d" "ear"
@"hello dear"
Defining Constants
使用 const 关键字定义常量。定义常量的语法为 -
Constants are defined using the const keyword. Syntax for defining a constant is −
const <data_type> <constant_name> = value;
以下程序演示在程序中定义并使用常数 -
The following program demonstrates defining and using a constant in your 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 result −
Enter Radius:
3
Radius: 3, Area: 28.27431