C# Example of Instance Constructor Input taken from User

C Sharp example to show how to take input from the user using an instance constructor…
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)
        {
            int var1, var2;
            Console.WriteLine(“Enter the two numbers”);
            var1=Convert.ToInt32(Console.ReadLine());
            var2=Convert.ToInt32(Console.ReadLine());
            Calculator c1=new Calculator(var1,var2);
            c1.addnumber();
            c1.display();
            Console.ReadLine();
        }
    }
}
Programming is running successfully in Visual Basic 2008. For any query Please Comment.

Add Comment