C#內建資料類型

來源:互聯網
上載者:User

C# 是一種強型別語言。在變數中儲存值之前,必須指定變數的類型,如以下樣本所示:

 
int a = 1;string s = "Hello";XmlDocument tempDocument = new XmlDocument();

注意,對於簡單的內建類型(如 )以及複雜的或自訂的類型(如)都必須指定類型。

C# 包括對下面的內建資料類型的支援:

資料類型 範圍

byte    

0 .. 255

sbyte

-128 .. 127

short

-32,768 .. 32,767

ushort

0 .. 65,535

int

-2,147,483,648 .. 2,147,483,647

uint

0 .. 4,294,967,295

long

-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807

ulong

0 .. 18,446,744,073,709,551,615

float

-3.402823e38 ..3.402823e38

double

-1.79769313486232e308 ..1.79769313486232e308

decimal

-79228162514264337593543950335 .. 79228162514264337593543950335

char

一個 Unicode 字元。

string

Unicode 字元的一個字串。

bool

True 或 False。

object

一個對象。

這些資料類型名稱為 命名空間中的預定義類型的別名。節中列出了這些類型。所有這些類型(除對象和字串以外)均為實值型別。

使用內建資料類型

內建資料類型在 C# 程式中有幾種用法。

作為變數:

C#  複製代碼
int answer = 42;string greeting = "Hello, World!";

作為常數:

C#  複製代碼
const int speedLimit = 55;const double pi = 3.14159265358979323846264338327950;

作為傳回值和參數:

C#  複製代碼
long CalculateSum(int a, int b){    long result = a + b;    return result;}

若要定義自己的資料類型,請使用類(Visual C# 速成版)枚舉(Visual C# 速成版)結構(Visual C# 速成版)

轉換資料類型

資料類型間的轉換可以隱式完成(轉換由編譯器自動完成)或使用強制轉換顯式完成(程式員強制進行轉換,並承擔丟失資訊的風險)。

例如:

C#  複製代碼
int i = 0;double d = 0;i = 10;d = i;        // An implicit conversiond = 3.5;i = (int) d;  // An explicit conversion, or "cast"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.