0

i recently started studying c# and something i dont have very clear is what does static mean and how it behaves.

For example if a have a static method , i know that to use that method i need to call upon the class and not the instance of the object but i really dont knwow the reason that gives that behavior. On the other side for the propierties of the class if they are static i am not very sure how they are modified and how they behave

  • A static property/field of a class is present exactly once irrespective of the number of objects you created, an instance property/field is present once per created object. – Klaus Gütter Apr 11 '21 at 05:36
  • Make sure you really know and understand the difference between a class and an/its instance(s). That's sth I often see beginners in OOP struggling with. If you get that, the explanation is simple: `static` are a method/property of the class - hence singleton, kinda like "scoped/namespaced globals", and non static belong to each instance – Marian Theisen Apr 11 '21 at 06:01
  • I've answers with few links highlighting the parts that are relevant to your question. Please let us know if this is clarifies things. – tymtam Apr 11 '21 at 06:28
  • @tymtam yes the info was very useful, thanks everyone! – Federico Garcik Apr 11 '21 at 06:41

1 Answers1

1

Please note that C# static is different from static in c++.

From Static Classes and Static Class Members (C# Programming Guide):

Static Member

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's explicitly passed in a method parameter.

and later:

Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.

The article also has an example of a static property:

public static int SizeOfGasTank
{
get
   {
      return 15;
   }
}

I recommend reading the whole Static Classes and Static Class Members (C# Programming Guide).

There also is Static Constructors (C# Programming Guide) which is worth reading as well because static constructors are important to understand, even if they are not used that often.

Please note that there also is using static directive, which is something completely different.

How they work

How may mean many things so I'm not sure what exactly you're asking about, but this paragraph, again from Static Classes and Static Class Members (C# Programming Guide) provides the answer to how technically do static methods work?:

A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for null object references. However, most of the time the performance difference between the two is not significant.

tymtam
  • 31,798
  • 8
  • 86
  • 126