Part 9 - Constructor in C# | Oops Tutorials in C# | Oops Concept

 •A constructor is a specialized function that is used to initialize fields. A constructor has the same name as the class.

•Type of constructor 1.Default Constructor 2.Parameterise Constructor 3.Copy Constructor 4.Static Constructor 5.Private Constructor •1. Default Constructor. •A Constructor without any parameters called Default Constructor. •Drawback of default constructor is every instance of the class will be initialized to same values. •2. Parameterized Constructor. •A Constructor with at least one parameter called Parameterized Constructor. •In parameterized constructor we can initialize each instance of the class to different values. •3. Copy Constructor. • A constructor which creates an object by copying variables from another object is called Copy constructor. •4. Static Constructor. •When we declare constructor as static it will be invoked only once for any number of instances of the class. It will execute during the first instance of the class. •Note 1.Static Constructor will not accept any parameters. Because it is automatically called by CLR. 2.Static constructor will not have any access modifiers. 3.Only one static constructor is allowed. •5. Private Constructor. •Private constructor is a special constructor used in a class that contains static member only. •If a class has one or more private constructor and no public constructor then we can’t create the object of the class & also it cannot be inherit by other class. •The main purpose of creating private constructor is used to restrict the class from being instantiated when it contains every member as static. •When to use of Private Constructor •To Create Helper Class To Create Common Routine Function class To Create Utilities #OopsConcept #Oops #OopsTutorials



Share this

Related Posts

Previous
Next Post »