C# notes part ->4


Abstract class

·        Abstract class is a collection of abstract methods as well as concrete methods.
·        It cannot be instantiated.
·        It can be extendable by other classes.



Syntax:
Abstract class classname
{
       //abstract methods
       //concrete methods
}
Concrete Method: Those are methods which will support the implementation details(these are ordinary methods).
Abstract Method: These are methods which do not support the implementation (it supports prototype declaration).
Eg: public void Add(int x, int y);
Program on Abstract class and Methods

using System;
namespace abstractckassmethods
{
    public abstract class Sample
    {
        public abstract void  Add(int x,int y);
        public abstract void Sub(int x,int y);
        public void Display()
        {
            Console.WriteLine("This is Concrete Method");

        }
       

    }
    public class Test : Sample
    {
        public override void Add(int x, int y)
        {
            Console.WriteLine("the sum is {0}",x+y);
        }
        public override void Sub(int x, int y)
        {
            Console.WriteLine("the difference is  {0}",x-y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Add(4, 2);
            t.Sub(4, 2);
            t.Display();
          
            Sample s = new Test();
            s.Add(10,5);
            s.Sub(10, 5);
            Console.ReadLine();


        }
    }
}

interface

it is a collection of abstract methods withot abstract keyword, these methods will be implemented by classes.
It cannot be instantiated.
It is reusable.
All the methods in the interface are by default public.
Syntax:
interface interfacename
{
       //abstract methods
}
public class classname : interfacename
{
       //implementation of abstract methods
}
program
using System;
namespace InterfaceEx
{
    interface I1
    {
        int add(int x,int y);//interface supports abstract methods without the abstract keyword
        int sub(int x,int y);
    }
    interface I2
    {
        int mul(int x,int y);
    }

    public class Sample:I1,I2
    {      
        public int  add(int x, int y)
         {
           return x+y;
         }

       public int  sub(int x, int y)
        {
           return x-y;
        }
   
        public int mul(int x, int y)
        {
            return x * y;
        }

  
    }

    class Program
    {
        static void Main(string[] args)
        {
            I1 obj =new Sample ();
            int r=obj.add(10,4);
            Console.WriteLine("the addition is {0}",r);
            r=obj.sub(10,5);
            Console.WriteLine("the subtraction is {0}",r);
           
           
            I2 obj1 = new Sample();
            r=obj1.mul(10, 10);
            Console.WriteLine("the multiplition is {0}", r);
           
           
           
            Console.ReadLine();


        }
    }
}


sealed class and sealed methos:

it is used to seal the overridden method.


using System.Text;
namespace sealex
{

    class Sample
    {
        public virtual void Display()
        {
            Console.WriteLine("This is Sample Display()");
        }
        public virtual void Print()
        {
            Console.WriteLine("This is Sample Print()");
        }
    }
    class Test : Sample
    {
        public override void Display()
        {
            base.Display();
            Console.WriteLine("This is Test Display()");

        }
        public sealed override void Print()   //Print() can't be inherited further from here
        {
            base.Print();
            Console.WriteLine("This is Test Print()");
        }
    }
    class Trial : Test
    {
        public override void Display()
        {
            base.Display();
            Console.WriteLine("This is Trial Display()");
        }
      

    }
    class Program
    {
        static void Main(string[] args)
        {
            Trial t = new Trial();
            t.Display();
            t.Print();
            Console.ReadLine();
        }
    }
}
delegates

·        A delegate is a reference type variable which holds the reference to a method. It is the general purpose mechanism for indirectly calling the methods at runtime.
·        The primary use of delegate is for implementing events and callback methods.
In order to implement the delegates, we should follow the  following steps.
1.  Declare a delegate
Syntax: 
delegate returntype delegatename(parameters list);
Eg: delegate void DelAdd(int x,inty);
2.  Defining the delegate function.
Syntax:
Accessspecifier returntype funct-name(paramerlist)
{
……
}
3.  Instantiating the delegate.
Syntax:
Delegatename objname=new           delegatename(delegatefunctionname);
4.  Invoking the delegate.
Syntax:
Objectname(values);

using System;
namespace DelegateappEx
{
    public delegate int deladdsub(int x,int y);

    class sample
    {
          public static  int add(int x, int y)
        {
            return x + y;
        }
        public static int sub(int x, int y)
        {
            return x - y;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            deladdsub d1 = new deladdsub(sample.add);
            deladdsub d2 = new deladdsub(sample.sub);
            int r=d1(10, 20);
            Console.WriteLine("the addition is {0}",r);
             r=d2(10, 20);
            Console.WriteLine("the addition is {0}", r);
            Console.ReadLine();

           
        }
    }
}

Delegate types
1.  Single cast delegate
2.  Multi cast delegate

Single cast delegate derived from System.Delegate class. It contain a reference to one method only at a time.
Multi cast delegate is derived from System.MulticastDelegate class. Therefore you can create a single delegate that invokes multiple methods. Here it should return “void” for all the methods.
It is an ability to crate a chain of methods which are called at runtime when you create an object. So here ‘+’ operator is used to add the method to the chain and ‘-‘ operator is used to remove the method from chain.

using System;
namespace DelegateE
{
    public delegate void deladdsub(int x, int y);

    class sample
    {
        public static void add(int x, int y)
        {
            Console.WriteLine("x+y = {0}",x+y);
        }
        public static void sub(int x, int y)
        {
            Console.WriteLine("x-y = {0}", x - y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            deladdsub d1 = new deladdsub(sample.add);
            deladdsub d2 = new deladdsub(sample.sub);
            deladdsub Rdel;
            Rdel = d1;
            Rdel += d2;
            Rdel += d1;
            Rdel -= d1;
            Rdel(4, 2);
           
            Console.ReadLine();
        }
    }   
}

Working with Events and Delegates

·        An event is an action occurs such as clicks, key presses, mouse movements and system generated notifications.
·        Applications can respond to event when they occur.
·        The events are declared and raised in a class and associated with the event handlers using delegates within the same class or other class.
·        Events use the publisher and subscriber model.
·        Publisher is an object that contains the definition of the event and delegate. The association of event with delegate is also specified in the publisher class.
·        Subscriber is an object that wahts to accept the event and provide a handler to the event.
·        The delegate of the publisher class invokes the method of publisher class.

Implementation of an Event:

1.  Event definition.
Public delegate returntype delegatename();
Private event delegatename eventname;
2.  Subscribing to an event.
Eventname=new delegatename(delegatefunctionname);
3.  Firing an event.
If(eventname != null)
{
eventname();
}


using System;
namespace eventhandlier
{
    public class Publisher
    {
        public delegate void DelDisplay(int x);
        public event DelDisplay MyEvent;
        public void Display(int x)
        {
            Console.WriteLine("the x value is {0}",x);
        }
        public void RaiseEvent()
        {
            if (MyEvent != null)
            {
                MyEvent(100);
            }
            Console.WriteLine("Event Called");
        }
    }

   
    class Subscriber
    {
        static void Main(string[] args)
        {
            Publisher p = new Publisher();
            p.MyEvent += new Publisher.DelDisplay(p.Display);
            p.RaiseEvent();
            Console.ReadLine();
        }
    }
}


Comments

Popular posts from this blog

WCF interview questions

The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program

what is Event Cache table in sharepoint