Geeks With Blogs
Rajesh Pillai My experiments with asp.net mvc, jquery, wpf, silverlight, sharepoint, tdd and design patterns.

c# 4.0 Part 1


The following are some of the new features and few interesting things you can do with c# 4.0.

- Named and Optional Parameters
- ExpandoObject
- Interface with Python (and example)

Program 1

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"

   }
}



Program 2

- Dynamic Invocation


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);
     
   }
}



Program 3


// 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(() =>
       {
           Console.WriteLine("{0} {1}", dp.FirstName, dp.LastNmae);
       });
     
     
       dp.Print();
       dp.PrintPretty();
         
   }
}



Program 4

Reference with other language IronPyton 2.6 CTP for .NET 4.0 Beta 1


using IronPython.Hosting;
class Program
{
   static void Main (string[] args)
   {
       var py = Python.CreateEngine();  // may be used for end user scripting customization
     
       // UseFile->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
     
   }
}



// some python code
Sample.py (add two numbers)
------------------------------


def add(x,y):
   return x + y

 

Posted on Saturday, November 28, 2009 4:05 PM .NET 4 , c# 4.0 | Back to top


Comments on this post: c# 4.0 Part 1

# re: c# 4.0 Part 1
Requesting Gravatar...
Could you put some more light on program 4?
Left by Rajesh Patel on Apr 14, 2010 12:46 AM

Your comment:
 (will show your gravatar)


Copyright © Rajesh Pillai | Powered by: GeeksWithBlogs.net