Subscribe to Subscribers
Talk to Webmaster Tony John

Resources » .NET programming » .NET Framework

C# Constructor - An Overview


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 10


Constructor is a special type of method which will have same name as that of the class. It is used to assign the values to variables during the Object Initialization and is Implicitly called during the Object Creation.



 


Contructor in C#


1. Special type of Method with same name as that of the Class.
2. It is Implicitely called during the Object Creation.
3. It is used to assign the values to the variable binded to the Object during the Object Creation.
4. Syntax

class A {
A() { }

A([parameters])
{ }
}

Types of Constructors


1. Default Constructor
A. Defined by the User or by the System.
B. No parameters
C. Class will have only one default constructor
Example

class A {
A() { } // Default Constructor

}

2. Parameterized Constructor
A. Defined by the User.
B. Takes Parameters
C. Class can have any number of parameterized constructors
Note: It will differ in Parameters datatype or Number of parameters.
Example:

class A {
A([parameters])
{
}
}

Methods verses Constructor


1. Methods
A. All methods will have different Names.
B. Takes Parameters and returns a value.
C. Explicitly invoked using the Objects.
2. Constructor
A. All Constructors will have same Name [Class Name].
B. Takes Parameters.
C. No Return Type
D. Implicitly Invoked.
Example
Define a class called Car with two variables brand, model and color. Define a Constructor to Assign the value and a Method to display the values.

// Example: Constructor in C#
class Car {
// variable declaration
String Brand;
String model;
double Price;

// Default Constructor(Without parameter)
public Car {
Brand = "Tata";
Model = "Nano";
Price=100000;
}

// Parameterized Constructor(With parameter)
public Car(String b, String m, double p){
Brand = b;
Model = m;
Price=p;
}
// Method to display the Car details
void showCarDetails() {
System.out.println("Brand = " + Brand);
System.out.println("Model = " + Model);
System.out.println("Price = " + Price);
}
} // End of the class definition

class CarDemo {
public static void main(String args[]) {
// Object Creation and Initialization
Car objCar1 = new Car(); // Invokes Default Constructor
objCar1.showCarDetails();

// Object Creation and Initialization
Car objCar2 = new Car("Maruthi", "Swift" 25000); // Invokes Parameterized Constructor
objCar2.showCarDetails();

}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "C# Constructor - An Overview"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How the Regular Expressions are used?
    Previous Resource: Word Automation using VB.NET
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.