Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of static in differents parts of the code?

Tags:

c#

I have been learning C# for two weeks now, though it is not my first either second language. I have been wondering about the static word. I know I should have researched about this word long before...but this is the first time I realized how much confusing this word is for me. For what I have read:

A static class is a class which does not need to be instanciated to be used ( Class with single method -- best approach? ). This may have some advantages and some disadvanatges regarding testing, polymorphism etc.

But the static word can be applied also to classes, fields, methods, properties, operators, events and constructors !!! ( https://msdn.microsoft.com/en-us/library/98f28cdx%28v=vs.80%29.aspx ). Example:

Property:

private static string s  = "";

Method:

public static void helperMethod() {
        Console.WriteLine("Whatever");
    }

Does the word static have a global meaning or employed in differents parts of the code the meaning can change?

like image 861
WristMan Avatar asked Oct 27 '25 00:10

WristMan


1 Answers

class modifier

When static is applied to a class it indicates four attributes.

  • Contains only static members.
  • Cannot be instantiated.
  • Is sealed.
  • Cannot contain Instance Constructors.

property or function modifier (and events and methods)

When applied to properties and functions, e.g.

public Thing
{
    static int SharedCount { get; set; }

    static string GenerateName()
    {
        // ...
    }
}

it means that the properties and functions will be accessible via the type name, without instantiating the class. This properties and functions will not be accessible via an instance of the class. So, both

var i = Thing.SharedCount;
var s = Thing.GenerateName();

Are valid and correct statements, Where as

var i = new Thing().SharedCount;
var s = this.GenerateName();

are both incorrect.

Code in functions and properties declared with the static modifier cannot access non-static members of the class.


member variables

Member variables declared with a static modifier, e.g.

class Thing
{
    private static int sharedCount = 0;
    private static readonly IDictionary<string, int> ThingLookup =
            new Dictionary<string, int>
                    {
                        { "a", 1 },
                        { "b", 2 }
                    };
}

are shared by all static functions and properties and by all instances of the class.


static constructors

When applied to constructors, e.g.

class Thing
{
    static Thing()
    {
        \\ Do this once and first.
    }
}

static means the constructor will run once per AppDomain, when the type is first accessed. There are special edge cases around this.


operators

When an operator is overloaded for a type, this is always declared as static, e.g.

public static Thing operator +(Thing left, Thing right)
{
    // Something special to do with things.
}

It is not logical for this to be declared at an instance level since operators must apply to types.

These distinctions are explained here, here, here and here.

like image 80
Jodrell Avatar answered Oct 29 '25 17:10

Jodrell