Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how does Console work?

Tags:

c#

oop

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().

like image 522
LaggKing Avatar asked Dec 25 '22 19:12

LaggKing


1 Answers

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.

like image 161
MarcinJuraszek Avatar answered Jan 02 '23 17:01

MarcinJuraszek