C# Example of Instance Constructor with Parameters ( Parameterized Values)

Here is an example of instance constructor with parameters in which values are passed in the main method…
Here is the code..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication29
{
    class Calculator
    {
        static int number1, number2, total;
        Calculator(int num1, int num2)
        {
            number1 = num1;
            number2 = num2;
        }
        public void addnumber()
        {
            total = number1 + number2;
        }
        public void display()
        {
            Console.WriteLine(“the total is:{0}”, total);
        }
        static void Main(string[] args)
        {
            Calculator c1 = new Calculator(20,10);
            c1.addnumber();
            c1.display();
            Console.ReadLine();
        }
    }
}
Program is executing successfully in Microsoft Visual Basic 2008.

Add Comment