C# 3.0 新增了一些激动人心的特性。本文通过一个控制台应用程序来展示 C# 3.0 相对于C#2.0 的所有的新特性。
完整示例:点击下载
using System;
using System.Collections.Generic;
using System.Linq;
namespace CS30NEW
{
///<summary>
///
/// C#3.0新特性
///
/// Blog:http://xianfen.net
///
/// Author:飘遥(Zxjay)
///
/// Date:2007-12-22
///
/// Email:tda7264@163.com
///
///</summary>
class Program
{
static void Main(string[] args)
{
/*
* C#3.0新特性之自动属性(Automatic Properties)
* 自动属性不能实现自定义类型及数据完整性检查
* 保留了属性的其他优点
*/
Console.WriteLine("C#3.0新特性之自动属性(Automatic Properties)");
Test1 t1 = new Test1();
t1.String1 = "http://xianfen.net";
t1.Int1 = 0;
Console.WriteLine(t1.String1 + "\n" + t1.Int1);
/*
* C#3.0新特性之对象初始化器(Object Initializers)
*/
Console.WriteLine("\nC#3.0新特性之对象初始化器(Object Initializers)");
Test1 t2 = new Test1 { String1 = "http://xianfen.net", Int1 = 0 };
Console.WriteLine(t2.String1 + "\n" + t2.Int1);
/*
* C#3.0新特性之匿名类型(Anonymous Types)
* 变量初始化后改变量的类型就确定了,如果再赋值其他类型将抛出异常
*/
Console.WriteLine("\nC#3.0新特性之匿名类型(Anonymous Types)");
var t3 = new Test1 { String1 = "http://xianfen.net", Int1 = 0 };
Console.WriteLine(t3.String1 + "\n" + t3.Int1);
/*
* C#3.0新特性之集合初始化器(Collection Initializers)
*/
Console.WriteLine("\nC#3.0新特性之集合初始化器(Collection Initializers)");
List<Test1> l = new List<Test1>
{
new Test1{ String1 = "http://xianfen.net", Int1 = 0 },
new Test1{ String1 = "http://www.xianfen.net", Int1 = 1 },
new Test1{ String1 = "http://zxjay.cn", Int1 = 2 }
};
Console.WriteLine(l.Count);
Console.WriteLine(l[0].String1 + "\n" + l[0].Int1);
/*
* C#3.0新特性之Lambda表达式(Lambda Expressions)
*/
Console.WriteLine("\nC#3.0新特性之Lambda表达式(Lambda Expressions)");
IEnumerable<Test1> e = l.Where(tst => tst.Int1 > 0);
foreach (Test1 t in e)
{
Console.WriteLine(t.String1 + "\n" + t.Int1);
}
/*
* C#3.0新特性之查询语法(Query Syntax)
* 语法跟SQL类似,select 放在最后
*/
Console.WriteLine("\nC#3.0新特性之查询语法(Query Syntax)");
IEnumerable<Test1> e1 = from t in l where t.Int1 < 2 orderby t.String1 descending select t;
foreach (Test1 t in e1)
{
Console.WriteLine(t.String1 + "\n" + t.Int1);
}
/*
* C#3.0新特性之扩展方法(Extension Methods)
* 有点类似于JavaScript的prototype
* 实现扩展方法的类及扩展的方法都必须是静态的,待扩展的类作为参数,前面加this关键字
* 扩展方法容易造成程序混乱,给维护和升级造成麻烦,如果不是非常必要,尽量不要使用
*/
Console.WriteLine("\nC#3.0新特性之扩展方法(Extension Methods)");
var t4 = new Test1 { String1 = "http://xianfen.net", Int1 = 0 };
Console.WriteLine(t4.AddDefaultPage());
Console.ReadKey();
}
}
public class Test1
{
public string String1 { set; get; }
public int Int1 { set; get; }
}
public static class Test2
{
/*
* C#3.0新特性之扩展方法(Extension Methods)
* 有点类似于JavaScript的prototype
* 实现扩展方法的类及扩展的方法都必须是静态的,待扩展的类作为参数,前面加this关键字
*/
public static string AddDefaultPage(this Test1 t)
{
return t.String1 + "/Default.aspx";
}
}
}
using System.Collections.Generic;
using System.Linq;
namespace CS30NEW
{
///<summary>
///
/// C#3.0新特性
///
/// Blog:http://xianfen.net
///
/// Author:飘遥(Zxjay)
///
/// Date:2007-12-22
///
/// Email:tda7264@163.com
///
///</summary>
class Program
{
static void Main(string[] args)
{
/*
* C#3.0新特性之自动属性(Automatic Properties)
* 自动属性不能实现自定义类型及数据完整性检查
* 保留了属性的其他优点
*/
Console.WriteLine("C#3.0新特性之自动属性(Automatic Properties)");
Test1 t1 = new Test1();
t1.String1 = "http://xianfen.net";
t1.Int1 = 0;
Console.WriteLine(t1.String1 + "\n" + t1.Int1);
/*
* C#3.0新特性之对象初始化器(Object Initializers)
*/
Console.WriteLine("\nC#3.0新特性之对象初始化器(Object Initializers)");
Test1 t2 = new Test1 { String1 = "http://xianfen.net", Int1 = 0 };
Console.WriteLine(t2.String1 + "\n" + t2.Int1);
/*
* C#3.0新特性之匿名类型(Anonymous Types)
* 变量初始化后改变量的类型就确定了,如果再赋值其他类型将抛出异常
*/
Console.WriteLine("\nC#3.0新特性之匿名类型(Anonymous Types)");
var t3 = new Test1 { String1 = "http://xianfen.net", Int1 = 0 };
Console.WriteLine(t3.String1 + "\n" + t3.Int1);
/*
* C#3.0新特性之集合初始化器(Collection Initializers)
*/
Console.WriteLine("\nC#3.0新特性之集合初始化器(Collection Initializers)");
List<Test1> l = new List<Test1>
{
new Test1{ String1 = "http://xianfen.net", Int1 = 0 },
new Test1{ String1 = "http://www.xianfen.net", Int1 = 1 },
new Test1{ String1 = "http://zxjay.cn", Int1 = 2 }
};
Console.WriteLine(l.Count);
Console.WriteLine(l[0].String1 + "\n" + l[0].Int1);
/*
* C#3.0新特性之Lambda表达式(Lambda Expressions)
*/
Console.WriteLine("\nC#3.0新特性之Lambda表达式(Lambda Expressions)");
IEnumerable<Test1> e = l.Where(tst => tst.Int1 > 0);
foreach (Test1 t in e)
{
Console.WriteLine(t.String1 + "\n" + t.Int1);
}
/*
* C#3.0新特性之查询语法(Query Syntax)
* 语法跟SQL类似,select 放在最后
*/
Console.WriteLine("\nC#3.0新特性之查询语法(Query Syntax)");
IEnumerable<Test1> e1 = from t in l where t.Int1 < 2 orderby t.String1 descending select t;
foreach (Test1 t in e1)
{
Console.WriteLine(t.String1 + "\n" + t.Int1);
}
/*
* C#3.0新特性之扩展方法(Extension Methods)
* 有点类似于JavaScript的prototype
* 实现扩展方法的类及扩展的方法都必须是静态的,待扩展的类作为参数,前面加this关键字
* 扩展方法容易造成程序混乱,给维护和升级造成麻烦,如果不是非常必要,尽量不要使用
*/
Console.WriteLine("\nC#3.0新特性之扩展方法(Extension Methods)");
var t4 = new Test1 { String1 = "http://xianfen.net", Int1 = 0 };
Console.WriteLine(t4.AddDefaultPage());
Console.ReadKey();
}
}
public class Test1
{
public string String1 { set; get; }
public int Int1 { set; get; }
}
public static class Test2
{
/*
* C#3.0新特性之扩展方法(Extension Methods)
* 有点类似于JavaScript的prototype
* 实现扩展方法的类及扩展的方法都必须是静态的,待扩展的类作为参数,前面加this关键字
*/
public static string AddDefaultPage(this Test1 t)
{
return t.String1 + "/Default.aspx";
}
}
}
完整示例:点击下载