Experienced .NET developers know there are five different visibility levels. As a best practice, you should always specify the visibility levels for all custom types and their members. If you decide not to set it or forget, the visibility level will default to a value based on three factors - what it is, the .NET language you are using, and its location.
Java developers aren't allowed to be lazy about this; they must specify the visibility for all types and members. Java does not include default visibility levels, per se. By not using a visibility modifier, Java developers are (in effect) assigning default package level visibility - similar to .NET's internal or Friend visibility levels. The big difference is that this is the only way they can assign this visibility level - by not assigning it at all!
Do you think you know all the default visibility levels for your .NET language? Let's take a quiz! I recommend you take the quiz for both languages because the answers will be different for C# and VB.
The following code examples were created by lazy VB/C# developers. None of the visibility levels were specified; they were defined using default values. What are they?
Here is the C# example:
class Greet
{
event EventHandler GreetCreated;
class Inner
{
class InnerChild { }
}
enum Honorific
{
Miss,
Mr,
Mrs,
Ms
}
string sName = string.Empty;
string Hello(string fullname, Honorific hon)
{
sName = fullname;
if (GreetCreated != null)
GreetCreated(this, new EventArgs());
return string.Format("Hello, {0}. {1}!", hon, sName);
}
}
Pick from the following five answers for the following questions:
A. public
B. protected internal
C. protected
D. internal
E. private
CS1. What is the visibility of the Greet class?
CS2. What is the visibility of the GreetCreated event?
CS3. What is the visibility of the Inner class?
CS4. What is the visibility of the InnerChild class?
CS5. What is the visibility of the Honorific enum?
CS6. What is the visibility of the sName field?
CS7. What is the visibility of the Hello method?
Here is the VB example:
Class Greet
Event GreetCreated()
Class Inner
Class InnerChild
End Class
End Class
Enum Honorific
Miss
Mr
Mrs
Ms
End Enum
Dim sName As String = String.Empty
Function Hello(ByVal fullname As String,
ByVal hon As Honorific) As String
sName = fullname
RaiseEvent GreetCreated()
Return String.Format("Hello, {0}. {1}!", hon, sName)
End Function
End Class
Pick from the following five answers for the following questions:
A. Public
B. Protected Friend
C. Protected
D. Friend
E. Private
VB1. What is the visibility of the Greet class?
VB2. What is the visibility of the GreetCreated event?
VB3. What is the visibility of the Inner class?
VB4. What is the visibility of the InnerChild class?
VB5. What is the visibility of the Honorific enum?
VB6. What is the visibility of the sName field?
VB7. What is the visibility of the Hello method?
Check your answers against the key at the bottom of this post.
If you answered all the questions correctly - good job! Kudos to you. You have all the default visibility settings memorized. Question: would everyone else on your development and support teams (now and in the future) pass this quiz just as easily?
If you didn't get 100%, trust me, you're not alone. This proves why setting explicit visibility should be required. By comparing the differences in default visibility between C# and VB, it seems quite obvious that it is especially important in VB. To make things worse, as you add new code files (for example, classes) to your projects, the starter code does not include any visibility settings in new code files.
Best Practice:
Always specify the visibility for all types and members that you declare.
Answer Key:
CS1. D, CS2. E, CS3. E, CS4. E, CS5. E, CS6. E, CS7. E
VB1. D, VB2. A, VB3. A, VB4. A, VB5. A, VB6. E, VB7. A
**NOTE: Please rate this post's usefulness by clicking on the number of stars it deserves below. Thank you!