Monday, January 27, 2014

Delegates and Delegates Default Functions

1. Delegates are objects that contain information about the function rather than data.
2. We can encapsulate a reference to the method inside the delegate object.
3. Delegates are type safe.
4. The return type and signature of  delegates should be the same as the function
5.It is not necessary to use delegates with parameters; we can use delegates with parameters and without       parameters.
6.We can use static or nonstatic functions; in the case of static functions we encapsulate the function with the class name and in the case of a nonstatic function we first make the object of the class and  then we use a function with the object.

Example:
namespace Delegates
 {
    public delegate int mydelegate(int a,int b);
     class Program
     {
         static void Main(string[] args)
         {
             mydelegate delObj = new mydelegate(abc.Sub);
             abc delObj = new abc();
             mydelegate delObj1= new mydelegate(obj.Add);
             int a = delObj(4, 2);
             int b = delObj1(4, 2);
             Console.WriteLine("the result of ss is {0} and the result of ss1 is {1}", a, b);
         }
     }
 class abc
     {
         public static int Sub(int i, int j)
         {
             return i * j;
         }

        public int Add(int x, int y)
         {
             return x + y;
         }
    }
 }

Multicasting with Delegates
1. We can call more than one function using the delegate.

2. For this we use the operators "+=" and "-=".

3. We use "+=" to add a method in the invocation list and the "-=" operator to remove a method    from the invocation list.

public delegate void my_multi_delegate(int x,int y);   
    //declaring a multi cast delegate with return type void

Multicasting Example:

mymultidelegate del = new mymultidelegate(abc.Add); //encapsulate the fun Add with the delegate

 del += new mymultidelegate (abc.Sub); //adding fun Sub in the invocation list with the help of += operator
  del += new mymultidelegate (abc.Mul); //adding fun Mul in the invocation list
 del(4, 2);
  //now we remove the function Add from the invocation lit with the help of -= operator
 del -= new mymultidelegate (abc.ss); // Now in the invocation list 2 method namely Sub and Mul remains
 
Multicast Delegate Support Void Return:

Because they are multicast, calling more than one method. Which return value would you use? How would you combine them (that is, how would the framework combine them)?
You can't use ref or out arguments either, for the same reason.

still its achiveable through GetInvocationList();

but manually call each handler from the invocation list of the delegate invocation list. Here is the example on how to do it:
 
var list = delegateInstance.GetInvocationList();
foreach (var handler in list) {
   DelegateName method = (DelegateName )handler;
   int retValue = method("some parameter");
   //do something with retValue...
}

Delegates system functions: Introduced in 3.5 Framework

Action is a delegate (pointer) to a method, that takes zero, one or more input parameters, but does not return anything.
list.Select(x => x.SomeProperty)

Func is a delegate (pointer) to a method, that takes zero, one or more input parameters, and returns a value (or refence).

Predicate is a special kind of Func often used for comparisons.

Though widely used with Linq, Action and Func are concepts logically independent of Linq.

Sunday, January 26, 2014

Factory Method Pattern

The factory method pattern defines an interface for creating an object, but let subclasses decide which class to instantiate.

Example:

//Create the interface
    public interface iEmployee
    {
        void display(string FirstName, string LastName);
    }


    //Create the Employee Class and Implement the iEmployee Interface
    public class Employee : iEmployee
    {

        public void display(string FirstName, string LastName)
        {
            Console.WriteLine(FirstName +' '+ LastName );
        }

    }

    //Create the abstract EmployeeFactory Class.
    //Create GetEmployeeFactory method with return type of interface (iEmployee).
    public abstract class EmployeeFactory
    {
        public abstract iEmployee GetEmployeeFactory();
   
    }


    //Inherit and Implement the abstract EmployeeFactory class.
    //Override the abstract GetEmployeeFactory method.
    public class EmployeeList : EmployeeFactory
    {

        public override iEmployee GetEmployeeFactory()
        {
            return new Employee();

        }


    }

static void Main(string[] args)
        {

            EmployeeFactory obj = new EmployeeList();
            iEmployee iObj = obj.GetEmployeeFactory();

            iObj.display("Factory", "Method");
               
            Console.ReadLine();
        }

    }

Thursday, January 23, 2014

Singleton Pattern with Example

1. Singleton pattern falls under Creational Pattern .
2. Singleton pattern ensures that a class has only one instance and provides a global point of access to it.

Singleton Example:
This is a class which is responsible for creating and maintaining its own unique instance.

Advantage vs Static calss
• we can create one instance of the object and reuse it.
• singleton class can have constructor.
• you can create the object of singleton class and pass it to method
• Singleton class does not say any restriction of Inheritance.
• We can dispose the objects of a singleton class but not of static class
• we can implement interface with Singleton class but not with Static class.

Syntax:
public class singleton
{
private singleton objInstance = new singleton();

private singleton() { }

public singleton GetInstance()
{
if (objInstance == null)
{
objInstance = new singleton();
}
return objInstance;
}

public string GetNames(string FirstName, string LastName)
{
return FirstName + ' ' + LastName;
}

}

Call from Mail Class:
string FullName = Singleton.GetSingleInstance().GetNames("Welcome", "Google");

Wednesday, January 22, 2014

Dispose Vs Finalize method ?

Dispose:
1) Dispose is a method which used to handle resource cleanup in run time environments ,that uses automatic garbage collection

2) Dispose is available with those object which implements IDisposable interface. (Defaulty by framework)

3) Internally, it is called by Garbage Collector and cannot be called by user code.

Finalize:

1) Finalize is used to fire when the object is going to release the memory.We can set a alert message to says that this object is going to dispose

2) Explicitly, it is called by user code and the class implementing dispose method must implement IDisposable interface.

For Example,
If we are opening a file in our application,this will be released in Dispose() method.

Normally before releasing database connection, we have to close the database connection. we have to place the code for Closing the file operation will be taken place in Finalize() method..






.Net Garbage Collection

Memory management is the main concern for any application whether application is window based or web based. In .Net, CLR has garbage collector that executes as a part of our program and responsible for reclaiming the memory of no longer used objects. Garbage collector free the memory for objects that are no longer referenced and keeps the memory for future allocations.

Importan Points,

All objects in the heap are allocated from one connecting range of memory address and heap is divided into generations so that it is easy to eliminate the garbage objects by looking at only a small fraction of the heap.

01.Generation 0
This is the youngest generation and contains the newly created objects. Generation 0 has short-lived objects and collected frequently. The objects that survive the Generation 0 are promoted to Generation 1.

Example : A temporary object.

02.Generation 1
This generation contains the longer lived objects that are promoted from generation 0. The objects that survive the Generation 1 are promoted to Generation 2. Basically this generation serves as a buffer between short-lived objects and longest-lived objects.

03.Generation 2
This generation contains the longest lived objects that are promoted from generation 1 and collected infrequently.

Example : An object at application level that contains static data which is available for the duration of the process.








Saturday, January 18, 2014

Difference between Stored Procedure and Function in SQL Server?

Stored Procedures are pre-compile objects which are compiled for first time and its compiled format is saved which executes (compiled code) whenever it is called. But Function is compiled and executed every time when it is called.

1. Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.

2.Exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used in a Function.

3. Procedures can not be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.

4. Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it.

Abstract Factory Vs Factory Method

Abstract Factory Provides an interface for creating families of related or dependent objects without specifying their concrete classes ...