Sunday, July 4, 2010
Code Reusability
I went to bed but I couldn't sleep, so before I sleep tonight I am going to tell you what is Better Programming.
What is Better Programming? You don't know the answer then don't worry, read this blog completely.
Writing reusable code is called Better Programming.
Then what is Code Reusability?
Write it once and make it available anywhere and any place. Make the changes in one place and changes available to the application it uses the code.
You don't want to rebuild the code again and again; you want to reuse the code.
Ctrl ‘C’ and Ctrl ‘V’ copying and pasting the code is that reusable code? Yes, it is also code reusability if you worked in ‘C’ or ‘C++’ languages then you know about header files, #include ‹io.h› this is actually does copying and pasting the code, the complier does that. But i didn't mean about this.
After Microsoft came up with .dll Dynamic Link Libraries then copying and pasting the code is gone away.
So write the code in such a manner that it can be reused. Because your manager should say that you did a Better Programming for that :-). Ok guys, I am going to sleep now. See you in my Next Blog.
Sunday, June 27, 2010
.Net 3.5: Extension Methods
OK.......Let us see what is Extension Methods.
What you do if you want to add a method to a class , yes it is simple, why not, we can add a method to a class, this is OK.
You want to add a method to a class but you don't have the source code of that class but still you want to add a method.
Without disturbing the source code of a class you want to add a method to that class.
Another case, You want to add a method which can be used by several different classes which are not from same family and you want to define the method in a central repository.
Extension Methods is the answer for all the above questions.
Extension methods enable you to add the behavior (method) externally to a class. You can define a method in a central repository and then you can attach that method to several different classes without disturbing the source code of that classes.
You can extend the existing functionality without changing the existing code. Extension methods is a great way to provide flexible design.
An extension method is nothing but Microsoft implementation of Visitor Pattern.
Visitor Pattern:
IVisitor is an interface, which is responsible for calling the exact implementaion (VisitorImp1A) of the methods. So if Class A wants to call a method then Class A will call the IVisitor and the IVisitor will decide the exact method to attach it to the Class A. VisitorImp1A can be attached to all the classes A, B and C.
Visitor Pattern : Allows you to define a new operations with out changing the classes on which it operates.
Extension method signature:
static string reverse(this string str, string value)
static: Extension method should be static method, static is a keyword which is used to declare a method as a static method.
string: return type of the reverse Extension method is a string.
reverse: reverse is the extension method name.
this: ‘this’ Keyword is used to denote that the current method is an extended method of this class.
the first parameter of a Extension method can be the name of the class to which this method is attached externally. Here the Extension method reverse is attached to the String class.
Create an ordinary method and the 1st parameter of the method can be the class using the keyword this to which the method is attached externally.
The class in which the extended methods are created that class should be a static class.
Sample Code:
In this sample code i am going to show you how to add a reverse method to a String class using Extension Methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Extension_Methods
{
public static class StringExtension
{
//Adding Extension method called reverse to the String class.
//this Keyword denotes that the reverse method is an extended method of String class.
public static string reverse(this string str, string value)
{
Char[] chars = value.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
}
}
I created a class called StringExtension inside this class I created a method called reverse and added it to the String class by declaring in the first parameter of the method this string str, which tells that this reverse method is a extension method of the string class. Don't forget the keyword this because this makes the difference of your method from Ordinary method to an Extension method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Extension_Methods
{
class Program
{
static void Main(string[] args)
{
String reverseValue = "Welcome";
//Calling the Extension method called reverse.
reverseValue = reverseValue.reverse(reverseValue);
Console.WriteLine(reverseValue);
Console.ReadLine();
}
}
}
I am calling the reverse method that I created in the StringExtension class and added it to the String class and I am printing the reverse value of "Welcome" using the Extension method called reverse.
If you want to make some changes to the reverse method then you can do the changes in the StringExtension class without anthing to the String class.
Interesting right!
Thanks to Extension Methods. Ok guys, See you in my next blog.