Archive

Archive for the ‘C Sharp’ Category

C# Class / Method Access Modifiers

March 26, 2010 1 comment

Introduction

In order to clarify certain programming concepts in my head, I thought it would be beneficial to write a few articles about some of the core parts of the C# language. When my work load dims down a little I will progressively write more articles on this blog and also increase the difficulty of the concepts I’m explaining to you. But for now please enjoy this article Access Modifiers in the C# language.

Class Modifiers

The class is one of the most fundamental and widely used encapsulation types in C# and in Object Oriented Programming (the other of course being its predecessor the ‘struct’). In C# a class can exhibit the following access behaviour:

  • abstract: An instance of the class cannot be created. The intention of this is to allow the class to be derived from and serve as a base class.
  • sealed: The class cannot be derived from (serve as a base class). abstract and sealed are mutually exclusive and therefore a class cannot be both at the same time.
  • internal: The class is only accessible from other classes in the same assembly (or program). This is the default access for non-nested types. If no modifier is specified, then a class’ default access behaviour will be internal.
  • new: Used only with nested classes. The modifier ‘new’ indicates that the class hides an inherited member of the same name.
  • private: Used when defining a nested class which can only be accessed inside the containing class.
  • public: No form of access restriction is placed on the class.

Access Modifiers

The method declaration syntax for a method generally contains one of the following:

  • internal
  • private
  • protected
  • protected internal
  • public

If no method access modifier is specified in the declaration then the method is defaulted to private access. Before I explain the standard access of methods, there are a few special cases that interact with classes in unique ways. These are also often used in combination with the standard access modifiers:

  • virtual: this modifier allows methods to be overriden by a derived class using the override keyword.
  • override: as mentioned above, this modifier overrides a virtual methods from the class that is being inherited from.
  • abstract: these methods must be overriden in a derived class. If any method contained in a class is abstract then the entire class must be marked as abstract.
  • sealed: a sealed method is a method that overrides an inherited virtual method having the same signature (name and parameters). When a method is sealed, it cannot be overridden in a derived class.

Method Access Modifiers

The following access modifiers affect the behaviour of methods within a class:

  • public: the method has no restrictions and can be accessed freely.
  • internal: the method is only accessible to types defined within the same assembly (or program).
  • protected: the method is accessible in the class in which it was defined, and in derived classes of that class. This access modifier is used to give derived classes access to the methods in their base class.
  • protected internal: the method is accessible to classes defined in the same assembly or to classes in a derived assembly (or program).
  • private: methods are only accessible in the class in which they are defined.
Design a site like this with WordPress.com
Get started