Example of basic Arithmetic Operation using Params Array in C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 773
public class ParamArithmeticOperation

{
static void Main()

{
//calling function

PerformArithmeticOperation(200,100,"+"); //perform addition

PerformArithmeticOperation(200,100,"-"); //perform subtraction

PerformArithmeticOperation(200,100,"*"); //perform multiplication

PerformArithmeticOperation(200,100,"/"); //perform division
}

//called function
public static void PerformArithmeticOperation(params object[] items)
{

var result = 0;

switch (Convert.ToString(items[2])) //items[2] is the operator(s) i.e. +,-,*,/
{
case '+': result = Convert.ToInt32(items[0]) + Convert.ToInt32(items[1]); break;
case '-': result = Convert.ToInt32(items[0]) - Convert.ToInt32(items[1]); break;
case '*': result = Convert.ToInt32(items[0]) * Convert.ToInt32(items[1]); break;
case '/': result = Convert.ToInt32(items[0]) / Convert.ToInt32(items[1]); break;

default: Console.WriteLine("No such operation available");
}
Console.WriteLine(result);
}
}

Comments or Responses

Login to post response