This is a pretty basic question,
But how does, for example
Console.Write("test");
work?
Console is a class, not an object.
I'm using console as an example because it is commonly used, but I've seen many examples of using Class.method() instead of object.method().
That method is called static method: Static Classes and Static Class Members (C# Programming Guide).
You don't need an instance to call static class member:
A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With