Parameterized constructor definition with example in C#.Net

Parameterized constructor:

We can write one or more constructors for a class, but we have to pass the parameters to the constructor while object creating for the class. Below example shows, BaseClass is having two constructors. one is with out parameters and another one is with parameters. This kind of arrangement we can call as constructor overloading.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ParameterizedConstructor
{
    class Program
    {
        static void Main(string[] args)
        {
            BaseClass obj = new BaseClass();  //output: no parameters
            BaseClass obj1 = new BaseClass("ranga"); //output: with one parameter ranga
            Console.Read();
        }
    }

    class BaseClass
    {
        public BaseClass()
        {
            Console.WriteLine("no parameters");
        }

        public BaseClass(string s)
        {
            Console.WriteLine("with one parameter "+s);
        }

    }

}

Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar