一、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."); } //实现
}

从C# 2.0新特性到C# 3.5新特性的更多相关文章

  1. Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth

    Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1 Python 3_x 新特性1 python3.4新特性1 python3.5新特性1 值得关注的新特性1 ...

  2. Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能

    Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能 1. MySQL  5.6    5 大新特性1 1.1. 优化器的改进1 1.2. InnoDB 改进1 1.3. 使用 ...

  3. Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能

    Atitit.mysql 5.0 5.5  5.6 5.7  新特性 新功能 1. MySQL  5.6    5 大新特性1 1.1. 优化器的改进1 1.2. InnoDB 改进1 1.3. 使用 ...

  4. PHP 7.4.0发布!一起看看有哪些新特性

    PHP 7.4.0 发布了,此版本标志着 PHP 7 系列的第四次特性更新. 看了英文手册后,发现其进行了许多改进,并带来了一些新特性,现在将这些新特性您: 1.Typed Properties 类型 ...

  5. 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...

  6. JDK8新特性(一) Lambda表达式及相关特性

    函数式接口 函数式接口是1.8中的新特性,他不属于新语法,更像是一种规范 面向对象接口复习 在这里先回顾一下面向对象的接口,创建接口的关键字为interface,这里创建一个日志接口: public ...

  7. Odoo 的库存管理与OpenERP之前的版本有了很大的不同,解读Odoo新的WMS模块中的新特性

    原文来自:http://shine-it.net/index.php/topic,16409.0.html 库存移动(Stock Move)新玩法Odoo的库存移动不仅仅是存货在两个“存货地点”之间的 ...

  8. SQL Server 2016新特性:列存储索引新特性

    SQL Server 2016新特性:列存储索引新特性 行存储表可以有一个可更新的列存储索引,之前非聚集的列存储索引是只读的. 非聚集的列存储索引支持筛选条件. 在内存优化表中可以有一个列存储索引,可 ...

  9. NET Core 3.0 AutoFac替换内置DI的新姿势

    原文:NET Core 3.0 AutoFac替换内置DI的新姿势 .NET Core 3.0 和 以往版本不同,替换AutoFac服务的方式有了一定的变化,在尝试着升级项目的时候出现了一些问题. 原 ...

  10. kubernetes1.4新特性:支持两种新的卷插件

    背景介绍 在Kubernetes中卷的作用在于提供给POD持久化存储,这些持久化存储可以挂载到POD中的容器上,进而给容器提供持久化存储. 从图中可以看到结构体PodSpec有个属性是Volumes, ...

随机推荐

  1. 轮子科技的.NET Core分享

    2016年8月11日 应轮子科技一众好友的邀请,在轮子科技给大家做了一个无责任的瞎聊段落,聊聊.NET的Core的一些内容. 恩,演讲者就只有我一个了,讲师是微软的 MVP 杨守斌,就是因为这个,所以 ...

  2. Windows Phone本地数据库(SQLCE):12、插入数据(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十二篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...

  3. CAD二次开发起步

    1 环境搭建(VS 2017 + CAD 2019) CAD版本一般要比VS版本晚两年以上,比如我现在使用VS2017,则CAD要用2019版,至于CAD2018能不能用没试验过,有兴趣的小伙伴可以试 ...

  4. Redis客户端集群

        1.Redis集群一般分为两类,即3.0版本后的服务端集群实现,3.0版本前的客户端集群实现,服务端集群即Redis Cluster(官方实现),采用slot槽的概念(分片,所有服务端redi ...

  5. 探讨一下Java单例设计模式

    所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在.就像是Java Web中的application,也就是提供了一个全局变量,用处相当广泛,比如保存全局数据,实现全局性的操作等. 1. ...

  6. SharePoint 压缩打包文件代码分享

    前言 最近碰到这样一个需求,用户需要批量打包下载sharepoint文档库中的文档,所以,就需要开发一个打包下载的服务. 然后,把打包的代码分享给大家,也许会有需要的人. static void Ma ...

  7. JS --- 三目运算符

    1.什么是三目运算:(布尔表达式 ? 值0:值1;) 5>3?alert('5大'):alert('3大'); 即    if(5>3){alert('5大')}else{alert('3 ...

  8. mysql查询当前系统时间

    第一种方法:select current_date: MySQL> select current_date as Systemtime;+------------+| Systemtime |+ ...

  9. 解决Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 4

    有时候我们可能需要将项目的版本降低,比如4.4降低到2.2这样的,可能会遇到类似于这样的错误 Using 1.7 requires compiling with Android 4.4 (KitKat ...

  10. 无需SherlockActionbar的SlidingMenu使用详解(一)——通过SlidingMenu设置容器并解决滑动卡顿的问题

    想必很多人都听过这个开源框架,一年前真的是风靡一时.只是它的配置较为繁琐,还需要sherlockActionbar的支持,我这里下载了最新的开源库,并且在实际用套用了AppCompat的官方库,这样就 ...