C#4.0 Dynamic Programming &Named Argument
来源:互联网 发布:网页源代码 隐藏数据 编辑:程序博客网 时间:2024/06/04 18:52
From :Code Project View original
Dynamic Programming
C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects. The type of these objects is resolved at run-time instead of at compile-time. A new keyword dynamic is introduced to declare dynamic typed object. The keyword tells the compiler that everything to do with the object, declared as dynamic, should be done dynamically at the run-time using Dynamic Language Runtime(DLR). Remember dynamic keyword is different from var keyword. When we declare an object as var, it is resolved at compile-time whereas in case of dynamic, the object type is dynamic and it's resolved at run-time. Let’s do some coding to see the advantage of Dynamic Typed Objects. :)
A year ago, I wrote a code of setting the property of an object using Reflection:
1: Assembly asmLib= Assembly.LoadFile(@"C:/temp/DemoClass/bin/Debug/DemoClass.dll"); 2: Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib"); 3: object demoClassobj= Activator.CreateInstance(demoClassType); 4: PropertyInfo pInfo= demoClassType.GetProperty("Name"); 5: pInfo.SetValue(demoClassobj, "Adil", null);
Notice lines 3-5 create instance of ‘demoClassType
’ and set property ‘Name
’ to ‘Adil
’. Now with C# 4.0, line # 3-5 can be written as simple as:
dynamic dynamicDemoClassObj = Activator.CreateInstance(demoClassType);dynamicDemoClassObj.Name = "Adil";
Simple, isn't it? Let’s see a slide from Anders Hejlsberg’s session at PDC 2008:
From the above slide, you can call method(s) such as x.ToString()
, y.ToLower()
, z.Add(1)
, etc. and it will work smoothly. :)
This feature is great and provides much flexibility for developers. Of course there are pros and cons of dynamic programming as well but where C# is going is something like having features of both static languages and dynamic languages.
Optional Parameters
The second feature is Optional Parameters. Let’s say I have a class Employee
and I provide few overloads of the constructor to enable making certain parameters as optional as follows:
public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string Qualification { get; set; } public string MiddleName { get; set; } public Employee(string firstName, string lastName) { FirstName= firstName; LastName= lastName; Qualification= "N/A"; MiddleName= string.Empty; } public Employee(string firstName, string lastName, string qualification) { FirstName= firstName; LastName= lastName; Qualification= qualification; MiddleName= string.Empty; } public Employee(string firstName, string lastName, string qualification, string middleName) { FirstName= firstName; LastName= lastName; Qualification= qualification; MiddleName= middleName } }
With C# 4.0, you need to create just one constructor for that as follows:
public Employee(string firstName, string lastName, string qualification = "N/A", string middleName = ""){ FirstName= firstName; LastName= lastName; Qualification= qualification; MiddleName = middleName;}
As simple as that :) and you can easily call that as follows:
Employee(“Adil”,”Mughal”);
This feature was available in some other languages but was for some reason not provided in C# yet, but now it’s available. This feature has a good impact in COM interop which allows developers to skip missing parameters which we will hopefully see in later post(s). Finally, the compiler will always fill the optional parameters by their default given values, if you do not provide them. For instance, in our current case, it will be:
Employee emp= newoyee("Adil", "Mughal");
Simple but useful feature!
Named Argument
So, we discussed an example of Employee
class in which we passed some optional parameters in the constructor:
public Employee(string firstName, string lastName, string qualification = "N/A", string middleName = "")
And I can simply call that as shown below:
Employee emp= new Employee("Adil", "Mughal");
A question can be raised "Is there any way that we can skip qualification, i.e. third parameter and give the last parameter of middleName
?"
The answer of this question is "Yes absolutely, we can and that feature is called Named Argument in C# 4.0." We can simply do this like:
Employee emp = new Employee("Adil", "Mughal", middleName: "Ahmed");
Good enough to answer the query. :). Now let’s make some changes with the Employee
constructor and make lastName
optional as well:
public Employee(string firstName, string lastName = "", string qualification = "N/A", string middleName = "")
Now I can instantiate object of Employee
in quite simple and flexible ways:
Employee("Adil", qualification:"BS");Employee("ABC", lastName: "EFG", qualification: "BS");Employee("XYZ", middleName: "MNO");
Conclusion
These upcoming features are really cool as they will enable C# developers to be more productive with the help of dynamic programming and optional parameters though some of the features are not new in the programming languages’ world.
- C#4.0 Dynamic Programming &Named Argument
- AMP
- AMP
- amp
- (debian 4.0 r1) mrtg && snmp
- C#中的装箱&&拆箱
- &和&&;|和||的区别C#
- C#基础学习日志===>命名空间的使用<===
- C++ AMP: Parallel Programming in Native Code
- C#的virtual & abstract
- Spring.Net&NHibernate 学习
- TweenLite&&TweenMax系列(一)
- sockaddr && sockaddr_in
- awk&&sed脚本语言
- C#网络编程个人理解UDP&TCP
- C#语言基础(1)----(条件(&&))逻辑与&和(条件(||))逻辑或|
- 重新开张 && two pieces of fascinating code
- CsvWriter&&CsvReader
- TStringList用法(转载)
- linux内核代码架构图-清晰版
- PL/SQL常用函数
- loadruner测试sap portal
- vc访问注册表
- C#4.0 Dynamic Programming &Named Argument
- Linux 软件安装技巧
- 网络地址转换
- 我国零售业信息化面临的三大障碍
- h4
- list_entry Head_list 模拟一下,方便以后查看
- 最近一些面试题
- 关于拷贝构造函数和赋值运算符
- ABAP的字符串与十六进制的相互转换