Udemy

Should i use Abstract Class or Interface ? (abstract class vs Interface)

Tuesday, April 28, 2015 0 Comments A+ a-

Most of the time developers get confused about abstract class and interface. while interview most of the time interviewer asks the difference between abstract class and interface, we give answer that we read on internet and what we have understanding, but it gets messed up always.


Also when developing applications, we are not sure often that it is the scenario to use abstract class or create interface.

I am posting text from CLR via C# book where Jeffery Richter is trying to explain when should we use abstract class and when interface. Here is the text from book:

I often hear the question "Should i design a base type or interface ?" The answer is not always clear cut.

Here are some guidelines that might help you:


  • IS - A VS CAN-DO relationship : A type can inherit only one implementation If the Derived type can't claim  IS-A relationship with the base type, don't use a base type, use an interface. interfaces imply a CAN-DO relationship. If the CAN-DO functionality appears to belong with various object types, use an interface. For example, a type can convert instances of itself to another type (IConvertible), a type can serialize an instance of itself (ISerializable), etc. Note that value types must be derived from System.ValueType, and therefore, they cannot be derived from an arbitrary base class. In this case, you must use a CAN-DO relationship and define an interface.
  • Ease of Use: It’s generally easier for you as a developer to define a new type derived from a base type than to implement all of the methods of an interface. The base type can provide a lot of functionality, so the derived type probably needs only relatively small modifications to its behavior. If you supply an interface, the new type must implement all of the members.
  • Consistent Implementation:No matter how well an interface contract is documented, it’s very unlikely that everyone will implement the contract 100 percent correctly. In fact, COM suffers from this very problem, which is why some COM objects work correctly only with Microsoft Word or with Windows Internet Explorer. By providing a base type with a good default implementation, you start off using a type that works and is well tested, you can then modify parts that need modification.
  • Versioning:If you add a method to the base type, the derived type inherits the new method, you start off using a type that works, and the user’s source code doesn't even have to be recompiled. Adding a new member to an interface forces the inheritor of the interface to change its source code and recompile.
Udemy

Extension Methods Feature in C# with Simple Example

Monday, April 27, 2015 0 Comments A+ a-

Introduction:


Extension methods are special type of methods which were introduced in C# 3.0. They are used to extend the functionality of  any existing type in .Net.Using extension method we can add new methods to a class/type without modifying original source code of  Type, recompiling, or creating a new derived type.

Normally when we use linq standard query operators that add query functionality to the existing IEnumerable and System.Collections.Generic.IEnumerable<T> types. For using the standard query operators,first bring them in scope by writing using System.Linq in the code file.

Now you can call for example GroupBy,OrderBy etc like instance methods on any type that implement IEnumerable<T>

According to MSDN :


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. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

Syntax of writing Extension Method :


As extension method is a special method, it is defined little differently than normal method.Here is the points to be noted when writing extension method:

  1. It should be a static method.
  2. It should be written in a static class.
  3. its first parameter starts with this keyword followed by type parameter
  4. Extension method should be in the same namespace otherwise you will need to inculde namespace of the class with a using statement. 

Simple Example:


We can write extension method for any existing type in our code.Most of the time we write extension method for types that we cannot modify which are built-in in the .Net Framework.

I will make a simple extension method on string type to convert string to integer.


public static class StringExtensions
{
 public static int ToInt32(this string value)
 {
  int result;
  if (int.TryParse(value, out result))
  {
   return result;
  }

  return 0;
 }
} 

and we can use it to convert string to integer if string contains numeric value in. We can call it this way:


public class Program
{
 public static void Main()
 {
  
  Console.WriteLine("123".ToInt32());
  
 }
}

You can see running example in  this fiddle