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();
        }
    }
}


No comments:

Post a Comment