top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is a chain Constructor?

+1 vote
265 views
What is a chain Constructor?
posted Nov 26, 2014 by Manikandan J

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Chain Constructor:

Constructor Chaining is an approach where a Constructor calls another Constructor in the
same or base class.

Example of chain Constructor code snippet

 namespace ConstructoreChaining  
  {
 class A
    {
     public A()
      {
        Console.WriteLine("ConstructorExampleExample A.");
      }

    public A(string s){

        Console.WriteLine("ConstructorExample A with parameter = {0}",s);

    }

    public A(string s,string t)
     {
        Console.WriteLine("ConstructorExample A with parameter = {0} & {1}", s,t);
    }
}

class B:A
{
    public B():base()
     {
        Console.WriteLine("ConstructorExample B.");
     }

    public B(string s):base(s)
     {
        Console.WriteLine("ConstructorExample B with parameter = {0}", s);
    }

    public B(string s, string t):base(s,t)
      {
        Console.WriteLine("ConstructorExample B with parameter = {0} &{1}", s, t);
      }
  }

class Program
{
    static void Main(string[] args)
    {
        B b1 = new B();
        B b2 = new B("First Parameter ", "Second Parameter");
        Console.Read();
       }
   }
}
answer Nov 26, 2014 by Jdk
Similar Questions
+1 vote
using System;

namespace CareerRideTest
{    
  class A
  {    
    public A()
    {
      Console.WriteLine("I am in A");
    }
  }

  class B : A
  {
    public B()
    {
      Console.WriteLine("I am in B");
    }
  }

  class C : B
  {
    static C()
    {
      Console.WriteLine("I am in Static C");
    }
    public C()
    {
      Console.WriteLine("I am in C");
    }
  }    

  class MainClass
  {
    static void Main(string[] args)
    {
      C obj = new C();    
      Console.ReadKey();    
    }
  }    
}
+4 votes

Why can’t we use a static class instead of singleton?

...