Parameterized Constructor

  • A Constructor at least one parameter is called a parameterized constructor . You van initialize each instance of the class to different values.
  • A constructor with at least one parameter is called a parametrized constructor. The advantage of a parametrized constructor is that you can initialize each instance of the class to different values.

  • Programe

    using System;
    
    namespace OOP {
    	class DEMO {
    		public string s1, s2;
    		public DEMO()     // Default Constructor {
    			s1 = "hello ";
    			s2 = "I am scanftree";
    		}
    		public DEMO(string x, string y)     // Declaring Parameterized constructor with Parameters {
    			s1 = x;
    			s2 = y;
    		}
    	}
        
    	class Program {
    		static void Main(string[] args) {
    			DEMO obj = new DEMO();
    			// Default Constructor is calling.
    			DEMO obj1=new DEMO("Welcome","scanftree");
    			// Parameterized Constructor is calling
    			Console.WriteLine(obj.s1 + ", "+obj.s2);
    			Console.WriteLine(obj1.s2 +" to " + obj1.s2);
    			Console.ReadLine();
    		}
    	}