Constructors in C#

Discussion in 'C#' started by Sanskruti, Apr 27, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    If you go through any standard C# books you will see the constructors and so I would not get into the details of the constructors but what is more important to me is the use of static constructors which is somethine new in C# or you can say that its a copy of initiliazation block in Java.

    Constructors are used for initializing the members of a class whenever an object is created with the default values for initialization. If a class is not defined with the constructor then the CLR (Common Language Runtime) will provide an implicit constructor which is called as Default Constructor. A class can have any number of constructors provided they vary with the number of arguments that are passed, which is they should have different signatures.

    • Constructors do not return a value.
    • Constructors can be overloaded.
    • If a class is defined with static and Non-static constructors then the privilege will be given to the Non-static constructors.
    The following are the access modifiers for constructors,

    • Public : A constructor that is defined as public will be called whenever a class is instantiated.
    • Protected : A constructor is defined as protected in such cases where the base class will initialize on its own whenever derived types of it are created.
    • Private : A constructor is defined as private in such cases whenever a class which contains only static members has to be accessed will avoid the creation of the object for the class.
    • Internal : An internal constructor can be used to limit concrete implementations of the abstract class to the assembly defining the class. A class containing an internal constructor cannot be instantiated outside of the assembly.
    • External : When a constructor is declared using an extern modifier, the constructor is said to be an external constructor.

    Types of Constructors


    • Static : Used for initializing only the static members of the class. These will be invoked for the very first time the class is being loaded on the memory. They cannot accept any arguments. Static Constructors cannot have any access modifiers.
      Code:
      Static ClassName()
      {
      	//Initialization statements;
      }
    • Non-Static : are used for initializing the Non-Static and Static members of a class. These will be invoked everytime a new object is defined for a class.
      Code:
       
      Public ClassName([argsInfo])
      {
      	//Initialization statements;
      }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I would add to the following types of constructors as follows

    • Public : A constructor that is defined as public will be called whenever a class is instantiated. Also the instantiation of the class can be from anywhere and from any assemblies.
    • Protected : A constructor is defined as protected in such cases where the base class will initialize on its own whenever derived types of it are created. In this case class object can only be created in the derived classes.
    • Private : A constructor is defined as private in such cases whenever a class which contains only static members has to be accessed will avoid the creation of the object for the class. The object of the class cannot be created and is used when any single instance of the class is needed and we have a static property or method to get the instance of the class.

    Constructor Overloading



    C# supports overloading of constructors, which means, we can have constructors with different sets of parameter signature. As an example :
    Code:
    public class MyClass
    {
        public MyClass()
        {
            // This is the default constructor method.
        }
    
        public MyClass(int no)
        {
            // This is the constructor with one parameter.
        }
    
        public MyClass(int no, string Name)
        {
            // This is the constructor with two parameters.
        }
    }

    Calling other constructors



    You can call the other constructor like this :
    Code:
    public class MyClass
    {
        public MyClass(): this(10)  // We are calling the one parameter constructor.
        {
            // This is the default constructor method.
        }
    
        public mySampleClass(int Age) 
        {
            // This is the constructor with one parameter.
        }
    }
    Now instead of calling the same class constructor you can also call the base class constructor using the base keyword. Initialization List is not possible to have any variable name in C# as it was the case with C++ and we can only have this or base.
     
  3. rekha11

    rekha11 New Member

    Joined:
    Jan 31, 2008
    Messages:
    13
    Likes Received:
    3
    Trophy Points:
    0
    Constructor are same as the class name.Constructors will be automatically invoked whenever the object is created.Constructor have no return value .
    constuctors are:
    overloaded constructors
    static constructors
    copy constructor
     
  4. alcheringa

    alcheringa New Member

    Joined:
    May 30, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    copy constructors are needed to be defined explicitly. and it is not a matter of preference for non static versus static when a class is intialised. a class may be say both static and insitialised methods, it's during the usage of the static methods, the class calls static constructor.

    even if a class has a static constructor and there's no constructor defined and we intiliased with the "new" keyword, static constructor would never be called.
     
  5. juliaandrews

    juliaandrews New Member

    Joined:
    May 12, 2011
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    In c# 5 access modifiers for constructors:-
    Public
    Protected
    Private
    Internal
    External
    Thanks
     
  6. donor

    donor Banned

    Joined:
    Jan 12, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    web developer
    Location:
    india
    Code:
    include <iostream>
    using namespace std;
    
    class CRectangle {
        int x, y;
      public:
        void set_values (int,int);
        int area () {return (x*y);}
    };
    
    void CRectangle::set_values (int a, int b) {
      x = a;
      y = b;
    }
    
    int main () {
      CRectangle rect;
      rect.set_values (3,4);
      cout << "area: " << rect.area();
      return 0;
    }
    
    Is there any error in program???
     
    Last edited by a moderator: Jan 12, 2012

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice