C# code for demonstrating ternary operator within return statement

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 640
public class BasicMathsCalculator
{
public int BasicArithmeticOperation(int a , int b, string @operator)
{
return @operator == "/" ? (a / b) :
@operator == "*" ? (a * b) :
@operator == "-" ? (a - b) :
(a + b);
}
}

Here we are using ternary operator within return statement. If @operator = "/" then perform division, if @operator = "*" perform multiplication, if @operator = "-" perform
subtraction else if nothing is specified, perform addition.

Comments or Responses

Login to post response