[C#]从C# 2.0新特性到C# 3.5新特性

来源:互联网 发布:js删除数组指定元素 编辑:程序博客网 时间:2024/05/18 03:36

一、C# 2.0 新特性:

1、泛型
List<MyObject> obj_list=new List<MyObject>();
obj_list.Add(new MyObject());

2、部分类(partial)
namespace xxx
{
    public partial class Class1
    {
        private string _s1;
        public string S1
        {
            get { return _s1; }
            set { _s1 = value; }
        }
    }
   
    //或在另一个文件中
    public partial class Class1
    {
        public string GetString()
        {
            string s = this.S1 + "aaa";
            return s;
        }
    }
}


3、静态类
public static class MyStaticObject
{
    private static string _s;
    static MyStaticObject()
    {
        _s = "Hello";
    }
    public static string Mothed1()
    {
        return _s + ",world.";
    }
}

4、属性访问器可访问性
public class Class2
{
    private string _str;
    public string Str
    {
        get { return _str; }
        protected set { _str = value; }
    }
}

5、可空类型
int? aa = null;
aa = 23;
if (aa.HasValue)
{
    int bb = aa.Value;
}
6、匿名方法
  class SomeClass //在C#1.0中
  {
      delegate void SomeDelegate();
      public void InvokeMethod()
      {
          SomeDelegate del = new SomeDelegate(SomeMethod);
          del();
      }
      void SomeMethod()
      {
          MessageBox.Show("Hello");
      }
  }

  class SomeClass2
  {
      public delegate void SomeDelegate();
      public void InvokeMothed()
      {
          SomeDelegate del = delegate {
              MessageBox.Show("Hello");
          };
          del();
      }
  }
7、名称空间别名限定符
global::

 

二、C# 3.0/3.5 新特性:
1、LinQ(语言集成查询)
以前,查询XML文件使用XPath,数据库刚用SQL,LinQ搜索任何IEnumerable<T>数据源.
在ORM解决方案中,LINQ对象用途很大.
示例:
List<Customer> customers = new List<Customer>();
IEnumerable<Customer> query_result = from c in customers
                                     where c.Money > 100
                                     orderby c.Name
                                     select c;
Linq 包括 Linq to SQL, Linq to Objects, Linq to XML 和 ADO.NET Entity Framework 等几个部分
                                    
2、Lambda表达式,更激动人心的,是一种匿名函数结构,它可以方便的实现委托、查询综合和扩展方法的 delegate 类型参数的初始化定义.
示例:原来的:
delegate void Func(int x);
void Add(int x){x++;}
Func f=new Func(Add);
f(1);
可简化为:
Func f=(x)=>{x++;};
或:
Func f=(int x )=>{x++;};


3、隐式类型本地变量,var关键字(类型脚本语言中的隐式声明变量,主要针对LinQ设计)
var num=0;
var nums[]={1,2,3,4,5};
var num='a';
var list=new List<int>();
foreach(var i in nums){
  num+=i;
}

4、扩展方法,extension(允许您扩充任何类,甚至是标记为封装的类,对于扩展的方法必须在静态类里来扩展)
示例,在string上实现Count()方法:
using System.Runtime.CompilerService;
public class Extensions{
  [Extension()]
  public int Count(this string source){
    int count = 0;
    foreach (var item in source){
      count++;
    }
    return count;
  }
}
//使用:
string s="Hello,world!";
int i=s.Count();


5、对象和集合初始值设定项,初始化的简化,写实体类方便了
public class Person{
  public string Name{get;set;}  //自动实现属性
  public int Age{get;set;}
}
var person1=new Person{Name="tang",Age=21};  //...
var persons=new List<Person>{    //集合初始化器
  new Person{Name="TEW",Age=21},
  new Person{Name="RSA",Age=18}
};

6、从 C# 不同版本看使用代理的不同代码(C# 3.0/3.5 宽松委托)
C# 1.0/1.1:
public class MyForm10
{
    System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
    System.Windows.Forms.TextBox tb = new System.Windows.Forms.TextBox();
    System.Windows.Forms.Button bt = new System.Windows.Forms.Button();
    public MyForm10()
    {
        bt.Click += new EventHandler(bt_Click);
    }

    void bt_Click(object sender, EventArgs e)
    {
        lb.Items.Add(tb.Text);
    }
}

C# 2.0:
public class MyForm20
{
    System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
    System.Windows.Forms.TextBox tb = new System.Windows.Forms.TextBox();
    System.Windows.Forms.Button bt = new System.Windows.Forms.Button();
    public MyForm20()
    {
        bt.Click += delegate {
            lb.Items.Add(tb.Text);
        };
    }
}

C# 3.0/3.5(宽松委托):
public class MyForm30
{
    System.Windows.Forms.ListBox lb = new System.Windows.Forms.ListBox();
    System.Windows.Forms.TextBox tb = new System.Windows.Forms.TextBox();
    System.Windows.Forms.Button bt = new System.Windows.Forms.Button();
    public MyForm30()
    {
        //bt.Click =>{lb.Items.Add(tb.Text);}; //还没搞懂
    }
}

7、匿名类型
var aa1=new{ID=12,Name="Tang",IsHello=false};
Console.Write(aa1.Name);
var aa2=new{ID=25,Name="Sing",IsHello=true}
aa1=aa2;

8、隐式类型化数组

9、分部方法(partial分部类的分部方法,必须是void返回类型)
// 文件 1.cs
public partial class A{
    void B(); //声明
}

// 文件 2.cs
public partial class A{
    void B { Console.WriteLine("B invoked."); } //实现
}

原创粉丝点击