C++程式員在學習C#時需要注意的一些問題(一)

來源:互聯網
上載者:User
c++|程式|程式員|問題

本文講述的是C++程式員在學習C#時需要注意的一些問題。


C++程式員在學習C#時需要注意的一些問題(一)

1)使用介面(interface)
在c#中,使用關鍵字interface來定義介面;而且,在c#中不能使用多重繼承。

 interface ICar//介面ICar
 {
  int Speed//屬性Speed
  {
   get;
   set;
  }

  void Run();//介面方法
  void Stop();
 }

 class MyCar : ICar //繼承介面
 {
  int _speed;

  public int Speed
  {
   get
   {
    return _speed;
   }

   set
   {
    _speed = value;
   }
  }

  public void Run()
  {
   System.Console.WriteLine("MyCar run, at speed {0}", _speed);
  }

  public void Stop()
  {
   System.Console.WriteLine("MyCar stop.");
  }
 }

2)使用數組
c#中的數組在堆中分配,因此是參考型別。c#支援3中類型的數組:一維數組(single dimensional),多維陣列(multi dimensional)和數組的數組(array of array)。

   int[] array = new int[10]; // single-dimensional array of int
   for (int i = 0; i < array.Length; i++)
    array[i] = i;

   int[ ,] array2 = new int[5,10]; // 2-dimensional array of int
   array2[1,2] = 5;

   int[ , , ] array3 = new int[5,10,5]; // 3-dimensional array of int
   array3[0,2,4] = 9;

   int[][] arrayOfarray = new int[2]; // array of array of int
   arrayOfarray[0] = new int[4];
   arrayOfarray[0] = new int[] {1,2,15};

3)使用索引(indexer)
索引使得可以像訪問數組一樣來訪問類資料。

 class IndexTest
 {
  private int[] _a;

  public IndexTest()
  {
   _a = new int[10];
  }

  public int this[int index]
  {
   get
   {
    return _a[index];
   }
  
   set
   {
    _a[index] = value;
   }
  }
 }

4)一般參數傳遞

 class PassArg
 {
  public void byvalue(int x)//By-Value
  {
   x = 777;
  }

  public void byref(ref int x)//By-Ref
  {
   x = 888;
  }

  public void byout(out int x)//Out
  {
   x = 999;
  }
 }

5)傳遞數組參數
使用params關鍵字來傳遞數組參數。

 class ArrayPass
 {
  public void Func(params int[] array)
  {
   Console.WriteLine("number of elements {0}", array.Length);
  }
 }

6)is 和 as
obj is class用於檢查對象obj是否是屬於類class的對象或者convertable。
obj as class用於檢查對象obj是否是屬於類class的對象或者convertable,如果是則做轉換操作,將obj轉換為class類型。

 class IsAs
 {
  public void work(object obj)
  {
   if(obj is MyCar)
   {
    System.Console.WriteLine("obj is MyCar object");
   
    ICar ic = obj as ICar;
    ic.Speed = 999;
    ic.Run();
    ic.Stop();
   }
   else
   {
    System.Console.WriteLine("obj is not MyCar object");
   }
  }
 }

7)foreach
   int []arr = new int[10];
   foreach(int i in arr)
    Console.WriteLine("{0}", i);

8)virtual and override
子類重載父類虛函數,需要使用override關鍵字。

 class BaseClass
 {
  public virtual void Speak()
  {
   System.Console.WriteLine("Hello, BaseClass.");
  }
 }

 class Son : BaseClass
 {
  public override void Speak()
  {
   System.Console.WriteLine("Hello, Son.");
  }
 }

 class GrandSon : BaseClass
 {
  public override void Speak()
  {
   System.Console.WriteLine("Hello, GrandSon.");
  }
 }

9)使用關鍵字new隱藏父類的函數實現

 class Shape
 {
     public virtual void Draw()
     {
  Console.WriteLine("Shape.Draw")    ;
     }
 }

 class Rectangle : Shape
 {
     public new void Draw()
     {
  Console.WriteLine("Rectangle.Draw");
     }           
 }
 class Square : Rectangle
 {
     public new void Draw()
     {
  Console.WriteLine("Square.Draw");
     }
 }

10)調用父類中的函數
使用關鍵字base來調用父類中的函數。

 class Father
 {
  public void Hello()
  {
   System.Console.WriteLine("Hello!");    
  }
 }

 class Child : Father
 {
  public new void Hello()
  {
   base.Hello();
  }
 }


 



聯繫我們

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