从C# 2.0新特性到C# 3.5新特性
一、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新特性的更多相关文章
- 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 ...
- 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. 使用 ...
- 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. 使用 ...
- PHP 7.4.0发布!一起看看有哪些新特性
PHP 7.4.0 发布了,此版本标志着 PHP 7 系列的第四次特性更新. 看了英文手册后,发现其进行了许多改进,并带来了一些新特性,现在将这些新特性您: 1.Typed Properties 类型 ...
- 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性
[源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...
- JDK8新特性(一) Lambda表达式及相关特性
函数式接口 函数式接口是1.8中的新特性,他不属于新语法,更像是一种规范 面向对象接口复习 在这里先回顾一下面向对象的接口,创建接口的关键字为interface,这里创建一个日志接口: public ...
- Odoo 的库存管理与OpenERP之前的版本有了很大的不同,解读Odoo新的WMS模块中的新特性
原文来自:http://shine-it.net/index.php/topic,16409.0.html 库存移动(Stock Move)新玩法Odoo的库存移动不仅仅是存货在两个“存货地点”之间的 ...
- SQL Server 2016新特性:列存储索引新特性
SQL Server 2016新特性:列存储索引新特性 行存储表可以有一个可更新的列存储索引,之前非聚集的列存储索引是只读的. 非聚集的列存储索引支持筛选条件. 在内存优化表中可以有一个列存储索引,可 ...
- NET Core 3.0 AutoFac替换内置DI的新姿势
原文:NET Core 3.0 AutoFac替换内置DI的新姿势 .NET Core 3.0 和 以往版本不同,替换AutoFac服务的方式有了一定的变化,在尝试着升级项目的时候出现了一些问题. 原 ...
- kubernetes1.4新特性:支持两种新的卷插件
背景介绍 在Kubernetes中卷的作用在于提供给POD持久化存储,这些持久化存储可以挂载到POD中的容器上,进而给容器提供持久化存储. 从图中可以看到结构体PodSpec有个属性是Volumes, ...
随机推荐
- Unity3D实践系列09, 物理引擎与碰撞检测
在Unity3D中,一个物体通常包含一个Collider和一个Rigidbody.Collider是碰撞体,一个物体是Collider,才可以进行碰撞检测.Collider组件中的"Is T ...
- Visual Studio 2012 调试程序加载缓慢,提示正在下载符号
当在Visual Studio 2012中调试程序的时候,整个过程加载缓慢,并且有类似"正在下载符号......"的提示. 这是因为Visual Studio在调试的时候,默认从M ...
- Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)
清除Windows图标缓存的代码: procedure RebuildIconCache; .... const sr_WindowMetrics='Control Panel\Desktop\Win ...
- Android_深入解析AsyncTask
转载:特别感谢浪人的星空,有部分修改! http://blog.csdn.net/hitlion2008/article/details/7983449 1.AsyncTask的内幕 AsyncTas ...
- Android 用户登录界面
本篇博客主要给大家演示怎样一步一步地创建一个类似于下图展示的这么一个UI界面: 一.准备图片资源 记住:因为Demo其中用到的图片资源都是直接从上面图片截取的,所以图片质量上面会差一些,只是.不影响我 ...
- IOS判断用户的网络类型(2/3/4G、wifi)
直接贴代码吧,ios7之后是获取的较为准确,7以下我拿iphone5测试的是无法区分3g/2g.连iphone4都能升到7.1.4,而且目前主流的设备7以下的系统已经很少了,这个方案尽管不太完美,但影 ...
- [Web 前端] React-router4简约教程
cp from : http://react-china.org/t/react-router4/15843/1 React-router和React-router-dom的选择 很多刚使用react ...
- .NET零基础入门之02:源码控制管理器的使用
一:概述 源码控制管理器,也叫"版本控制"软件,用于存储.追踪目录(文件夹)和文件的修改历史,是软件开发者的必备工具,是专业软件公司的基础工具.它主要是协助在多人团队中控制代码,防 ...
- java常见反编译工具
1.Java反编译插件 —— Jadclipse JadClipse是Jad的Eclipse插件,是一款非常实用而且方便地Java反编译插件,我们只需将下载的插件包复制到eclipse的plugins ...
- LaTeX使用技巧
使用LaTex的方法: (1)推荐一个手写公式.自动生成LaTex的网站——Web Equation. (2)如果会LaTex,可以直接用在线LaTex编辑 (3)从mathtype转换: 首先打开文 ...