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

You cannot call a private static property on an instance of a class in C#. Private static properties are only accessible within the class in which they are declared.

However, you can create a public static method within that class that accesses the private static property and returns its value. Then you can call that static method on an instance of the class.

For example, consider the following class with a private static property:

main.cs
public class MyClass
{
    private static int myPrivateStaticProperty = 42;
}
78 chars
5 lines

To access the value of myPrivateStaticProperty from an instance of MyClass, you can create a public static method that returns its value:

main.cs
public class MyClass
{
    private static int myPrivateStaticProperty = 42;

    public static int GetMyPrivateStaticProperty()
    {
        return myPrivateStaticProperty;
    }
}
182 chars
10 lines

Then you can call GetMyPrivateStaticProperty() on an instance of MyClass:

main.cs
MyClass myInstance = new MyClass();
int value = MyClass.GetMyPrivateStaticProperty();
86 chars
3 lines

gistlibby LogSnag