Showing posts with label General C#. Show all posts
Showing posts with label General C#. Show all posts

Wednesday, 23 September 2015

real world use of dynamic in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Moq;

namespace DynamicKeyword
{
    public class Base
    {
        public void foo(int x) { Console.WriteLine(x); }
    }
    public class Derived : Base
    {
        public void foo(string x) { Console.WriteLine(x); }
    }
    class Program
    {
        static void Main(string[] args)
        {

            dynamic baseclass = new Derived();
            dynamic d = "4";
            dynamic c=1;
            baseclass.foo(d);
            baseclass.foo(c);
            //new StaticTester().Call();
            //new ReflectiveTester().Call();
            //new DynamicTester().Call();
            Console.ReadKey(false);
        }
    }

}

Tuesday, 26 August 2014

Diffrence between Overloading and Overriding in C#

Overloading: is the mechanism to have more than one method with same name but with different signature (parameters). A method can be overloaded on the basis of following properties
  1. Have different number of parameter
  2. Having same number of parameters but of different type
  3. Having same number and type of parameters but in different orders
A method cannot be overloaded on the basis of
  1. Different return type
  2. Different access modifier
  3. Normal and optional parameters
Some example valid examples:
public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
public List<Customer> FindCustomer(Int32 customer_Id)
{
  // your code
}
public List<Customer> FindCustomer(String customer_name, String city)
{
  // your code
}
public List<Customer> FindCustomer(String customer_name, Int32 customer_Id)
{
  // your code
}
public List<Customer> FindCustomer(String customer_name, String city, String zip)
{
  // your code
}
Following are invalid overloading because we can not overload on the basis of different return type
public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
public Customer FindCustomer(String customer_name)
{
  // your code
}
Following are also invalid overloading, because we cannot overload on the basis of access modifier
public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
private List<Customer> FindCustomer(String customer_name)
{
  // your code
}
Following are also not valid, because we cannot overload normal and optional parameter
public List<Customer> FindCustomer(String customer_name)
{
  // your code
}
private List<Customer> FindCustomer(String customer_name = String.Empty)
{
  // your code
}
In the same way we can overload constructor of a class by defining more than one constructor with different parameters which is known as constructor overloading.
Overriding: Overriding can be done in derived class, an override method provides a new implementation of a method inherited from parent class.
To override a method in base (parent) class it must be
  1. virtual
  2. abstract
  3. override
We cannot override a base method which is in base class as
  1. non-virtual
  2. static
We cannot use the following modifiers to modify an override method in derived class
  1. new
  2. static
  3. virtual
  4. abstract
Let's see it with example:
public class Square {
  public double side;
  // Constructor:
  public Square(double side) 
  {  this.side = x; }

  public virtual double Area() 
  {   return side  * side ;  }
}
Note Area method is defined with virtual so it can be overridden in derived class, now lets derive a class cube from square and override it's Area method
class Cube: Square {
   // Constructor:
   public Cube(double side): base(x)    { }
   // Calling the Area base method:
   public override double Area() 
   { return (6*(base.Area())); }
}
Conclusion:
  • Overloading can be done in same class
  • Overriding can be done in parent and derived class
  • Overloading in used when we need same method in same class with different parameters
  • Overriding is used when we need to redefine a method that has already been defined in parent class (using the exact same signature
  • Overloading is resolved at compile time
  • Overriding is resolved at run time

Difference between var and dynamic



var
dynamic
Introduced in C# 3.0
Introduced in C# 4.0
Statically typed – This means the type of variable declared is decided by the compiler at compile time.
Dynamically typed - This means the type of variable declared is decided by the compiler at runtime time.
Need to initialize at the time of declaration.
e.g., var str=”I am a string”;
Looking at the value assigned to the variable str, the compiler will treat the variable str as string.
No need to initialize at the time of declaration.
e.g., dynamic str; 
str=”I am a string”; //Works fine and compiles
str=2; //Works fine and compiles
Errors are caught at compile time.
Since the compiler knows about the type and the methods and properties of the type at the compile time itself
Errors are caught at runtime 
Since the compiler comes to about the type and the methods and properties of the type at the run time.
Visual Studio shows intellisense since the type of variable assigned is known to compiler.
Intellisense is not available since the type and its related methods and properties can be known at run time only

e.g., var obj1;
will  throw a compile error since the variable is not initialized. The compiler needs that this variable should be initialized so that it can infer a type from the value.
e.g., dynamic obj1; 
will compile;
e.g. var obj1=1;
will compile 
var obj1=” I am a string”;
will throw error since the compiler has already decided that the type of obj1 is System.Int32 when the value 1 was assigned to it. Now assigning a string value to it violates the type safety.
e.g. dynamic obj1=1;
will compile and run 
dynamic obj1=” I am a string”; 
will compile and run since the compiler creates the type for obj1 as System.Int32 and then recreates the type as string when the value “I am a string” was assigned to it.
This code will work fine. 

Wednesday, 20 August 2014

Difference between virtual and abstract method in c#





Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and forces the derived classes to override the method.
So, abstract methods have no actual code in them, and subclasses HAVE TO override the method. Virtual methods can have code, which is usually a default implementation of something, and any subclasses CAN override the method to provide a custom implementation.
public abstract class E
{
    public abstract void AbstractMethod(int i);

    public virtual void VirtualMethod(int i)
    {
        // Default implementation which can be overridden by subclasses.
    }
}

public class D : E
{
    public override void AbstractMethod(int i)
    {
        // You HAVE to override this method
    }
    public override void VirtualMethod(int i)
    {
        // You are allowed to override this method.
    }
}


An abstract method is a method that must be implemented to make a concrete class. The declaration is in the abstract class (and any class with an abstract method must be an abstract class) and it must be implemented in a concrete class.
A virtual method is a method that can be overridden in a derived class using the override, replacing the behavior in the superclass. If you don't override, you get the original behavior. If you do, you always get the new behavior. This opposed to not virtual methods, that can not be overridden but can hide the original method. This is done using the new modifier
  • Abstract functions - when the inheritor must provide its own implementation
  • Virtual - when it is up to the inheritor to decide
  • Only abstract classes can have abstract members.
  • A non-abstract class that inherits from an abstract class must override its abstract members.
  • An abstract member is implicitly virtual.
  • An abstract member cannot provide any implementation (abstract is called pure virtual in some languages).