Tuesday, 25 November 2014

C#.NET Tutorial - Day - 15 - Classes - Visibility


VISIBILITY



The visibility of a class, a method, a variable or a property tells us how this item can be accessed. The most common types of visibility are private and public, but there are actually several other types of visibility within C#. Here is a complete list, and although some of them might not feel that relevant to you right now, you can always come back to this page and read up on them: 

PUBLIC - the member can be reached from anywhere. This is the least restrictive visibility. Enums and interfaces are, by default, publicly visible. 

PROTECTED - members can only be reached from within the same class, or from a class which inherits from this class. 

INTERNAL - members can be reached from within the same project only. 

PROTECTED INTERNAL - the same as internal, except that also classes which inherits from this class can reach it members, even from another project. 

PRIVATE - can only be reached by members from the same class. This is the most restrictive visibility. Classes and structs are by default set to private visibility. 

So for instance, if you have two classes, Class1 and Class2, private members from Class1 can only be used within Class1. You can't create a new instance of Class1 inside of Class2, and then expect to be able to use its private members. 

If Class2 inherits from Class1, then only non-private members can be reached from inside of Class2.



No comments:

Post a Comment