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

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)

Related

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.

C# how does Console work?

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.

instance of a class and derived classes can change a static member of the class indirectly through a non-static method in c#

public class Class1
{
public static string Name="foo";
public void ChangeName(string _name)
{
Name=_name;
}
}
in some other class..
Class1 _c=new Class1();
_c.ChangeName("bar");
and the Name gets changed.. an instance changing a static member!
I thought a static member is available only for a Class. If a Class wants it can change its static members.
But here an instance is able to change it indirectly.Shouldn't an instance not be able to change it? When we create an instance it occupies its own space in the heap without having access to the static members of the Class. So what is really happening here?
The important part is
public static string Name="foo";
which creates a changeable static property. This allows for
Class1.Name="bar"
from outside the class as well as from inside the class - inside the class you can shorten this to
Name="bar"
which is exactly what is called via ChangeName("bar")
You might want to consider
public static readonly string Name="foo";
EDIT
If you want to be able to change the value only from within static methods, consider using a setter/getter construct with appropriate setter logic.
No, an instance is not kept from accessing static members. Instances are separate from each other, but static members are available both to static methods and instance methods.
As you have made it public, it's not even only methods in the class itself that can access it. You can change it from anywhere:
in some other class...
Class1.Name = "Albert";
MSDN clearly states :
"While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field."

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