add a protected property to a class in csharp

To add a protected property to a class in C#, you can declare it with the protected keyword like this:

main.cs
public class MyClass
{
    protected int myProtectedProperty { get; set; }
}
77 chars
5 lines

In the example above, a new property named myProtectedProperty has been added to the MyClass class. The protected keyword before the property specifies that it can only be accessed within the class and its derived classes, but not outside of it.

Using protected properties allows for encapsulation in objects that implement inheritance. Any derived objects can access the inherited protected properties they need without exposing them outside of the objects.

gistlibby LogSnag