add a static property to a class in csharp

To add a static property to a class in C#, you simply need to use the static keyword.

main.cs
public class MyClass
{
    public static int MyStaticProperty { get; set; }
}
78 chars
5 lines

In the above example, we've added a static property MyStaticProperty to the class MyClass. This property can be accessed using the class name, rather than an instance of the class.

main.cs
MyClass.MyStaticProperty = 1;
int myValue = MyClass.MyStaticProperty;
70 chars
3 lines

Note that static properties are shared across all instances of the class and can be accessed even if no instances of the class have been created.

gistlibby LogSnag