<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>c# 4.0</title>
        <link>http://geekswithblogs.net/rajeshpillai/category/10986.aspx</link>
        <description>c# 4.0</description>
        <language>en-US</language>
        <copyright>Rajesh Pillai</copyright>
        <managingEditor>thinkrajesh@yahoo.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>c# 4.0 Part 1</title>
            <link>http://geekswithblogs.net/rajeshpillai/archive/2009/11/28/csharp41.aspx</link>
            <description>&lt;h1&gt;c# 4.0 Part 1&lt;/h1&gt;
&lt;p&gt;&lt;br /&gt;
The following are some of the new features and few interesting things you can do with c# 4.0.&lt;br /&gt;
&lt;br /&gt;
- Named and Optional Parameters&lt;br /&gt;
- ExpandoObject&lt;br /&gt;
- Interface with Python (and example)&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-style: italic;"&gt;Program 1&lt;br /&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;
class Program
{
   // Optional parameters should be at the end
   static void PrintMessage(string name = "World", string greeting = "Hello")
   {
       Console.WriteLine("{0} {1}", greeting, name);
   } 
   static void Main (string[] args)
   {
       PrintMessage("World", "Hello");  // prints "Hello World"
       PrintMessage(greeting:"Goodbye");  // prints "Goodbye World"

   }
}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-style: italic;"&gt;Program 2&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
- Dynamic Invocation&lt;/p&gt;
&lt;pre&gt;

class Person
{
   public string FirstName {get;set;}
   public string LastName {get;set;}
}

class Program
{
   static void Main (string[] args)
   {
       var person = new Person();
       person.FirstName = "Rajesh";
       person.LastName = "Pillai";
     
       object o = person;
       // o.GetType().InvokeMember(....);   older method of late binding
     
       // hold ref. to almost anything, but more extensible (like  interface with python or ruby
       dynamic dp = person; 
     
       // The below code compiles "even though FullName is not declared".  You get runtime exception
       Console.WriteLine(dp.FullName);
     
   }
}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-style: italic;"&gt;Program 3&lt;/span&gt;&lt;/p&gt;
&lt;pre&gt;

// Build Person object at runtime
using System.Dynamic;
class Program
{
   static void Main (string[] args)
   {
       // ExpandoObject :  similar to javascript object works (add dynamic prop and funcs
       dynamic dp = new ExpandoObject();
     
       // Dynamically create "FirstName"
       dp.FirstName = "Rajesh";
       dp.LastName = "Pillai";
     
       Console.WriteLine("{0} {1}", dp.FirstName, dp.LastName);
     
       // Add methods  (methods need to be delegate)
       dp.Print = new Action(delegate ()
       {
           Console.WriteLine("{0} {1}", dp.FirstName, dp.LastNmae);
       });
     
       // OR use lambda syntax
       dp.PrintPretty = new Action(() =&amp;gt;
       {
           Console.WriteLine("{0} {1}", dp.FirstName, dp.LastNmae);
       });
     
     
       dp.Print();
       dp.PrintPretty();
         
   }
}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-style: italic;"&gt;Program 4&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Reference with other language IronPyton 2.6 CTP for .NET 4.0 Beta 1&lt;/p&gt;
&lt;pre&gt;

using IronPython.Hosting;
class Program
{
   static void Main (string[] args)
   {
       var py = Python.CreateEngine();  // may be used for end user scripting customization
     
       // UseFile-&amp;gt;ScriptScope is similar to ExpandoObject
       dynamic sample = py.Runtime.UseFile("Sample.py");  // refer the python file
       dynamic sum =  sample.add(5, 6);
     
       Console.WriteLine(sum);
       Console.WriteLine(sum.GetType());  // System.Int32
 

       dynamic sum1 =  sample.add(5.6, 6.7);
     
       Console.WriteLine(sum1);
       Console.WriteLine(sum1.GetType());  // System.Double
     
   }
}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
&lt;br /&gt;
// some python code&lt;br /&gt;
Sample.py  (add two numbers)&lt;br /&gt;
------------------------------&lt;/p&gt;
&lt;pre&gt;

def add(x,y):
   return x + y&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt; &lt;img src="http://geekswithblogs.net/rajeshpillai/aggbug/136593.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rajesh Pillai</dc:creator>
            <guid>http://geekswithblogs.net/rajeshpillai/archive/2009/11/28/csharp41.aspx</guid>
            <pubDate>Sat, 28 Nov 2009 10:35:40 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/rajeshpillai/comments/136593.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/rajeshpillai/archive/2009/11/28/csharp41.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/rajeshpillai/comments/commentRss/136593.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/rajeshpillai/services/trackbacks/136593.aspx</trackback:ping>
        </item>
    </channel>
</rss>
