中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

C# 常量

常量是固定值,程序執(zhí)行期間不會(huì)改變。常量可以是任何基本數(shù)據(jù)類(lèi)型,比如整數(shù)常量、浮點(diǎn)常量、字符常量或者字符串常量,還有枚舉常量。

常量可以被當(dāng)作常規(guī)的變量,只是它們的值在定義后不能被修改。

整數(shù)常量

整數(shù)常量可以是十進(jìn)制、八進(jìn)制或十六進(jìn)制的常量。前綴指定基數(shù):0x 或 0X 表示十六進(jìn)制,0 表示八進(jìn)制,沒(méi)有前綴則表示十進(jìn)制。

整數(shù)常量也可以有后綴,可以是 U 和 L 的組合,其中,U 和 L 分別表示 unsigned 和 long。后綴可以是大寫(xiě)或者小寫(xiě),多個(gè)后綴以任意順序進(jìn)行組合。

這里有一些整數(shù)常量的實(shí)例:

212         /* 合法 */
215u        /* 合法 */
0xFeeL      /* 合法 */
078         /* 非法:8 不是一個(gè)八進(jìn)制數(shù)字 */
032UU       /* 非法:不能重復(fù)后綴 */

以下是各種類(lèi)型的整數(shù)常量的實(shí)例:

85         /* 十進(jìn)制 */
0213       /* 八進(jìn)制 */
0x4b       /* 十六進(jìn)制 */
30         /* int */
30u        /* 無(wú)符號(hào) int */
30l        /* long */
30ul       /* 無(wú)符號(hào) long */

浮點(diǎn)常量

一個(gè)浮點(diǎn)常量是由整數(shù)部分、小數(shù)點(diǎn)、小數(shù)部分和指數(shù)部分組成。您可以使用小數(shù)形式或者指數(shù)形式來(lái)表示浮點(diǎn)常量。

這里有一些浮點(diǎn)常量的實(shí)例:

3.14159       /* 合法 */
314159E-5L    /* 合法 */
510E          /* 非法:不完全指數(shù) */
210f          /* 非法:沒(méi)有小數(shù)或指數(shù) */
.e55          /* 非法:缺少整數(shù)或小數(shù) */

使用小數(shù)形式表示時(shí),必須包含小數(shù)點(diǎn)、指數(shù)或同時(shí)包含兩者。使用指數(shù)形式表示時(shí),必須包含整數(shù)部分、小數(shù)部分或同時(shí)包含兩者。有符號(hào)的指數(shù)是用 e 或 E 表示的。

字符常量

字符常量是括在單引號(hào)里,例如,'x',且可存儲(chǔ)在一個(gè)簡(jiǎn)單的字符類(lèi)型變量中。一個(gè)字符常量可以是一個(gè)普通字符(例如 'x')、一個(gè)轉(zhuǎn)義序列(例如 't')或者一個(gè)通用字符(例如 'u02C0')。

在 C# 中有一些特定的字符,當(dāng)它們的前面帶有反斜杠時(shí)有特殊的意義,可用于表示換行符(n)或制表符 tab(t)。在這里,列出一些轉(zhuǎn)義序列碼:

轉(zhuǎn)義序列含義
\ 字符
'' 字符
"" 字符
?? 字符
aAlert 或 bell
b退格鍵(Backspace)
f換頁(yè)符(Form feed)
n換行符(Newline)
r回車(chē)
t水平制表符 tab
v垂直制表符 tab
ooo一到三位的八進(jìn)制數(shù)
xhh . . .一個(gè)或多個(gè)數(shù)字的十六進(jìn)制數(shù)

以下是一些轉(zhuǎn)義序列字符的實(shí)例:

namespace EscapeChar
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("HellotWorldnn");
            Console.ReadLine();
        }
    }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Hello   World

字符串常量

字符串常量是括在雙引號(hào) "" 里,或者是括在 @"" 里。字符串常量包含的字符與字符常量相似,可以是:普通字符、轉(zhuǎn)義序列和通用字符

使用字符串常量時(shí),可以把一個(gè)很長(zhǎng)的行拆成多個(gè)行,可以使用空格分隔各個(gè)部分。

這里是一些字符串常量的實(shí)例。下面所列的各種形式表示相同的字符串。

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";   // \serversharefile.txt
string h = @"\serversharefile.txt";      // \serversharefile.txt
string i = "onerntwornthree";
string j = @"one
two
three";

定義常量

常量是使用 const 關(guān)鍵字來(lái)定義的 。定義一個(gè)常量的語(yǔ)法如下:

const <data_type> <constant_name> = value;

下面的代碼演示了如何在程序中定義和使用常量:

實(shí)例

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);
? ? }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

x = 11, y = 22
c1 = 5, c2 = 10