Friday, 24 August 2012

Evolution of C# - Part I

Evolution of C#  - Part I

A quick reference about the evolution of c# , this blog will give an insight in C# 1.0,2.0,3.5,4.0.
The below image will give a overview sentence about the main features about the C# versions.

Evolution of C#

C# 2.0


A delegate is a type-safe object that can point to another method (or possibly multiple methods) in the application, which can be invoked at later time.


using System; 

delegate string ConvertMethod(string inString); 

public class DelegateExample 

            public static void Main() 
                    // Instantiate delegate to reference UppercaseString method 
                    ConvertMethod  convertMeth = UppercaseString; 
                    string name = "Dakota";
                    // Use delegate instance to call UppercaseString method 

             private static string UppercaseString(string inputString) 

                    return inputString.ToUpper(); 

C# 2.0 Anonymous Delegates

Before C# 2.0, the only way to use delegates was to use named methods. In some cases, this results in forced creation of classes only for using with delegates. In some cases, these classes and methods are never even invoked directly.

C# 2.0 offers an elegant solution for these methods described above. Anonymous methods allow declaration of inline methods without having to define a named method. 
This is very useful, especially in cases where the delegated function requires short and simple code. Anonymous methods can be used anywhere where a delegate type is expected.

Anonymous Method declarations consist of the keyword delegate, an optional parameter list and a statement list enclosed in parenthesis.

Event Handler (without using anonymous methods)

public partial class Form1 : Form 
// This method connects the event handler. 
public Form1() 
button1.Click += new EventHandler(ButtonClick); 
// This is the event handling method. 
private void ButtonClick(object sender, EventArgs e) 
MessageBox.Show("You clicked the button."); 

Event Handler (using anonymous methods)

public partial class Form1 : Form 
public Form1() 

button1.Click += delegate(object sender, EventArgs e) 
// The following code is part of an anonymous method. 
MessageBox.Show("You clicked the button, and " + "This is an anonymous                                 method!"); 

One more example:

public partial class Form1 : Form 
public Form1() 
// Declare the delegate that points to an anonymous method. 
EventHandler clickHandler = delegate(object sender, EventArgs e) 
MessageBox.Show("You clicked the button, and " + "This is an anonymous method!"); 
// Attach the delegate to two events. 
button1.Click += clickHandler; 
button2.Click += clickHandler; 

C# 2.0 Iterators

Iterator is one of the new feature in c# 2.0 which provide a way to create classes that can be used with foreach statement without implementing the IEnumerator & IEnumerable interfaces when compiler detects iterator it will automatically generate the current, MoveNext and dispose method of IEnumerable or IEnumerator interface.

foreach (item OrderItem in catalog) 
// (Process OrderItem here.) 

Enumerator e = catalog.GetEnumerator(); 
while (e.MoveNext()) 
OrderItem item = e.Current; 
// (Process OrderItem here.) 

one more example:

public class LinkInfo 
        public string FileName { get; set; }
        public string  LinkContent {  get; set;  } 
        public string  LinkStatus {  get; set;  }
        public string  NewLink {  get; set;  } 

    public static IEnumerable<LinkInfo> GetLinksFromListView(ListView lstLinkList) 
        foreach (ListViewItem li in lstLinkList.Items) 
            LinkInfo newLink = new LinkInfo(); 
            newLink.FileName = new string(li.Text.ToCharArray());
            newLink.LinkContent =  new string (li.SubItems[1].Text.ToCharArray()); 
            newLink.LinkStatus =  new string (li.SubItems[2].Text.ToCharArray()); 
            newLink.NewLink =  new string (li.SubItems[3].Text.ToCharArray()); 
            yield return newLink; 

C# 2.0 Partial Classes

Partial class was introduced in C#2.0 , it means that your class can be divied into multiple segments physically but at the time of compilation it will we grouped as a single entity.

// Stored in file MyClass1.cs 
public partial class MyClass 
public MethodA() 

partial  public  MethodB(); 

// Stored in file MyClass2.cs 
public partial class MyClass
public MethodB() {...} 

C# 2.0 Generics

Generics is a most powerful feature in .net.Generics allow you to define type-safe classes,collections,remoting,arrays etc..this will improve the performance in your code and leads to a quality code.

public class ObjectList<ItemType> : CollectionBase

private ArrayList list = new ArrayList(); 

public int Add(ItemType value) 

return list.Add(value); 

public void Remove(ItemType value) 


public ItemType this[int index] 

get { return (ItemType)list[index]; } 
set { list[index] = value; } 


// Create the ObjectList instance, and 
// choose a type (in this case, string). 
ObjectList<string> list = new ObjectList<string>(); 

// Add two strings. 

// The next statement will fail because it has the wrong type. 
// In fact, this line won't ever run, because the compiler
// notices the problem and refuses to build the application. 

C# 2.0 Generics constraints

where T: structThe type argument must be a value type
where T: classThe type argument must be a reference type
where T: new( )The type argument must have a public default constructor
where T: <base class name>The type argument must be the base class name or derived from the base class name.
where T: <interface name>The type argument must be the interface or implement the interface. Multiple interfaces may be specified.

Hope this post is useful...
Will be concentrate on the C#3.0/3.5 & 4.0 in Part II.

Thanks & Regards
Prabhakaran S Pandian

No comments:

Post a Comment