再用2008一段时间了,但是C#3.0的新特性却一直没有学,今天边学边练,体验新特性给我能带来什么便捷。
1.隐含类型局部变量
关键字var 从右边自动识别对象类型,是强类型,且仅限于局部变量
var a = "abc";
var b = new []{1,2,3,5};
var c = null; 不是类型,不能用
var d; 不可以
比如:foreach(var c in list<string>)
{...}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DemosTest;
namespace DemosTest
{
//包含2个字段的类
public class point
{
int x, y;
public point()
{
Console.WriteLine("使用前提,公有无参构造器!");
}
public int Y
{
get { return y; }
set { y = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
public int X
{
get { return x; }
set { x = value; }
}
public void read()
{
Console.WriteLine(string.Format("X={0},Y={1}", x, y));
}
}
![](/Images/OutliningIndicators/InBlock.gif)
static class Program
{
//扩展方法
static void Foo(this string a)
{
Console.WriteLine(string.Format("this is {0}", a));
}
![](/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
{
var a = 10; //int
var b = "Hello World"; //string
![](/Images/OutliningIndicators/InBlock.gif)
var c = new List<string> { "a","b","c","d"}; //List<T>
var d = new point { X = 1, Y = 0 }; //对象构造器
![](/Images/OutliningIndicators/InBlock.gif)
var e_1 = new { Name = "张三", age = 18 }; //匿名对象,字段read only
var e_2 = new { Name = "李四", age = 20 }; //对象字段类型同样可以自动识别
![](/Images/OutliningIndicators/InBlock.gif)
b.Foo(); //调用扩展方法
![](/Images/OutliningIndicators/InBlock.gif)
foreach(var temp in c) //打印集合成员
Console.WriteLine(temp);
![](/Images/OutliningIndicators/InBlock.gif)
d.read(); //打印对象字段
e_1 = e_2; //只有在结构相同的匿名类型时,才能互相赋值
Console.WriteLine(e_1); //匿名对象的打印结果,什么对象都不是
Console.ReadKey();
}
学完这些,感觉在新版C#3.0 微软给我的感觉是越来越灵活,某些程度上感觉方便些。
比如不需要通过继承或者改写类来扩展类的方法,匿名对象在局部使用时,比较方便,不需要构造太多的东西。
但是同时可能会造成滥用后的混乱,和不易维护。
1.隐含类型局部变量
关键字var 从右边自动识别对象类型,是强类型,且仅限于局部变量
var a = "abc";
var b = new []{1,2,3,5};
var c = null; 不是类型,不能用
var d; 不可以
比如:foreach(var c in list<string>)
{...}
2.扩展方法
不改变现有类型的实例方法的情况下,扩展类的方法。Extention
static class Extention{static void Foo(this string s){...}}
给我的感觉就是对象调用全局方法。。
不需要通过继承方式扩展方法
缺点就是不能override
当出现方法重名时的优先级问题:
当前实例方法
最近的NameSpace的Extention Method
较远的NameSpace的Extention Method
3.对象与集合初始化器
public class point
{
int x, y;
public int Y
{get { return y; }set { y = value; }}
public int X
{get { return x; }set { x = value; }}
}
对象初始化
var e = new point { X = 1, Y = 0 };
集合初始化,不需通过ADD方法,以前比如构造个List,都要一个个现在就不用了
var b = new List<string> { "a","b","c","d"};
4.匿名类型
var e_1 = new { Name = "张三", age = 18 };
以下是我自己边学,练手用的测试代码
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
学完这些,感觉在新版C#3.0 微软给我的感觉是越来越灵活,某些程度上感觉方便些。
比如不需要通过继承或者改写类来扩展类的方法,匿名对象在局部使用时,比较方便,不需要构造太多的东西。
但是同时可能会造成滥用后的混乱,和不易维护。