Constructor definition with example in C#.Net | C# Parameterize constructor with an example

A method which should be call by creating an object for the class called as constructor and constructor name should be the class name.

In the below example ConstructorExample is class having two constructors. one is ConstructorExample() and ConstructorExample(int a, int b). This is called as constructor overloading, means name method name with different arguments.

If a constructor is having only ConstructorExample(int a, int b) method. that time we have to use these parametes which creating and object like ConstructorExample obj = new ConstructorExample(3, 4);

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

namespace OOPSConcepts
{
    class Program
    {
        static void Main(string[] args)
        {
            ConstructorExample obj = new ConstructorExample();

            obj = new ConstructorExample(3, 4);

            Console.Read();

        }
    }
    public class ConstructorExample
    {
        internal ConstructorExample()
        {
            Console.WriteLine("this is constructor");
        }

        internal ConstructorExample(int a, int b)
        {
            Console.WriteLine(a + b);
        }

    }
}

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