Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Using Constructors (C# Programming Guide)
In this articleWhen a class or struct is instantiated, its constructor is called. Constructors have the same name as the class or struct, and they usually initialize the data members of the new object. In the following example, a class named
A constructor that takes no parameters is called a parameterless
constructor. Parameterless constructors are invoked whenever an object is instantiated by using the Unless the class is static, classes without constructors are given a public parameterless constructor by the C# compiler in order to enable class instantiation. For more information, see Static Classes and Static Class Members. You can prevent a class from being instantiated by making the constructor private, as follows:
For more information, see Private Constructors. Constructors for
struct types resemble class constructors, but
The following code, however, causes a compiler error because it doesn't use
Alternatively, objects based on
So calling the parameterless constructor for a value type isn't required. Both classes and
This class can be created by using either of the following statements:
A
constructor can use the
In this example, the constructor for the base class is called before the block for the constructor is executed. The In a derived class, if a base-class constructor isn't called explicitly by using the
If a base class doesn't offer a parameterless constructor, the derived
class must make an explicit call to a base constructor by using A constructor can invoke another constructor in the same object by using the this keyword. Like
The use of the
Constructors can be marked as public, private, protected, internal, protected internal or private protected. These access modifiers define how users of the class can construct the class. For more information, see Access Modifiers. A constructor can be declared static by using the static keyword. Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members. For more information, see Static Constructors. C# Language SpecificationFor more information, see Instance constructors and Static constructors in the C# Language Specification. The language specification is the definitive source for C# syntax and usage. See also
FeedbackSubmit and view feedback for What happens when this is used in a constructors body to call another constructor of the same class if that call is not the first statement in the constructor?What happens when this is used in a constructor's body to call another constructor of the same class if that call is not the first statement in the constructor? A compilation error occurs.
Which keyword is used to call a constructor from another constructor?The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.
How do you call a constructor from another constructor in the same class?Within same class: It can be done using this() keyword for constructors in the same class. From base class: by using super() keyword to call the constructor from the base class.
Can a constructor call another constructor?Inside the first constructor, we have used this keyword to call the second constructor. this(5, 2); Here, the second constructor is called from the first constructor by passing arguments 5 and 2. Note: The line inside a constructor that calls another constructor should be the first line of the constructor.
|