menu

Sunday, September 1, 2013

Constructor in C#

using System;
namespace Constructor
{
    public abstract class A
    {
        //Static cunstructor con't have access modifire and argument
        //so only one static constructor can be created
        //public static A(int a)
        static A()
        {
            Console.WriteLine("A:A()");
        }
       
    }
    public class C : A
    {
        public C(int a)
        {
            Console.WriteLine("A:C(int a)");
        }
        static C()
        {
            Console.WriteLine("A:C()");
        }
    }
    //A class with both static and non static constructor, can't be inharited
    //public class D : C
    //{

    //}
    public static class C1 // : C // static class can't derive from any class
    {
        public static void Fun()
        {
            Console.WriteLine("A:Fun()");
        }
    }
    public class B
    {
        //private and protected constructor is not accessable
        public B()
        {
            Console.WriteLine("B:B()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //A o = new A();//Abstract class can't be initialize
            C a = new C(1);
            B b = new B();
           
            Console.ReadKey();
        }
    }
}


Wednesday, August 28, 2013

Inheritance in C#

using System;
namespace Inheritance
{
    interface I1
    {
        void Fun();
    }
    public abstract class A
    {
        // Implemented function can also be marked as virtual
        public virtual void Fun()
        {
            Console.WriteLine("I1::Fun()");
        }
        public abstract void Fun1();
        public virtual void Fun2()
        {
            Console.WriteLine("C1::Fun()");
        }
    }
    public class C1 : A
    {
        // Overriding abstract function of class A
        public override void Fun1()
        {
            Console.WriteLine("C1::Fun1()");
        }
        // Overriding virtual function of class A
        public override void Fun2()
        {
            Console.WriteLine("C1::Fun2()");
        }
    }
    public class C2 : C1
    {
        // Overriding virtual function of class A
        public override void Fun()
        {
            Console.WriteLine("C2::Fun()");
        }
        // Overriding abstract function of class A
        public override void Fun1()
        {
            Console.WriteLine("C2::Fun1()");
        }
        // Overriding virtual function of class A
        public override void Fun2()
        {
            Console.WriteLine("C2::Fun2()");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Console.ReadKey();
        }
    }
}


Implement Two Interfaces With Same Method Name in C#

using System;
interface I1
{
    void Fun();
}
interface I2
{
    void Fun();
    void Fun1();
}
public class C : I1, I2
{
    public void Fun()  
    {
        Console.WriteLine("I1::Fun()");
    }
    /* This function will be implemented as private
     * because one more implementation with same name
     * is already from another base type(I1)*/
    void I2.Fun()
    {
        Console.WriteLine("I2::Fun()");
    }
    public void Fun1()
    {
        Console.WriteLine("I2::Fun1()");
    }
}
public class A
{
    public static void Main()
    {
        I1 o1 = new C();
        o1.Fun();            // Of I1

        I2 o2 = new C();
        o2.Fun();            // Of I2
        o2.Fun1();           // Of I2
       
        C o = new C();
        o.Fun();             // Of I1
        o.Fun1();            // Of I1

        Console.ReadKey();
    }

}

Sunday, August 25, 2013

Generic in C#

Generic is a term which enables us to design classes or methods which differ as per types until it is declared and instantiated. For exp …

using System;
namespace Generic
{
    class Program
    {
        public static void Main()
        {
            B<int> obj = new B<int>();
            System.Console.WriteLine(obj.Compare(2, 6));

            B<string> obj1 = new B<string>();
            System.Console.WriteLine(obj1.Compare("KUNAL", "MAURYA"));

            B<char> obj2 = new B<char>();
            System.Console.WriteLine(obj2.Compare('X', 'X'));

            ComplexNumber o1 = new ComplexNumber(2, 3);
            ComplexNumber o2 = new ComplexNumber(4, 8);
            B<ComplexNumber> obj3 = new B<ComplexNumber>();
            System.Console.WriteLine(obj3.Compare(o1, o2));

            Console.ReadKey();
        }
    }
    public class B  // Generic Class for all types having Equals method
    {
        public string Compare(myType a, myType b)
        {
            if (a.Equals(b))
            {
                return "Equal";
            }
            else
            {
                return "Not-Equal";
            }
        }
    }
    public class ComplexNumber
    {
        int a;
        int b;
        public ComplexNumber(int x, int y)
        {
            a = x;
            b = y;
        }
        public bool Equals(ComplexNumber z)
        {
            if (this.a.Equals(z.a) && this.b.Equals(z.b))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}