A Simple Calculator Class in C# 6

This article is based on the original article that was previously written using older version of Visual Studio. You can find the original article on the given below link:

This article shows how to create a new class and call its member functions from Main program. Calculator is a class which has some member functions such as square, add, subtract, multiply, division etc.

ClassLibrary:

  • We can create a class library (in other words DLL file) of code containing classes and methods.

  • One DLL file can be used in many applications in .NET like Console Application, Windows Application and Web Application and so on.

  • To use a DLL file in an application we need to add a reference to the DLL file.

  • DLL is not a self-executable file instead it is to be shared by many applications for code reusability purposes.

Procedure to Create a Class Library:

  • Start a new project in Visual Studio from the New Project dialogue box, select Class Library Template from the Project Template Window.

  • In the Name box, type a name for your Class Library. Then click OK.

  • When you click OK, it will create a namespace with the name we gave for the Library and you will see the following in the code window:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6.   
    7. namespace ClassLibrary1   
    8. {  
    9.     public class CalCulator {}  
    10. }  

See the file Class1.cs in the Solution Explorer as given below:

class library
Now add the following methods to the Calculator to do arithmetic operations (Square, Addition, Subtraction, Multiplication and Division).

We've added five simple methods; one for Square, second for addition, third for subtraction, fourth for multiplication, and fifth for Division. Similarly, we can add more methods to CalCulator for other operations.

  1. public class CalCulator  
  2. {  
  3.     // Square function   
  4.     public int Square(intnum)  
  5.     {  
  6.         returnnum * num;  
  7.     }  
  8.     // Add two integers and returns the sum  
  9.     public int Add(int num1, int num2)  
  10.     {  
  11.         return num1 + num2;  
  12.     }  
  13.   
  14.     // Multiply two integers and retuns the result  
  15.     public int Multiply(int num1, int num2)  
  16.     {  
  17.         return num1 * num2;  
  18.     }  
  19.     // Subtracts small number from big number  
  20.     public int Subtract(int num1, int num2)  
  21.     {  
  22.             if (num1 > num2)  
  23.             {  
  24.                 return num1 - num2;  
  25.             }  
  26.   
  27.             return num2 - num1;  
  28.   
  29.         }  
  30.         //performing Division on two float variables.  
  31.     public float Division(float num1, float num2)   
  32.     {  
  33.         return num1 / num2;  
  34.     }  
  35.   
  36. }  
This shows how to use CalCulator class from Main C# program:
  1. using ClassLibrary1;  
  2.   
  3. namespace ConsoleApplication2  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             {  
  10.                 CalCulator sq = new CalCulator();  
  11.                 Console.WriteLine(sq.Square(12));  
  12.                 Console.WriteLine(sq.Add(8, 63));  
  13.                 Console.WriteLine(sq.Multiply(5, 8));  
  14.                 Console.WriteLine(sq.Subtract(10, 42));  
  15.                 Console.WriteLine(sq.Division(3, 4));  
  16.                 Console.ReadLine();  
  17.             }  
  18.         }  
  19.     }  
  20. }  
Output:

output

To read other articles on the Calculator, please click on the following references link:

Up Next
    Ebook Download
    View all
    Learn
    View all