Sobes.tech
Junior — Middle

Can you explain what is meant by a method called on a specific instance of a class?

sobes.tech AI

Answer from AI

A method called on a specific instance of a class is a function that belongs to that object and can access its internal state (fields). Such a method has access to the instance data via the this keyword.

For example, if there is a class Person with a method greet(), then calling person.greet() executes the method code for that specific object, using its properties:

class Person {
    public string Name { get; set; }
    public void Greet() {
        Console.WriteLine($"Hello, my name is {Name}!");
    }
}

Person person = new Person { Name = "Ivan" };
person.Greet(); // Will output: Hello, my name is Ivan!

Thus, an instance method works with the specific state of the object, unlike static methods which are not tied to an instance.