C#學習筆記 ----運算子和強制類型轉換

來源:互聯網
上載者:User

標籤:style   blog   color   os   使用   io   strong   ar   for   

運算子

算術運算子  +- */%

邏輯運算子  & | ^ ~ && || !

字串串連運算子  +

增量和遞減運算子  ++ --

移位元運算符  << >>

比較子  == != <> <= >=

賦值運算子  = += -= *= /= %= &= |= ^= <<= >>=

成員訪問運算子  .

索引運算子  []

類型轉換運算子  ()

條件運算子(三元運算子)  ?:

委託串連和刪除運算子  + -

對象建立運算子  new

類型資訊運算子  sizeof is typeof as

溢出異常控制運算子  checked unchecked

間接定址運算子  []

名稱空間別名限定符  ::

空合并運算子  ??

 

簡化賦值運算子

x++,++x  x=x+1x--,--x x=x-1x+=y x=x+yx-=y x=x-yx*=y x=x*yx/=y x=x/yx%=y x=x%yx>>=y x=x>>yx<<=y x=x<<yx&=y x=x&yx|=y x=x|y
int x=5;if(++x == 6)//true ----x is incremented to 6 before the evaluation{    Console.WriteLine("This will execute");}if(x++ == 7)//false ----x is incremented to 7 after the evaluation{    Console.WriteLine("This won‘t");}

 

代碼塊標記為checked,CLR就會執行溢出檢查,如果發生溢出,就拋出OverflowException異常

byte b = 255;checked{    b++;}Console.WriteLine(b.ToString());

禁止溢出檢查,標記為unchecked

 

is運算子 可以檢查對象是否與特定的類型相容

int i = 10;if (i is object){    Console.WriteLine("i" is an object");}

 

as運算子用於執行參考型別的顯示類型轉換

如果要轉換的類型與制定的類型相容,轉換就會成功進行;如果類型不相容,as運算子就會返回null值。

object o1 = "Some String";object o2 = 5;string s1 = o1 as string;//s1 = "Some String"string s2 = o2 as string;//s2 = null;

 

sizeof運算子 可以確定棧中實值型別需要的長度(單位是位元組)

Console.WriteLine(sizeof(int));

 

typeof運算子 返回一個表示特定類型的System.Type對象

 

空合并運算子?? 提供了一種捷徑,可以在處理可空類型和參考型別時表示null可能的值

 

在負責的運算式中,應避免利用運算子優先順序來產生正確的結果。使用圓括弧指定運算子的執行順序,可以使代碼整潔,避免出現潛在衝突。

 

隱式轉換

只要保證值不會發生變化,類型轉換就可以自動(隱式)進行

 

顯示轉換

類型強制轉換

long val = 30000;int i = (int)val; //A valid cast.The maximum int is 2147483647

所有預定義實值型別都支援Parse()方法

 

裝箱和拆箱可以把實值型別轉換為參考型別,並把參考型別轉換為實值型別

 

參考型別的相等性:ReferenceEquals() 和倆個版本的Equals()、比較子(==)

 

比較實值型別相等性時,採用與參考型別相同的規則:ReferenceEquals()用於比較引用,Equals()用於比較值,比較子可以看作一個中間項

 

運算子多載

運算子多載的聲明方式與方法相同,但operator關鍵字告訴編輯器,它實際上是一個自訂的運算子多載,後面是相關運算子的實際符號。

C#要求所有的運算子多載都聲明為public 和static

struct Vector{public double x,y,z;public Vector(double x,doubley,double z){    this.x = x;    this.y = y;    this.z = z;}public Vector(Vector rhs){    x = rhs.x;    y = rhs.y;    z = rhs.z;}public override string ToString(){    return "("+x+","+y+","+z+")";}public static Vector operator +(Vector lhs,Vector rhs){    Vector result = new Vector(lhs);    result.x += rhs.x;    result.y += rhs.y;    result.z += rhs.z;    return result;}//測試Vector結構static void Main(){    Vector vect1,vect2,vect3;    vect1 = new Vector(3.0,3.0,1.0);    vect2 = new Vector(2.0,-4.0,-4.0);    vect3 = vect1 +vect2;    Console.WriteLine("vect1 = " + vect1.ToString());    Console.WriteLine("vect2 = " + vect2.ToString());    Console.WriteLine("vect3 = " + vect3.ToString());}

 

C#語言要求成對重載比較子。即,如果重載了“==”,就必須重載“!=”

 

使用者定義的強制類型轉換

C#允許進行倆種不同資料類型的強制轉換:隱式強制轉換和顯式強制轉換

類型強制轉換必須同時聲明為public 和static

 

struct Currency{    public uint Dollars;    public ushort Cents;        public Currency(uint dollars,ushort cents)    {        this.Dollars = dollars;        this.Cents = cents;    }    public override string ToString()    {        return string.Format("${0}.{1,-2:00}",Dollars,Cents);    }        //隱式    public static implicit operator float(Currency value)    {        return value.Dollars + (value.Cents/100.0f);    }    //顯式    public static explicit operator Currency(float value)    {        checked        {            uint dollars = (uint)value;            ushort cents = (ushort)((value - dollars)*100);            return new Currency(dollars,cents);        }    }}

 

定義不同結構或類的執行個體之間的類型強制轉換是完全合法的,但有倆個限制:

.如果某個類派生自另一個類,就不能定義這倆個類之間的類型強制轉換(這些類型的類型轉換已經存在)

.類型強制轉換必須在來源資料類型或目標資料類型的內部定義

 

裝箱和拆箱資料類型強制轉換

 

多重類型強制轉換

C#學習筆記 ----運算子和強制類型轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.