When ever an object of class is created the constructor of that class gets called and the required memory get allocated. While creating the object of a class we are explicitly calling the constructor as following
ConDemo cd = new ConDemo(); (Calling the constructor)
Constructors are of two types
- Parameter-less (zero argument) Constructor.
- Parametrized Constructor.
- Parameter-less (zero argument) Constructor: A constructor without any parameter is a parameter-less constructor which can be either defined implicitly provided there is no explicit constructor or else can be defined explicitly.
- Parametrized Constructor: A constructor with parameters is a Parametrized Constructor and can be defined only explicitly. if the constructor of a class is Parametrized values to the parameter are sent while calling the constructor.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApps { class ConParam { int a; // a is a class variable public ConParam(int a) // a is a block variable { this.a = a; } public void display() { Console.WriteLine("Values of a is{0}", a); } static void Main() { ConParam p1 = new ConParam(10); ConParam p2 = new ConParam(20); ConParam p3 = new ConParam(30); p1.display(); p2.display(); p3.display(); Console.ReadLine(); } } }
No comments:
Post a Comment