一、C# 2.0 新特性:

1、泛型
List<MyObject> obj_list=new List();
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数据源.
在ORM解决方案中,LINQ对象用途很大.
示例:
List customers = new List();
IEnumerable 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();
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{ //集合初始化器
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."); } //实现
}

最新文章

  1. 新版C#编译器关于函数闭包的一处更改
  2. Java 2D API - 1. 基本概念
  3. 使用HTML5新支持的搭建WebRtc环境来作为视频通讯
  4. AC日记——产生数 codevs 1009 (弗洛伊德)(组合数学)
  5. Control Flow in Async Programs
  6. 菜鸟系列之C/C++经典试题(七)
  7. /bin/bash^M: 坏的解释器: 没有那个文件或目录
  8. hadoop得知;block数据块;mapreduce实现样例;UnsupportedClassVersionError变态;该项目的源代码相关联
  9. iOS 之 assign、retain、copy、nonatomic
  10. 第 18 章 高可用设计之 MySQL 监控
  11. Tomcat在windows系统中的防火墙设置
  12. Java 单元测试 JUnit4 快速入门
  13. The Mathematics of the Rubik’s Cube
  14. 【elasticsearch】关于elasticSearch的基础概念了解【转载】
  15. 动态SQL中 实现条件参数 varchar类型的参数名称 以及模糊查询实现
  16. 用CSS3做3D动画的那些事
  17. [Node.js] Level 2 new. Event
  18. linux高性能服务器编程
  19. nyoj115——裸dijksta(点之间最短路)
  20. poj1474 Video Surveillance

热门文章

  1. Java Swing 使用非本地字体
  2. Swift - 用CATransform3DMakeRotation实现翻页效果
  3. Spring Boot中使用JavaMailSender发送邮件
  4. ios之归档demo
  5. Android性能优化工具之Systrace
  6. caffe中的学习率的衰减机制
  7. dubbo源码解析-spi(二)
  8. @Dubbo概述
  9. WHY数学图形显示工具
  10. win10 下 protobuf 与 qt