C# how does Console work? - c#

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.

Write is a static method, so you invoke it on the type instead of on an instance. See documentation.

Write is a static method in the Console class, not an instance method.

Console.Write is a static method which writes to the console without adding a new line.

Related

why can't I access other members from Main() method?

I was told any method can access any other class members within same class by directly calling their names. But this is not true for Main method. You can't access other members unless you instantiate this class. Very inconsistent.
That's not because it's the Main method but because it's a static method. Static methods cannot access non-static ones. That goes for every single static method, not just the Main one. It's because static methods are defined for the class while the non-static methods are defined for an instance of the class (which static methods have no idea about)

Do C# classes default to instance or static classes if not specified?

When declaring a class in C# without specifying if the class is a static or non-static class which will it default to?
EDIT: Here's an article I wrote based on this discussion.
https://hackernoon.com/c-static-vs-instance-classes-and-methods-50fe8987b231
If you don't declare the class as static then its members can be either static or non-static.
A static class can only have static members.
You can invoke non-static members only on instances of the class.
You can invoke static members only on the class itself.
Also, in your class declaration, there are no parentheses.
If the "static" is not specified, it will require an instance of the class to be used (Unless the member itself is specified as static).
If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable.
From: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes
A class is always an instance class unless you specify otherwise

Does Static class create an instance? msdn says i doesn't, but why constructor then?

I came across trying to understand the difference between singleton object and static class
and the simplest information I see everywhere is that static class DOES NOT create an instance while singleton requires to
But why the hell can I have the static constructor from static class? what does that mean? doesn't it create an instance?
if you run simple code with breakpoint on static class constructor you will see it reaches it
I'm confused, anybody?
But why that hell can I have the static constructor from static class? what does that mean? doesn't it create an instance?
No. The static constructor allows you to initialize static members of the class (basically, the static state for that class).
With the singleton pattern, the static constructor (or a static inline initializer) often creates an instance, but that instance is still created via the normal, non-static constructor. It's then stored within a static variable (the single "instance" variable).
Lazy-initialized singletons will avoid that, and initialize the static variable on demand.
A static class is a different thing - a static class will never work as a singleton, since you can't create an instance of a static class. Static classes are specifically intended to be used when you will never create an instance.
A singleton will (typically) be created by having a non-static class, but using a private constructor (so the instance can only be created within that class). There will be a static property used to retrieve a single instance of that class. The class instance will either be created on demand or within the static constructor.
The static constructor is just a place where you can initialise your static member variables. You don't need a static constructor - you can initialise the static member variables inline instead, but I think it's tidier to put them in the static constructor.
Remember that even though you don't instantiate the static class, you do instantiate its static members, and it's nice to have a place to do it. This is an improvement on the way you have to do it in C++.
Note that your class doesn't need to be static in order to have a static constructor. You can have a non-static class that provides both an ordinary constructor and a static constructor. Same rule applies.

Extension methods on a static object

I know (or so I hear) that writing extension methods for a single stand alone .net class (not an implementation of IEnumerable) is potential code smell. However, for the sake of making the life easier I need to attach a method to the ConfigurationManager class in asp.net. It's a static object so this won't work:
public static List<string> GetSupportedDomains(this ConfigurationManager manager)
{
//the manager needs to be static.
}
So the question is - is it possible to write an extension method for a static class in .net?
No, it isn't possible.
They are defined as static objects that appear to be instance methods.
From MSDN:
Extension methods are defined as static methods but are called by using instance method syntax.
No you can not. Extension methods require an instance of a given type, which you cannot get from a static class.

Can we have a private constructor in a static class?

I have a doubt that a static class can contain a private constructor.
Static classes cannot have instance constructors
http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
The following list provides the main features of a static class:
Contains only static members.
Cannot be instantiated.
Is sealed.
Cannot contain Instance Constructors.
A static class cannot have any instance constructor ( see CS0710 ), whether it be public, private, protected or internal.
See the following article for more info.
Static Classes and Static Class Members (C# Programming Guide)
What would this constructor do? The class is static, so it is never instantiated. You can have a static constructor on a non-static class to initialize static fields, but on a static class, the only constructor that makes sense is the static constructor, and that gets called be the CLR.
Addition: Jon Skeet posted an article about the timing of the initialization of the static class (normally it's initialized on first use, but sometimes you want to initialize it when the program starts) and a possible change in .net 4.
Your doubt is correct.
A static class can only have a static constructor and public/private does not apply since your code can never call this constructor (the CLR does).
So you may not use a access modifier (public/private/...) on a static constructor.
rule is static classes cannot have instance constructors

Categories