Junior — Middle
Kas saaksite selgitada, mis on staatiline meetod objektorienteeritud programmeerimises?
sobes.tech AI
Vastus AI-lt
A static method is a class method that belongs to the class itself, not to a specific instance of the class. It can be called without creating an instance of the class. In C#, static methods are declared with the keyword static.
Features of static methods:
- They do not have access to the class's non-static fields and methods because they are not associated with a specific object.
- They can be used for operations that do not depend on the state of an object.
Example:
class Calculator
{
public static int Add(int a, int b)
{
return a + b;
}
}
// Calling a static method without creating an object
int result = Calculator.Add(5, 3);