call a static property on an instance of a class in csharp

To call a static property on an instance of a class in C#, you should use the class name instead of the instance name. Here is an example:

main.cs
public class MyClass
{
    public static int myStaticProperty { get; set; }
    public int myProperty { get; set; }
}

// Create an instance of the class
MyClass obj = new MyClass();

// Set the value of the static property using the class name
MyClass.myStaticProperty = 10;

// Access the value of the static property using the class name or the instance name
int staticValue = MyClass.myStaticProperty; // returns 10
int instanceValue = obj.myStaticProperty; // returns 0
475 chars
16 lines

Note that static properties are associated with the class itself, rather than with any specific instance of the class. Therefore, they can be accessed using the class name without creating an instance of the class.

gistlibby LogSnag