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

In C#, we cannot directly access a protected static property on an instance of a class. However, we can access the protected static property through a public or protected static method defined in the class.

Here's an example of a class with a protected static property and a public static method that accesses the property:

main.cs
public class MyClass {
    protected static int MyProtectedStaticProperty { get; set; } = 42;

    public static int GetProtectedStaticProperty() {
        return MyProtectedStaticProperty;
    }
}
198 chars
8 lines

To access the protected static property, you can call the public static method as follows:

main.cs
MyClass instance = new MyClass();
int value = MyClass.GetProtectedStaticProperty();
84 chars
3 lines

Note that we call the method on the class itself, not on the instance of the class.

gistlibby LogSnag