C# 3.0 的特性 总结
2011-07-29 10:49
411 查看
C# 作为当前版,发布于2007年10月17日,是.NET Framework 3.5 的一部分, 它的新特性灵感来自于函数式编程语言,如:Haskell 和 ML,并广泛地引入了Language Integrated Query (LINQ) 模式到通用语言运行库中e.[4]
Linq语言集成查询(英语:Language Integrated Query,缩写:LINQ):[5] "from, where, select" context-sensitive keywords allowing queries across SQL, XML, collections, and more. These are treated as keywords in the LINQ context, but their addition won't break existing variables named from, where, or select.
类型初始化器Customer c = new Customer();
c.Name = "James";
可写作:
Customer c = new Customer { Name="James" };
集合初始化器MyList list = new MyList();
list.Add(1);
list.Add(2);
可写作
MyList list = new MyList { 1, 2 };
假设 MyList 实现了 System.Collections.IEnumerable 且有一个Add 方法method[6]
匿名类型var x = new { Name="James" };
局部变量类型推断局部变量 类型推断:
var x = new Dictionary<string, List<float>>();
等同于
Dictionary<string, List<float>> x = new Dictionary<string, List<float>>();
它只是一个语法糖, 这个特性被匿名类型声明时所需要
Lambda表达式
Lambda表达式指:
listOfFoo.Where(
delegate(Foo x)
{
return x.Size > 10;
}
)
可写作
listOfFoo.Where(x => x.Size > 10);
编译器翻译Lambda表达式为强类型委托或强类型表达式树.
自动化属性编译器将自动生成私有变量和适当的getter(get访问器)和setter(set访问器),如:
public string Name
{
get;
private set;
}
扩展方法
扩展方法指,一个静态类包含this关键字作为方法的第一参数时,这个方法将被添加到该this的类型中:
public static class IntExtensions
{
public static void PrintPlusOne(this int x)
{
Console.WriteLine(x + 1);
}
}
int foo = 0;
foo.PrintPlusOne();
分部方法Allow codegenerators to generate method declarations as extension points that are only included in the source code compilation if someone actually implements it in another portion of a partial class.[7]
1.分部方法 (Partial methods) 必须定义在分部类 (partial classes) 中
2.定义分部方法 需要用 partial 做修饰符
3.分部方法不一定总是有执行内容的,也就是说定义的方法 可以一句操作语句都没有
4.分部方法返回值必须是void
5.分部方法可以是静态 (static) 方法
6.分部方法可以包含参数,参数可以包含以下修饰词:this,ref,params
7.分部方法必须是私有 (private) 方法
例子:
partial class C
{
static partial void M(int i); // defining declaration
}
partial class C
{
static partial void M(int i)
{
dosomething();
}
}
很多人对c#的概念和特性还不是很了解,这里做简单说明,欢迎补充!
Linq语言集成查询(英语:Language Integrated Query,缩写:LINQ):[5] "from, where, select" context-sensitive keywords allowing queries across SQL, XML, collections, and more. These are treated as keywords in the LINQ context, but their addition won't break existing variables named from, where, or select.
类型初始化器Customer c = new Customer();
c.Name = "James";
可写作:
Customer c = new Customer { Name="James" };
集合初始化器MyList list = new MyList();
list.Add(1);
list.Add(2);
可写作
MyList list = new MyList { 1, 2 };
假设 MyList 实现了 System.Collections.IEnumerable 且有一个Add 方法method[6]
匿名类型var x = new { Name="James" };
局部变量类型推断局部变量 类型推断:
var x = new Dictionary<string, List<float>>();
等同于
Dictionary<string, List<float>> x = new Dictionary<string, List<float>>();
它只是一个语法糖, 这个特性被匿名类型声明时所需要
Lambda表达式
Lambda表达式指:
listOfFoo.Where(
delegate(Foo x)
{
return x.Size > 10;
}
)
可写作
listOfFoo.Where(x => x.Size > 10);
编译器翻译Lambda表达式为强类型委托或强类型表达式树.
自动化属性编译器将自动生成私有变量和适当的getter(get访问器)和setter(set访问器),如:
public string Name
{
get;
private set;
}
扩展方法
扩展方法指,一个静态类包含this关键字作为方法的第一参数时,这个方法将被添加到该this的类型中:
public static class IntExtensions
{
public static void PrintPlusOne(this int x)
{
Console.WriteLine(x + 1);
}
}
int foo = 0;
foo.PrintPlusOne();
分部方法Allow codegenerators to generate method declarations as extension points that are only included in the source code compilation if someone actually implements it in another portion of a partial class.[7]
1.分部方法 (Partial methods) 必须定义在分部类 (partial classes) 中
2.定义分部方法 需要用 partial 做修饰符
3.分部方法不一定总是有执行内容的,也就是说定义的方法 可以一句操作语句都没有
4.分部方法返回值必须是void
5.分部方法可以是静态 (static) 方法
6.分部方法可以包含参数,参数可以包含以下修饰词:this,ref,params
7.分部方法必须是私有 (private) 方法
例子:
partial class C
{
static partial void M(int i); // defining declaration
}
partial class C
{
static partial void M(int i)
{
dosomething();
}
}
很多人对c#的概念和特性还不是很了解,这里做简单说明,欢迎补充!
相关文章推荐
- C# 3.0 之新特性总结
- Atitit. c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0 attilax总结 1. 版本历史 1 1.1. C# 1.0-纯粹的面向对象 2 1.2. C# 2.0
- C# 3.0 之新特性总结
- C# 2.0&3.0新特性总结
- C# 3.0以后新特性总结(转)
- C# 3.0 之新特性总结
- C# 2.0&3.0新特性总结
- C# 3.0 之新特性总结
- Atitit.c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结
- 3、步步为营VS 2008 + .NET 3.5(3) - C# 3.0新特性之Automatic Properties(自动属性)、Object Initializers(对象初始化器)、Collection Initializers(集合初始化器)和Extension Methods(扩展方法)
- c# 3.0 语言的新特性(四)
- 委托是什么?匿名方法是什么?在C# 3.0中,Lambda表达式是什么?扩展方法是什么?LINQ是什么?您觉得C# 3.0中还有哪些重要的特性,它们带来了什么优势?BCL中哪些类库和这些特性有关?您平时最常用哪些
- 步步为营VS 2008 + .NET 3.5(4) - C# 3.0新特性之LambdaExpressions(Lambda表达式)
- C# 3.5 新特性的总结(一)
- C# 3.0语言规范 (C# 3.0新特性)
- C# 3.0特性之Lambda表达式
- [笔记] C# 3.0 新特性[1]-implicitly typed local variables
- C# 3.0新特性初步研究 Part4:使用集合类型初始化器
- C# 3.0语言新特性(语言规范):8 表达式树
- Servlet 3.0新特性总结