Sunday, July 4, 2010

Test your C sharp skills - 1

http://www.wiziq.com/online-tests/14096-csharp-your-skills-1

Extension methods in c#

Extension methods in c#

This feature has been added to c# 3.0. Have a look at it.


An extension method is a new language feature of C# starting with the 3.0 specification. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.

Using extension methods, you can declare methods that appear to augment the public interface, or contract, of a type. At first glance, they may appear to provide a way to extend classes that are not meant to be extended. However, it’s very important to note that extension methods cannot break encapsulation. That’s because they’re not really instance methods at all and thus cannot crack the shell of encapsulation on the type they are extending.

An Extension Method is defined by several rules:

· The method is defined in a non-generic static class that is not nested.

· The method itself is static.

· The first parameter of an Extension Method is preceded by the modifier this. This parameter is called an “instance parameter” and can only appear as the first parameter of the method.

· No other parameter modifiers (ref, out, etc…) are allowed with the modifier this. As a result, a value type can’t be passed by reference to an Extension Method.

· The instance parameter cannot be a pointer type.

· The method must be public, internal or private: it's a stylistic choice to declare them public, but not a requirement!

· Extension Methods are in a namespace which is in scope.

· If your method successfully matches all these points, you can safely say that it’s an Extension Method!

Here is an example of creating Extension method that adds ConvertToUpper() method to already existing Framework class String.

namespace ExtensionMethods

{

public static class Tools

{

public static string ConvertToUpper(this string str)

{

return str.ToUpper();

}

}

}

Here is the use of extension method in actual program.

namespace ExtensionMethods

{

class Program

{

static void Main(string[] args)

{

String s = "My document";

Console.WriteLine(s.ConvertToUpper());

}

}

}

Notice that the static class containing extension method (here, class Tools) and the class using the extension method (here class Program) both are in same namespace.

· If your Extension Method is in another namespace (or another DLL), you will need a using statement to import the content of this namespace and make the call of your method possible

using ExtensionMethods;

namespace anothernamespace

{

class newprogram

{

static void Main()

{

String s = "New document";

Console.WriteLine(Tools.ConvertToUpper(s));

}

}

}

· If you're extending a 3rd party class and in a later version they introduce a new method with the same signature, you won't easily know that the meaning of your calling code has changed. If the new method is very similar to your extension, but with subtly different boundary conditions (or whatever) then this could lead to some very tricky bugs. It's relatively unlikely to happen though.

There is little restriction on Extension methods:

1. Extension methods cannot be used to override existing methods.

2. An extension method with the same name and signature as an instance method will not be called.

3. The concept of extension methods cannot be applied to fields, properties or events.

So caution: Use extension methods sparingly....overuse can be a bad thing.

variable arguments

2 methods are defined as below:

public void Add(int a, int b)
{
Console.WriteLine("2 parameter function");
Console.WriteLine(a + b);
}

public void Add(params int[] val)
{
int sum=0;
Console.WriteLine("variable argument method");

for (int i = 0; i < val.Length; i++)
{
sum += val[i];
}
Console.WriteLine(sum);

}

Which method will be called on following statements :

1. Add ();
2. Add ( 4,6);
3. Add (10, 20, 30);

Convert.ToString or ToString ?

What is difference between Convert.ToString() and ToString()? Which one to prefer?

Virtual or abstract?

First post in this blog.


One of my friend asked me this question.

class A
{
XXX method1()
......
}


class B : A
{
override method1 ()
{
base.method1();
}
}

He says, how can we identify, XXX (written in front of method1 in class A) is virtual or abstract. Give justification.