-
Key Features and
Create a new project in Visual Studio 2022 - Naming Standards & Comments
- Data Types
- Variables and Constant Variable
- Conditional Statement
- Switch Conditional Statement
- Arithmetic, Comparison, Assignment and Logical Operators
- For, While and Do While Loops
- Importance of Break and Continue Keywords
- Strings and Important String Methods
- Single Dimensional Array
- Multi Dimensional Array
- Methods and Method Overloading
- Math Class and Functions
- Casting and Type Conversions
- Class and Objects
- Constructor, Parameter less and Parameterized Constructor
- Single, Multi level and Hierarchical Inheritance
- Compile Time Polymorphism, Method and Operator Overloading
- Run-time Polymorphism and Importance of base keyword
- Abstract Class and Method
- Interface and Multiple Inheritance through Interfaces
Agenda
- Constructor in C#
- Parameter less Constructor
- Parameterized Constructor
- Static Constructor
- Private Constructor
- Constructor Overloading
Constructor in C#
- Constructor is a block of codes similar to the method. It is called when an instance of the object is created, and memory is allocated for the object.
- A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. Like methods, a constructor also contains the collection of instructions that are executed at the time of Object creation. It is used to assign initial values to the data members of the same class.
- The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing C# code.
- Every time an object is created using new() keyword, at least one constructor is called. It calls a default constructor.
- C# constructor can be public or private. A class can have multiple overloaded constructors but can not be overridden.
- Constructor(s) of a class must has name as the class name in which it resides.
- A Constructor must have no explicit return type.
- C# compiler distinguish between a method and a constructor by its name and return type. In C#, a constructor has same name as that of the class and doesn’t return any value.
- If a constructor is private, user cannot create objects of the class. Hence, all fields and methods of the class should be declared static, so that they can be accessed using the class name.
C# Constructor Example
Example:
class Test {
Test() {
// constructor body
}
} //Here, Test() is a constructor; it has same name as that of the class and doesn’t
have a return type.
class Test1 {
void Test1() {
// method body
}
} //Here, Test1() has same name as that of the class. However, it has a return type
void. Hence, it’s a method not a constructor.
Test obj = new Test();
// At this time the code in the constructor will be executed
C# Constructor Overloading
User can create two or more constructor in a class. It is known as constructor overloading. Based on the number of the argument passed during the constructor call, the corresponding constructor is called.
Constructor Overloading:
public class Bank
{
public Bank() {
Console.WriteLine(“Bank Constructor”); // parameterless constructor
}
public Bank(String bankName, double interestRate, double depositRate)
{
Console.WriteLine(bankName + ” ” + interestRate +” “+depositRate); // parameterized constructor
}
}
static void Main()
{
Bank b1 = new Bank();
Bank b2 = new Bank();
Bank b3 = new Bank(“ICICI”, 8.5, 6.5);
Bank b4 = new Bank(“SBI”, 8.25, 6.25);
}
public class Bank
{
public Bank() {
Console.WriteLine("Bank Constructor"); // parameterless constructor
}
private Bank(String bankName)
{
Console.WriteLine("Bank Constructor" + bankName); // private constructor
}
static Bank()
{
Console.WriteLine("Bank Static Constructor"); // static constructor User cannot call a static constructor directly. However, when they call a regular constructor, the static constructor gets called automatically.
}
public Bank(String bankName, double interestRate, double depositRate)
{
Console.WriteLine(bankName + " " + interestRate +" "+depositRate); // parameterized constructor
}
}
class Program
{
int i;
double d;
string s;
static void Main()
{
Bank b1 = new Bank();
Bank b2 = new Bank();
Bank b3 = new Bank("ICICI", 8.5, 6.5);
Bank b4 = new Bank("SBI", 8.25, 6.25);
// Bank b5 = new Bank("HDFC"); //Error CS0122 'Bank.Bank(string)' is inaccessible due to its protection levelProgram obj = new Program(); //User have not created any constructor in the Program class. However, while creating an object, they are calling the constructor. C# automatically creates a default constructor. The default constructor initializes any uninitialized variable with the default value.
Console.WriteLine(obj.i);
Console.WriteLine(obj.i);
Console.WriteLine(obj.s);