CAB201

icon picker
CAB201 Method Types

C#-CAB201

Separate code blocks for each method (Sum, Subtract, Multiply, and Division) along with their respective main methods. This way, you can easily reference each specific implementation.

Sum Instance Method (No return, no parameter)

using System;
class Calculator
{
public void Sum()
{
int a = 10;
int b = 5;
Console.WriteLine($"Sum: {a + b}");
}
}
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
calc.Sum();
}
}

Sum Static Method (No return, no parameter)

using System;

class Calculator
{
public static void Sum()
{
int a = 10;
int b = 5;
Console.WriteLine($"Sum: {a + b}");
}
}

class Program
{
static void Main(string[] args)
{
Calculator.Sum();
}
}

Subtract Instance Method (With parameter, no return)


using System;
class Calculator
{
public void Subtract(int a, int b)
{
Console.WriteLine($"Difference: {a - b}");
}
}
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
calc.Subtract(20, 7);
}
}

Subtract Static Method (With parameter, no return)

using System;

class Calculator
{
public static void Subtract(int a, int b)
{
Console.WriteLine($"Difference: {a - b}");
}
}

class Program
{
static void Main(string[] args)
{
Calculator.Subtract(20, 7);
}
}

Multiply Instance Method (With parameter and return)

using System;

class Calculator
{
public int Multiply(int a, int b)
{
return a * b;
}
}

class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
int product = calc.Multiply(6, 4);
Console.WriteLine($"Product: {product}");
}
}

Multiply Static Method (With parameter and return)

using System;

class Calculator
{
public static int Multiply(int a, int b)
{
return a * b;
}
}

class Program
{
static void Main(string[] args)
{
int product = Calculator.Multiply(6, 4);
Console.WriteLine($"Product: {product}");
}
}

Division Instance Method (With return, no parameter)


using System;
class Calculator
{
public double Division()
{
double a = 10;
double b = 3;
return a / b;
}
}
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
double quotient = calc.Division();
Console.WriteLine($"Quotient: {quotient:F2}");
}
}

Static Method (With return, no parameter)

using System;

class Calculator
{
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.