Wednesday, April 8, 2015

Reflection in C#

Reflection


1.Reflection can be used for type discovery (i.e finding methods, properties, events, fields, constructors etc) and late binding.

2.When we drag and drop a button from toolbox in visual studio, vs uses reflection to get all properties of button class.

3.Late binding can be achieved by using reflection. You can use reflection to dynamically create an instance of a type, about which we don't have any information at compile time.

Example:

class Program
    {
        static void Main(string[] args)
        {
            //Type t = Type.GetType("ReflectionExp.Customer");
            Type t = typeof(ReflectionExp.Employee);

            PropertyInfo[] propInfo = t.GetProperties();

            // Print the list of Methods
            Console.WriteLine("Methods in Employee Class");
            MethodInfo[] methods = t.GetMethods();
            foreach (MethodInfo method in methods)
            {
                // Print the Return type and the name of the method
                Console.WriteLine(method.ReturnType.Name + " " + method.Name);
            }
            Console.WriteLine();

            //  Print the Properties
            Console.WriteLine("Properties in Employee Class");
            foreach(PropertyInfo prop in propInfo)
            {
                Console.WriteLine(prop.Name + " " + prop.PropertyType.Name + " " + prop.ReflectedType);
            }

            Console.WriteLine();
            //  Print the Constructors
            Console.WriteLine("Constructors in Employee Class");
            ConstructorInfo[] constructors = t.GetConstructors();
            foreach (ConstructorInfo constructor in constructors)
            {
                Console.WriteLine(constructor.ToString());
            }
            Console.Read();
        }
    }

     public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }


        public Employee(int ID, string Name)
        {
            this.Id = ID;
            this.Name = Name;
        }


        public Employee()
        {
            this.Id = -1;
            this.Name = string.Empty;
        }


        public void PrintID()
        {
            Console.WriteLine("ID = {0}", this.Id);
        }
        public void PrintName()
        {
            Console.WriteLine("Name = {0}", this.Name);
        }

    }

Sunday, April 5, 2015

Multicast Delegates

Multicast Delegates

1.A multicast delegate is a delegate that has references to more than one function.

2.When a multicat delegate is invoked,all the functions the delegate is pointing to, get invoked.

3.There are two approaches to create a multicast delegate:

    + or += to register a method with the delegate
    - or -= to un-register a method with the delegate

Example:

public delegate void SampleDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            SampleDelegate del1, del2, del3, del4;
            del1 = new SampleDelegate(SampleMethod1);
            del2 = new SampleDelegate(SampleMethod2);
            del3 = new SampleDelegate(SampleMethod3);

            del4 = del1 + del2 + del3;
            del4();

            //another way
            Console.WriteLine();
            Console.WriteLine("another way");
            Console.WriteLine();
            SampleDelegate del = new SampleDelegate(SampleMethod1);
            del += SampleMethod2;
            del += SampleMethod3;

            //del -= SampleMethod1;

            del();

            Console.Read();
        }

        public static void SampleMethod1()
        {
            Console.WriteLine("Sample Method 1");
        }

        public static void SampleMethod2()
        {
            Console.WriteLine("Sample Method 2");
        }

        public static void SampleMethod3()
        {
            Console.WriteLine("Sample Method 3");
        }
    }

Delegates Example

Delegates Example


class Program
    {
        static void Main(string[] args)
        {
            List<Employee> empList = new List<Employee>
            {
                new Employee{ Id = 1,Name= "Mary",Salary=5000,Experience = 5},
                new Employee{ Id = 2,Name= "Mike",Salary=4000,Experience = 4},
                new Employee{ Id = 3,Name= "John",Salary=6000,Experience = 6},
                new Employee{ Id = 4,Name= "Todd",Salary=3000,Experience = 3}
            };
            IsPromotable isPromotable = new IsPromotable(Promote);
            Employee.PromoteEmployee(empList,isPromotable);

            Employee.PromoteEmployee(empList, x=> x.Experience >= 5);  // using lambda expression
            Console.ReadKey();
        }

        public static bool Promote(Employee emp)
        {
            if (emp.Experience >= 5)
            {
                return true;
            }
            return false;
        }
    }

    public delegate bool IsPromotable(Employee emp);

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }

        public static void PromoteEmployee(List<Employee> employeeList, IsPromotable isPromotable)
        {
            foreach (Employee emp in employeeList)
            {
                if (isPromotable(emp))
                {
                    Console.WriteLine(emp.Name + " promoted");
                }
            }
        }
    }

Delegates in C#

Delegates in C#



1.A delegate is a type safe function pointer. It holds a reference(pointer) to a function.

2.The signature of a delegate must match to the signature of the function the delegate points to, otherwise it will give compile time error. This is the reason, why delegates are type safe function pointer.

3.A delegate is similar to a class. You can create an instance of it, and pass the function as parameter in delegate constructor. This way, the delegate will point to that function.

Example:

public delegate void MyDelegate(string msg);

    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate del = new MyDelegate(PrintHello);
            del("My delegate");
            Console.Read();
        }

        public static void PrintHello(string message)
        {
            Console.WriteLine(message);
        }
    }

Saturday, April 4, 2015

Abstract Classes Vs Interfaces

Abstract Classes Vs Interfaces


1.An Abstract class can have some of its members(methods) implementation but an Interface can't have its members implementation .

2.An Abstract class members can have access modifiers where as interface members can't.

3.An Abstract class can have fields where as interface can't.

4.An interface can only inherit from another interface only and can not inherit from abstract class where as an abstract class can inherit from another abstract class or interface.

Abstract Classes

Abstract Classes:

1.The abstract keyword is used to create abstract classes.

2.An abstract class is incomplete hence can't be instantiated.

3.An abstract class can only be used as a base class.

4.An abstract class can only be sealed.

5.An abstract class may contain abstract members(method,properties,indexers and events),but not mandatory.

6.A non-abstract class derived from an abstract class must provide implementation for all inherited abstract members.

Example:

public abstract class Customer
    {
        public abstract void Print();
    }
    class Program:Customer
    {
        public override void Print()
        {
            Console.WriteLine("Abstract Print");
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Print();
            Console.ReadKey();
        }
    }