C++的准程序员不可忽视的C#内容

C#和C++不同 又相当雷同 (与 Java几乎神似)

 

Item 0:C#中几乎所有东西都是对象(至少很多在C++中不是的 在C#是了)

 

Item 1:foreach(Type Variable in Array){}

和for循环相同,作用是每次循环讲Array中的一个元素复制到Variable中,在{}中使用

e.g.

int a[10] = {1,2,3,4,5,6,7,8,9,10};

for( int temp in a)

{//输出temp}

 

 

Item 2:属性 get set

public class Person{

private string P_name = "小张";

public string Name//定义一个属性 用方法get和方法set对变量P_name进行读和写

{get{return P_name;}

set{return {P_name = value;}  }  //Person p = new Person(); p.Name = "小毫"; //调用set

};

自动属性

上面完全可以简单的写成

...

 public string Name{get; set;}

...

 

Item 3:委托类型delegate

相当于函数指针(即是指向函数的指针)

先假定有一个函数   class fuction{

            void display(){  Console.WriteLine("do the displaying stuff.");}

                      };

定义委托类型      public delegate void D();  //在C++中 void (*d)();

定义 function对象     fuction fn = new fuction();

          D d = new D(fn.display); //c++中 d = display;

调用            d();  //屏幕显示do the displaying stuff.

 

 

 Item 4:

Lambda表达式  //  用 =>操作符连接参数和方法的实现语句

int[] n = new[] { 1,2,3,4,5,6,7,8};            

 int[] evenN = Array.FindAll(n ,i => ( i % 2 == 0)) ;       

 foreach(int n1 in evenN)               

    Console.WriteLine("{0}\n", n1);

 

 Item...:

还有些C++没有的东西, 因为C#着重于开发WPF程序 或者WEB 等等

 例如

 事件的预定+= 和撤销 -=

正则表达式 用 IsMatch( cmpStr,  "^[0-9]+([0-9]{1,2})?$"  );

...

 and so on and on and on

 ...

 

 

 

 

 

 

 

 

 

 

  

posted @ 2012-03-20 22:45  KelvinDesus  阅读(176)  评论(0编辑  收藏  举报