C# 2 新增语法特性
2024-08-28 01:18:26
C# 2.0 ,.NET Framework 2.0,.NET Framework 3.0,Visual Studio 2005
C#2主要添加了泛型、匿名方法,分部类型(类、结构、接口),可空类型,迭代器,属性访问控制器,方法组转换,协变和逆变,静态类、委托推断
其中最为经典的当属【泛型】
1、Generics:泛型
1 public static bool GreatTo<T>(T t,T t1) where T : IComparable
2 {
3 if (t.CompareTo(t1) > 0)
4 {
5 return true;
6 }
7 else
8 {
9 return false;
10 }
11 }
2、Anonymous methods:匿名方法
this.btnValidateCode.BeginInvoke(new MethodInvoker(() => { this.btnValidateCode.Text = "Cancel"; }));
3、Partial types:分部类型,可以将类、结构、接口等类型定义拆分到多个文件中,使用关键字partial。最常见的就是WinForm中窗体的业务部分和设计器部分
1 public partial class Form1 : Form
2 {
3
4 }
5
6
7 partial class Form1
8 {
9 /// <summary>
10 /// 必需的设计器变量。
11 /// </summary>
12 private System.ComponentModel.IContainer components = null;
13
14 /// <summary>
15 /// 清理所有正在使用的资源。
16 /// </summary>
17 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
18 protected override void Dispose(bool disposing)
19 {
20 if (disposing && (components != null))
21 {
22 components.Dispose();
23 }
24 base.Dispose(disposing);
25 }
26
27 #region Windows 窗体设计器生成的代码
28
29 /// <summary>
30 /// 设计器支持所需的方法 - 不要
31 /// 使用代码编辑器修改此方法的内容。
32 /// </summary>
33 private void InitializeComponent()
34 {
35 this.SuspendLayout();
36 //
37 // Form1
38 //
39 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
40 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
41 this.ClientSize = new System.Drawing.Size(284, 261);
42 this.Name = "Form1";
43 this.Text = "Form1";
44 this.ResumeLayout(false);
45
46 }
47
48 #endregion
49 }
4、Nullable types:可以为Null的类型,该类可以是其它值或者null
1 public int? Age;
2 public decimal? Price;
3 public bool? Flag;
5、Iterators:迭代器
6、Getter/setter separate accessibility:属性访问控制
public string UserName { get; set; }
7、Method group conversions (delegates):方法组转换,可以将声明委托代表一组方法,隐式调用
1 public delegate void GreetHandler(string name);
2
3 public void Test()
4 {
5 GreetHandler greetHander = ChineseGreet;
6 //GreetHandler greetHander = new GreetHandler( ChineseGreet); //可以不用写成这样子
7 greetHander("jim");
8 }
9 public void ChineseGreet(string name)
10 {
11 Console.WriteLine("您好" + name);
12 }
8、Co- and Contra-variance for delegates and interfaces:委托、接口的协变和逆变
9、Static classes:静态类
最新文章
- AutoCad2008 部分快捷键
- shell学习之路:shell基础大全2
- ETL工具与脚本实现之间的对比
- python 读写、创建 文件
- (Abstract Factory)抽象工厂
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.5.SportsStore一个真实的程序
- 教你ECSHOP去版权与标志(新增272版)
- Spring集成JPA提示Not an managed type
- HDU 1080
- 基于CSS+dIV的网页层,点击后隐藏或显示
- 540C: Ice Cave
- [C/CPP系列知识] 在C中使用没有声明的函数时将发生什么 What happens when a function is called before its declaration in C
- Oracle EBS-SQL (SYS-10):锁定表查询.sql
- JS编程
- IOS把图片缓存到本地的几种方法
- Lua利用cjson读写json
- 集成电路883和883b有什么区别
- FIRMWARE BUG – THE BIOS HAS CORRUPTED HW-PMU RESOURCES
- sqlserver删除临时表中的数据
- java中JVM的原理