C 的符號用法 ?問號用法

2021-10-05 07:21:01 字數 3655 閱讀 7359

在字串前加$相當於對string

.format

()的簡化

如:int m_a =1;

int m_b =2;

使用string

.format():

console.

writeline

(string

.format

("this is a:,this is b:"

),m_a,m_b));

使用了$,就可以在原來佔位符的地方直接用引數代替

console.

writeline

($"this is a: ,this is b:");

datetime dt =

newdatetime

(2017,4

,1,13

,16,32

,108);

string

.format(""

,dt)

;//17 17 2017 2017

string

.format(""

, dt)

;//4 04 四月 四月

string

.format(""

, dt)

;//1 01 週六 星期六

string

.format(""

, dt)

;//下 下午

string

.format(""

, dt)

;//13 13

string

.format(""

, dt)

;//1 01

string

.format(""

, dt)

;//16 16

string

.format(""

, dt)

;//32 32

string

.format(""

, dt)

;//1 1 108 108 108 108 108

string

.format(""

, dt)

;//1 10 108 1080 10800 108000 1080000

string

.format(""

, dt)

;//+8 +08 +08:00

string

.format(""

,dt)

;//2017/04/01 13:16:32.108

string

.format(""

, dt)

;//2017/04/01 星期六

string

.format(""

, dt)

;//2017/04/01 星期六 下午 01:16

string

.format(""

, dt)

;//20170401

string

.format(""

, dt)

;//2017-04-01 13:16:32.108

1.限定字串

用 @ 符號加在字串前面表示其中的轉義字元「不」被處理。

如果我們寫乙個檔案的路徑,例如"d:/文字檔案"路徑下的text.txt檔案,不加@符號的話寫法如下:

stringfilename=

"d://文字檔案"

;如果使用@符號就會比較簡單:

stringfilename=

@"d:/文字檔案/text.txt"

;2.讓字串跨行

有時候乙個字串寫在一行中會很長(比如sql語句),不使用@符號,一種寫法是這樣的:

string strsql=

"select * from humanresources.employee as e"

+"inner joinperson.contact as c"

+"on e.contactid=c.contactid"

+"orderby c.lastname"

; 加上@符號後就可以直接換行了:

string strsql=

@"select * from humanresources.employee as e inner join person.contact as c on e.contactid=c.contactid orderbyc.lastname"

;

1. 可空型別修飾符(?):

引用型別可以使用空引用表示乙個不存在的值,而值型別通常不能表示為空。

例如:string str=

null

;是正確的。

int i=

null;編譯器將報錯。

為了使值型別也可為空,可空型別出現了,可空型別使用可空型別修飾符?來表示,表現形式為t?。

例:int

?表示是可空的整形,datetime?表示為可空的時間。

t?其實是system.nullable(泛型結構)的縮寫形式,也就意味著當你用到t?時編譯器在編譯時會把t?編譯成system.nullable的形式,

例如:int

?,編譯後便是system.nullable<

int>的形式。

2. 三元(運算子)表示式(?:

):語法為:條件表示式?表示式1:表示式2;

該操作首先求出條件表示式的值(

bool型別)

,為true時呼叫表示式1

,為flase時呼叫表示式2。

其邏輯為:"如果為真執行第乙個,否則執行第二個。"

例: test ? expression1 : expression2

test 任何 boolean 表示式。

expression1 test 為 true 時返回的表示式。可能是逗點表示式。

expression2 test 為 false 時返回的表示式。可能是逗點表示式。

例如:string prm1=

"4";

string prm2=

"5";

string prm3 = prm1==prm2?

"yes"

:"no"

// 此時prm3值為"no".

3. 空合併運算子(??):

空合併運算子 (

null coalescing operator)?

?  用於定義可空型別和引用型別的預設值。如果此運算子的左運算元不為 null,則此運算子將返回左運算元;否則返回右運算元。

例:a?

?b 如果 a 為非空,則 a ?

? b 的結果為 a;否則結果為 b 。

空合併運算子為右結合運算子,即操作時從右向左進行組合的。

例:「a?

?b??c」的形式按「a?

?(b?

?c)」計算。

4.實體變數名之後加?

string s =

"123456789"

;var r1 = s?

.last()

;s =

null

;var r2 = s?

.last()

;當變數不為null時,正常執行;當變數為null時,返回null。

C 中問號 的用法

看別人的 c 中有個奇怪的問號 public datetime?statusdatetime null 腦子便也出現個問號,這是什麼意思呢?網上搜下,總結如下 1.可空型別修飾符 引用型別可以使用空引用表示乙個不存在的值,而值型別通常不能表示為空。例如 string str null 是正確的,in...

關於Android的問號?和 符號的用法

表示引用資源,宣告這是乙個資源引用 隨後的文字是以 package type name形式提供的資源名。android string表明引用的系統的 android.資源 string表示引用應用內部資源 對於id,可以用 id表明建立乙個id 表示引用屬性 引用主題屬性,當您使用這個標記,你所提供...

關於Android的問號?和 符號的用法

表示引用資源,宣告這是乙個資源引用 隨後的文字是以 package type name形式提供的資源名。android string表明引用的系統的 android.資源 string表示引用應用內部資源 對於id,可以用 id表明建立乙個id 表示引用屬性 引用主題屬性,當您使用這個標記,你所提供...