| Often when studying a new
technology or language it is helpful to write out, in condensed form,
the important concepts so that one can easily refer to it during the course
of assimilating the concepts in practice. Many programmers coming
from the VB world are unaware of the different class and method modifiers
in a new language such as C# and what the purpose of each is. The
following synopsis continues to be helpful to me, and so I present it
here. For the purposes of this article, the word "type" can
be assumed to be synonymous with the word "class".
Class Modifiers
The class is one of the two basic encapsulation constructs in C# (the
other being the struct). Every executable statement must be placed inside
a class or struct. Classes define reference types that are the basic building
blocks of C# programs, and they are the architectural blueprint for the
"objects" in OOP.
A class can be...
abstract: An instance of the class cannot be created.Usually
this means the class is intended to serve as a base class.
sealed: The class cannot serve as a base class for another
class (it can't be derived from). A class cannot be both abstract and
sealed.
internal: The class is only accessible from other classes
in the same assembly. This is the default access for non-nested types.
If no modifier is specified, the class has internal access.
new: Used only with nested classes. "New"
indicates that the class hides an inherited member of the same name.
private: A nested class that can only be accessed inside
the class in which it is defined.
public: Instances of this class are available to any
class that wants to access it.
Constructor Modifiers
A class defines data members, methods and nested
types. A constructor is a special method that is normally used to
initialize the data members of a class, and its name is always the same
as the name of the class. Constructors have no return value, and any number
of constructors can be defined within a class. If no constructor is defined,
the C# compiler provides a default constructor having no parameters.
Methods
Methods are always defined within the bounds of a class or struct. Methods
can be instance (called as an instance of the type within which
the method is defined) or static, where the method is associated
with the type itself. Methods can be declared as virtual, abstract
, or sealed. Methods can be overloaded, overridden and hidden.
Access Modifiers
Access modifiers are specified as part of the method declaration syntax
and can be:
internal
private
protected
protected internal
public
If no modifier is specified, the method is given private access.
virtual methods can be overriden by a derived class
using the override keyword.
abstract methods must be overriden in a derived class.
If any method of a class is abstract, the entire class must be declared
as abstract.
sealed methods are methods that override an inherited
virtual method having the same signature. When a method is sealed, it
cannot be overriden in a derived class.
Method Access Modifiers
public indicates the method is freely accessible inside
and outside of the class in which it is defined.
internal means the method is only accessible to types
defined in the same assembly.
protected means the method is accessible in the type
in which it is defined, and in derived types of that type. This is used
to give derived classes access to the methods in their base class.
protected internal means the method is accessible to
types defined in the same assembly or to types in a derived assembly.
private methods are only accessible in the class in
which they are defined.
| |
| Peter Bromberg is a C# MVP, MCP, and .NET consultant who has worked in the banking and financial industry for 20 years. He has architected and developed web - based corporate distributed application solutions since 1995, and focuses exclusively on the .NET Platform. Pete's samples at GotDotNet.com have been downloaded over 41,000 times. You can read Peter's UnBlog Here. --><-- NOTE: Post QUESTIONS on FORUMS! |  | Do you have a question or comment about this article? Have a programming problem you need to solve? Post it at eggheadcafe.com forums and receive immediate email notification of responses.
|