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)
usingSystem;
classCalculator
{
publicvoidSum()
{
int a =10;
int b =5;
Console.WriteLine($"Sum: {a + b}");
}
}
classProgram
{
staticvoidMain(string[] args)
{
Calculator calc =newCalculator();
calc.Sum();
}
}
Sum Static Method (No return, no parameter)
usingSystem;
classCalculator
{
publicstaticvoidSum()
{
int a =10;
int b =5;
Console.WriteLine($"Sum: {a + b}");
}
}
classProgram
{
staticvoidMain(string[] args)
{
Calculator.Sum();
}
}
Subtract Instance Method (With parameter, no return)
usingSystem;
classCalculator
{
publicvoidSubtract(int a,int b)
{
Console.WriteLine($"Difference: {a - b}");
}
}
classProgram
{
staticvoidMain(string[] args)
{
Calculator calc =newCalculator();
calc.Subtract(20,7);
}
}
Subtract Static Method (With parameter, no return)
usingSystem;
classCalculator
{
publicstaticvoidSubtract(int a,int b)
{
Console.WriteLine($"Difference: {a - b}");
}
}
classProgram
{
staticvoidMain(string[] args)
{
Calculator.Subtract(20,7);
}
}
Multiply Instance Method (With parameter and return)
usingSystem;
classCalculator
{
publicintMultiply(int a,int b)
{
return a * b;
}
}
classProgram
{
staticvoidMain(string[] args)
{
Calculator calc =newCalculator();
int product = calc.Multiply(6,4);
Console.WriteLine($"Product: {product}");
}
}
Multiply Static Method (With parameter and return)
usingSystem;
classCalculator
{
publicstaticintMultiply(int a,int b)
{
return a * b;
}
}
classProgram
{
staticvoidMain(string[] args)
{
int product = Calculator.Multiply(6,4);
Console.WriteLine($"Product: {product}");
}
}
Division Instance Method (With return, no parameter)
usingSystem;
classCalculator
{
publicdoubleDivision()
{
double a =10;
double b =3;
return a / b;
}
}
classProgram
{
staticvoidMain(string[] args)
{
Calculator calc =newCalculator();
double quotient = calc.Division();
Console.WriteLine($"Quotient: {quotient:F2}");
}
}
Static Method (With return, no parameter)
usingSystem;
classCalculator
{
publicstaticdoubleDivision()
{
double a =10;
double b =3;
return a / b;
}
}
classProgram
{
staticvoidMain(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.
publicstaticintAdd(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.
publicclassCounter
{
privateint count =0;
publicvoidIncrement()
{
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.