Code Focused

Default Access Levels in C# and Visual Basic

It pays to be explicit and not use the emotional defaults of each language.

The C# and Visual Basic access modifiers let you indicate the level of interpersonal relationships your type member can have. Whether they're Private, Public or something else, each modifier makes clear what code can access each member.

Every access modifier in C# has a parallel modifier in Visual Basic, and most of them use the same names. The "public" modifier in C# is identical to the Visual Basic "Public" modifier. The "private" and "protected" keywords are, likewise, identical in Visual Basic, save for the initial capital letter. The lone exception is the C# "internal" modifier, which is called "Friend" in Visual Basic.

This difference in naming might shed light on how each language treats type members when you omit access modifiers from your member declarations. C#, the shy language that it is, tends to internalize things, keeping them private in the absence of a clear direction from the programmer. As a result, all C# class members are private by default. If you want them to be anything other than private, you need to be explicit:

class JustSomeClass
{
  // ----- All members are private by default.
  int ThisFieldIsPrivate;
  private int SoIsThisOne;
  public int FinallySomethingPublic;
}

Visual Basic, on the other hand, is a big, friendly, bear-hug of a language, a bit more trusting than C#, and somewhat free with its type members. If you leave the access modifiers off of a field or constant declaration in Visual Basic, those members will be Private, just as in C#. However, all other members -- methods, properties, events and so on -- are Public by default, and will be Public unless you indicate differently:

Class JustSomeClass
  ' ----- Fields and constants are Private by default.
  Dim ThisFieldIsPrivate As Integer
  Private SoIsThisOne As Integer

  ' ----- Methods are Public by default.
  Sub RandomPublicMethod()
  End Sub

  Private Sub ButThisIsPrivate()
  End Sub
End Class

The friendly attitude of Visual Basic extends to structures, as well. All structure members are Public by default. This varies considerably from C#, which considers structure members private unless otherwise indicated. In both languages, a structure member can never by marked as Protected:

// ----- Simple C# structure.
struct JustSomeStructure
{
  int TypicalPrivateMember;
}

' ----- Simple Visual Basic structure.
Structure JustSomeStructure
  Dim TypicalPublicMember As Integer
End Structure

At the namespace level, where classes and structures typically live, things start to even out again in both languages. You can apply the access modifiers to type declarations, but if you leave them out, C# will treat classes as internal:

namespace SomeApplication
{
  // ----- This class is internal by default.
  class SomeClass
  {
    // ----- Members are private by default.
  }
}
Visual Basic, likewise, treats unmodified types as Friend:
Namespace SomeApplication
  ' ----- This class is Friend by default.
  Class SomeClass
    ' ----- Members are Private or Public by default,
    '       as described earlier.
  End Class
End Namespace

The moral is: Don't let your type members be controlled by the emotional defaults of each language. Be explicit.

About the Author

Tim Patrick has spent more than thirty years as a software architect and developer. His two most recent books on .NET development -- Start-to-Finish Visual C# 2015, and Start-to-Finish Visual Basic 2015 -- are available from http://owanipress.com. He blogs regularly at http://wellreadman.com.

comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube