rss
twitter
  •  

C# 3.0 New Features

| Posted in C# |

0

In this post, i will write about all the new features in C# 3.0, this features will help you more and more in your programming life, and class design.

this list of C# features which will be covered here,

1. Extension method

2. Lambda expression

1. Extension Method

  • extension method is new feature in C# 3.0, it is like static methods which we know in our object oriented background but with more main differences:
    • Extension methods must written in static class not like static method which can written in any class
    • To specify extension method, you need to write this keyword before the first argument in the method, static methods don’t have this keyword at declaration
    • at consumed, the argument with this keyword isn’t passed to the function
    • extension methods can be called only on instances, calling them on class will result in compilation errors, the class instances on which they are called are determined by the first argument in the declaration in other words the argument has this keyword
    • Note that extension methods can’t access private methods of extended type
    • Note that if you don’t have source code of type, and you don’t add new methods to type
    • Note that if you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never called.
    • extension methods can’t be used to override existing methods
    • concept of extension methods can’t be applied to fields, properties or events
    • if the class is sealed than there in no concept of extending its functionality, for this a new concept is introduced

But why you determine that you want to implement this new feature, let’s assume the following scenario you need to implement new method in Int class to get the power of the integer, as the traditional thinking you will design class which inheriting from the parent class Int and implement new method called Power but with C# 3.0 you have another option implement extension method  Power() on Int class, let’s try it with your hand

Post a comment