C# 4.0的一些新特性
vs2010正式版4月12日发布了,前几天我也下了一个,但这几天都没有时间好好试用一下,今天针对C#语言的新特性使用了一下,感觉还不错,有几个新特性和大家分享一下,希望我没有太火星……
一、新关键词——dynamic
在新版本的C#中,dynamic关键词是一个很重要的新特性,现在你可以创建动态对象并在运行时再决定它的类型。而且.net 4.0为CLR加入了一组为动态语言服务的运行时环境,称为DLR(Dynamic Language Runtime动态语言运行时),这使得C#这种静态类型语言可以在 .NET Framework 中开发动态语言并为与其它动态语言提供互操作性了。DLR架构见下图:
关于DLR的更详细信息,可以参考msdn:http://msdn.microsoft.com/en-us/library/dd233052(VS.100).aspx
先来看看dynamic的一个例子:
dynamic dyn = 1; Console.WriteLine(dyn.GetType()); dyn = 1.234; Console.WriteLine(dyn.GetType()); dyn = "ojlovecd"; Console.WriteLine(dyn.GetType());/*输出:System.Int32System.DoubleSystem.String*/
object obj = 10; obj = obj + 10;
object obj = 10; obj = (int)obj + 10;
object obj = 10; obj = (string)obj + 10;/*Unhandled Exception: System.InvalidCastException: Unable to cast object of type'System.Int32' to type 'System.String'.*/
dynamic dyn = 10; dyn = dyn + 10; Console.WriteLine(dyn); dyn = 10.02; dyn = dyn + 10; Console.WriteLine(dyn); dyn = "ojlovecd"; dyn = dyn + 10; Console.WriteLine(dyn);
dynamic dyn = 10; dyn = dyn + DateTime.Now; Console.WriteLine(dyn);/*Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '+' cannot be applied to operands of type 'int' and 'System.DateTime' at CallSite.Target(Closure , CallSite , Object , DateTime ) at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) at ConsoleApplication3.Program.Main(String[] args) in D:\CSharpProj\ConsoleApplication3\ConsoleApplication3\Program.cs:line 26*/
static void Main(string[] args) { TestMethod(); TestMethod(3); } static void TestMethod(int i = 10) { Console.WriteLine(i); }
static void Main(string[] args) { TestMethod("hello"); } static void TestMethod(int i = 10, string s = "ojlovecd") { Console.WriteLine("i:{0},s:{1}", i, s); }
static void Main(string[] args) { TestMethod2(s: "ojlovecd", i: 26); } static void TestMethod2(int i, string s) { Console.WriteLine("i:{0},s:{1}", i, s); }
static void Main(string[] args) { TestMethod(s: "hello"); } static void TestMethod(int i = 10, string s = "ojlovecd") { Console.WriteLine("i:{0},s:{1}", i, s); }
var excelApp = new Excel.Application();// . . .excelApp.get_Range("A1", "B4").AutoFormat( Excel.XlRangeAutoFormat.xlRangeAutoFormatTable3, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excelApp.Range["A1", "B3"].AutoFormat( Excel.XlRangeAutoFormat.xlRangeAutoFormatClassic2);
}
static void TestMethod(int i = 10, string s = "ojlovecd")
{
Console.WriteLine("i:{0},s:{1}", i, s);
}
ps:msdn我还没看。。。
[解决办法]
学习
终于有默认参数了
[解决办法]
没想明白
三、命名参数
当类的构造函数或者方法的重载有很多个的时候
而且参数类型和名称都一样,
那用这个 命名参数
编译器怎么知道该调用哪个呢?
[解决办法]
第三点 参数命名
TestMethod(s: "hello");只要调入一个参数
static void TestMethod(int i = 10, string s = "ojlovecd")
{
Console.WriteLine("i:{0},s:{1}", i, s);
}
这里却有2个参数,怎么调入呢。
这个与它传入参数的顺序可以随意有什么关系呢
[解决办法]
1.这里与传入的参数顺序没应该没关系吧,这就是4.0特性;
2.TestMethod(s: "hello");表示第一个参数没传入,是因为默认定义了int i=10
3.TestMethod(s: "hello");中的s与static void TestMethod(int i = 10, string s = "ojlovecd")中string s="";是对应的;
[解决办法]