C# 語言中的 string 和 String 資料型別是什麼?


String 位於 System.String 的常量,而 string 是 C# 中 System.String 的一個別名

例如,

string str = "Welcome!";

雖然不是必需的,但通常在使用 class 時使用 String

string str = String.Format("Welcome! {0}!", user);

由於 string 是 System.String 的別名。其他資料型別的別名有:

示例

object: System.Object
string: System.String
bool: System.Boolean
float: System.Single
double: System.Double
decimal: System.Decimal
byte: System.Byte
sbyte: System.SByte
short: System.Int16
ushort: System.UInt16
int: System.Int32
uint: System.UInt32
long: System.Int64
ulong: System.UInt64
char: System.Char

C# 中的 String 型別允許你向變數分配任何字串值。字串型別是 System.String 類的別名。它派生自 object 型別。可以透過字串字面量使用兩種形式給字串型別分配值:帶引號和 @ 帶引號。

輸出

String str = "Okay!";

更新於:2020 年 6 月 21 日

583 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告