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
{
public static double Division()
{
double a = 10;
double b = 3;
return a / b;
}
}

class Program
{
static void Main(string[] args)
{
double quotient = Calculator.Division();
Console.WriteLine($"Quotient: {quotient:F2}");
}
}

When to Use Static Methods

Utility Functions: If a method performs a task that does not require any data from an instance of the class, it can be static. Examples include mathematical calculations or string manipulations.
public static int Add(int a, int b)
{
return a + b;
}
Factory Methods: Methods that create instances of the class can be static, as they do not need an existing instance to function.
Entry Points: The Main method in a C# application is static because it serves as the entry point for the program and does not require an instance to start execution.
Shared Data: When a method needs to operate on static data shared across all instances of the class, it should be static.

When Not to Use Static Methods

Instance Data Access: If a method needs to access or modify instance variables (non-static fields), it cannot be static because static methods do not have access to instance data.
public class Counter
{
private int count = 0;

public void Increment()
{
count++;
}
}
Polymorphism: Static methods cannot be overridden in derived classes, so if you need polymorphic behavior (where a derived class provides a specific implementation of a method), you should use instance methods.
Interface Implementation: Static methods cannot be part of an interface, as interfaces define instance methods that must be implemented by classes.
Encapsulation: If a method is closely tied to the state of an object and is part of its behavior, it should be an instance method to encapsulate that behavior within the object.

Summary

Static Methods: Use when the method does not need to access instance-specific data or behavior. They are ideal for utility functions and operations that are independent of object state.
Instance Methods: Use when the method needs to interact with instance-specific data or when polymorphic behavior is required. They encapsulate functionality that is specific to an object's state.
Choosing between static and instance methods depends on the design and requirements of your application. Use static methods for operations that do not depend on object state and instance methods when you need to work with specific object data.
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.