一、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. 基于设备树的TQ2440 DMA学习(1)—— 芯片手册

    作者 彭东林pengdonglin137@163.com 平台 TQ2440内核Linux4.9 概述 一直想抽时间学习一下DMA驱动,今天就以S3C2440为例,这款芯片的DMA控制器足够简单,也比 ...

  2. C#编程(四十七)----------集合接口和类型

    原文链接: http://blog.csdn.net/shanyongxu/article/details/47005979 集合接口和类型 前面介绍了数组和Array类实现的接口.数组的大小是固定的 ...

  3. 一致性Hash算法说明

    本文章比较好的说明了一致性Hash算法的概念 Hash算法一般分为除模求余和一致性Hash1.除模求余:当新增.删除机器时会导致大量key的移动2.一致性Hash:当新增.删除机器时只会影响到附近的k ...

  4. Caused by: java.lang.IllegalArgumentException: Can not set int field reyo.sdk.enity.xxx.xxx to java.lang.Long

    由于数据库字段设置不正确引起的,不能选中 alter <table> modify <column> int unsigned; 关于unsigned int类型,可以看看它的 ...

  5. SharePoint 应用程序页匿名

    前言 最近,有朋友问开发应用程序页,都是需要先登录再访问,无法开发匿名的应用程序页. 解决方法 其实,SharePoint帮我们提供了匿名访问的应用程序页的方法,只是和普通应用程序页继承的基类不一样, ...

  6. 如何解决SSH连接Linux超时自动断开?

    最近项目开发中用到云服务器,部署了MySQL,开发团队总是反映MySQL过一会儿就断开连接了,必须手动重连才可以.反映越来越强烈,已经到了影响开发进度的高度了,必须解决! 查了资料,这个可能和SSH超 ...

  7. nvidia Compute Capability(GPU)

    GPU Compute Capability NVIDIA TITAN X 6.1 GeForce GTX 1080 6.1 GeForce GTX 1070 6.1 GeForce GTX 1060 ...

  8. Java(C#)基础差异-语法

    1.long类型 Java long类型,若赋值大于int型的最大值,或小于int型的最小值,则需要在数字后加L或者l,表示该数值为长整数,如long num=2147483650L. 举例如下: p ...

  9. go语言之进阶篇error接口应用

    1.error接口应用 示例: package main import "fmt" import "errors" func MyDiv(a, b int) ( ...

  10. knockout示例

    最近项目需要用到knockout js,有关knockout的介绍网上已经很多很多了,但是很少有比较全面的示例,于是乎我就自己做了一个小demo,已备以后查阅.knockout经常和knockout. ...