Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Some "real world" examples for static variables:</p> <p>building a class where you can reach hardcoded values for your application. Similar to an enumeration, but with more flexibility on the datatype.</p> <pre><code>public static class Enemies { public readonly static Guid Orc = new Guid("{937C145C-D432-4DE2-A08D-6AC6E7F2732C}"); } </code></pre> <p>The widely known singleton, this allows to control to have exactly one instance of a class. This is very useful if you want access to it in your whole application, but not pass it to every class just to allow this class to use it.</p> <pre><code>public sealed class TextureManager { private TextureManager() {} public string LoadTexture(string aPath); private static TextureManager sInstance = new TextureManager(); public static TextureManager Instance { get { return sInstance; } } } </code></pre> <p>and this is how you would call the texturemanager</p> <pre><code>TextureManager.Instance.LoadTexture("myImage.png"); </code></pre> <p>About your last question: You are refering to compiler error <a href="http://msdn.microsoft.com/en-us/library/zhcxt2bd%28v=vs.80%29.aspx" rel="noreferrer">CS0176</a>. I tried to find more infor about that, but could only find what the msdn had to say about it:</p> <blockquote> <p>A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload