Wednesday, January 26, 2011

C# Extension Methods

What are extension methods? 
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Actually Inheritence is a powerfull way to add new features to an existing type. But it is not useful in some scenarios. Suppose if we have a type in our code that is used in many places. Now If we want to add some new methods to this type then one way we can do this is by using inheritance we can create a new type and provide the new methods we want in this type. This solution however has few disadvantages.
 

1. If we are using the class variables in many place in our code then we will have to replace all the existing class variables with the the new class variables. This needs refactoring of the entire application.
2. We can not use inheritance with structures.


as we know structures are implicitily sealed.  :-(

The solution is using extension methods.

Using extension methods we can add new methods to an existing Type. If we have lets say an int type and we want to add a new method to this type then by using extension methods we can do this. To achieve this we need to define a static class and a static method in that static class. And we need to use the this keyword along with the type we are adding this extension method to as the first method parameter type.

Here is a short example of an extension method that multiplies the integer entered by the user by 5 using extension method.

public static class MyInt
{
    public static int MultiplyByFive(this int num)
    {
        num *= 5;
        return num;

    }
}
class ExtensionMethods
{   

public static void Main(string[] args)
    {
        int x;
        Console.WriteLine("Enter an integer");
        x=int.Parse(Console.ReadLine());
        int y=x.MultiplyByFive();
        Console.WriteLine("Integer {0} multiplied by 5 is {1}",x,y);
        Console.WriteLine(y);
        Console.ReadLine();

    }
}
Remember that extension methods must be defined inside a top level static class and not inside a nested class and the method itself must be declared as static. System. Linq namespace has a lot of extension methods that makes easier working with Linq.

Sunday, January 16, 2011

Top 3 Successful Habits (You Must Adopt)


Do you want to be a winner in your life? If you do, this will be the perfect article that you must continue to read on. You are about to find out the top 3 winner attitude that are adopted by every successful person out there. And if you adopt the same habit, you will produce the same amazing results in your life. You can achieve any target, goal and dream that you desire.


You have to understand that success is something that you repeatedly do in your life. It is not something that will happen in an instant. You are not going to become a millionaire overnight. Thus, success is determined by the actions that you take in your life. And your actions will be determined by your habits. Therefore, if you adopt the winning habit, you will produce winner’s results…

 1. The first winner habit that you must adopt is this, you must have a clear vision. Always think and focus on your vision. You have to have a clear target of what you want. If you do not have a target, how can you actually hit it? It is just like driving a car, if you do not have a destination, you will end up nowhere. Thus, find out what you want to accomplish in your life right now. Set clear and specific targets and goals in every area of your life.
2. The second winner habit is to be proactive. Think about it, money will not grow on tree and it will not fall from the sky. You have to be the one who is proactive to take action and make things happen. If you are not doing anything, you will produce nothing. So make sure you do something each day so that you will move toward your goals and your dreams everyday. Action produces results. Stop putting things off. Watching television and chatting online are not going to change your life. Do what is necessary right now.
3. The final winner habit that you must adopt here is never give up. This is a common sense, but unfortunately, most people do not have this habit. How many rejection and failure can you take before you quit? If you quit, you will waste all the effort that you have poured in before. So do not quit no matter what. Treat failure and rejection as feedback that can improve your knowledge and skill. This is how Thomas Edison became one of the best inventors in the century. He treats all failures as feedback to improve and move toward his target. You have to be the same.