Posts

Showing posts with the label Constructor

C# - Constructors in C# with Example, Types of Constructor in C# with Example

Constructor is a special method of a class which will invoke automatically whenever instance or object of class is created. Constructors are responsible for object initialization and memory allocation of its class. If we create any class without constructor, the compiler will automatically create one default constructor for that class. There is always at least one constructor in every class. Here you need to remember that a class can have any number of constructors and constructors don’t have any return type, not even void and within a class we can create only one static constructor. Generally constructor name should be same as class name. If we want to create constructor in a class we need to create a constructor method name same as class name check below sample method for constructor class   SampleA { public  SampleA() { Console .WriteLine( "Sample A Test Method" ); } } Types of Constructors Basically constructors are 5 types those ar...