In this tutorial, we'll be exploring the concepts of static and instance methods in C#. The goal is to understand the differences between these two types of methods and learn when to use each.
By the end of this tutorial, you will:
- Understand what static and instance methods are,
- Learn how to declare and use each type of method,
- Know when to use static or instance methods.
Prerequisites:
- Basic knowledge of C# programming language,
- Familiarity with object-oriented programming concepts.
Static methods belong to the class itself and not to any instance of the class. They're declared with the keyword static
and can be called directly on the class without creating an instance of the class.
Example:
public class MyClass {
public static void MyMethod() {
// Method body
}
}
// Call the method
MyClass.MyMethod();
Tips for using static methods:
- Use static methods when the method does not depend on instance variables.
- Static methods can't access non-static class members.
Instance methods are associated with an instance of a class. They can access instance variables and change their state. Instance methods are declared without the static
keyword.
Example:
public class MyClass {
public void MyMethod() {
// Method body
}
}
// Create an instance and call the method
MyClass myObject = new MyClass();
myObject.MyMethod();
Tips for using instance methods:
- Use instance methods when the method needs to access or modify instance variables.
public class Calculator {
public static int Add(int a, int b) {
return a + b; // returns the sum of a and b
}
}
// Call the method
int result = Calculator.Add(5, 3);
Console.WriteLine(result); // Prints: 8
public class Calculator {
private int _result = 0;
public void Add(int number) {
_result += number; // adds the number to the _result
}
public int GetResult() {
return _result; // returns the current _result
}
}
// Create an instance and call the methods
Calculator calculator = new Calculator();
calculator.Add(5);
calculator.Add(3);
Console.WriteLine(calculator.GetResult()); // Prints: 8
In this tutorial, we've learned about static and instance methods in C#. We found out that static methods are associated with the class itself, while instance methods are associated with an instance of the class.
For further learning, explore more about object-oriented programming in C#, including classes, objects, and properties.
Create a Dog
class with a static method Bark()
which prints "Woof!" and an instance method Eat()
which prints "Yum!".
Create a Car
class with a static method NumberOfWheels()
which returns 4 and an instance method Drive()
which prints "Vroom!".
Solutions:
public class Dog {
public static void Bark() {
Console.WriteLine("Woof!");
}
public void Eat() {
Console.WriteLine("Yum!");
}
}
// Call the static method
Dog.Bark(); // Prints: Woof!
// Call the instance method
Dog myDog = new Dog();
myDog.Eat(); // Prints: Yum!
public class Car {
public static int NumberOfWheels() {
return 4;
}
public void Drive() {
Console.WriteLine("Vroom!");
}
}
// Call the static method
int wheels = Car.NumberOfWheels();
Console.WriteLine(wheels); // Prints: 4
// Call the instance method
Car myCar = new Car();
myCar.Drive(); // Prints: Vroom!
Keep practicing with different scenarios to get a better understanding of when to use static and instance methods.