Classes are a blueprint for creating individual objects that contain the general characteristics of a defined object type.
Class declarations can have up to four different parts, surrounding the
The class-body element specifies type members. The following is an example of a very simple class declaration, the class body being the lines following line 1:
We will now consider the other parts of a class declaration.
- C# Class Attributes
- C# Class Modifiers
- C# Base Class
- C# Constructors
C# Class Attributes
Attributes can add metadata information to your assemblies. An attribute is actually an object that is associated with any of these elements: Assembly, Class, Method, Delegate, Enum, Event, Field, Interface, Property and Struct.
They can be used to associate declarative information - you can retrieve such information at runtime at a later point of time if need be using reflection.
Attributes can be posted at the front of a class declaration. These comprise user-defined meta-data about the class; information which can be brought out at runtime. For example, one might define a
Serializable attribute to indicate that instances of this type can be serialized. You can learn more about c# attributes here.
C# Class Modifiers
There are seven different class modifiers. Four of these -
private - are used to specify the access levels of the types defined by the classes.
The following five different access levels can be specified with these four modifiers:
public keyword identifies a type as fully accessible to all other types. This is the implicit accessibility of enumeration members and interface members.
If a class is declared as
internal, the type it defines is accessible only to types within the same assembly (a self-contained ‘unit of packaging’ containing code, metadata, etc.). This is the default access level of non-nested classes.
If a class is declared as
protected, its type is accessible by a containing type and any type that inherits from this containing type. This modifier should only be used for internal classes (ie. classes declared within other classes).
The permissions allowed by this access level are those allowed by the
protected level plus those allowed by the
internal level. The access level is thus more liberal than its parts taken individually. This modifier should only be used for internal classes (ie. classes declared within other classes).
Where a class is declared as
private, access to the type it defines is limited to a containing type only. This modifier should only be used for internal classes (ie. classes declared within other classes).
We now turn to the final three class modifiers:
new keyword can be used for
nested classes. A nested class is one that is defined in the body of another class; it is in most ways identical to a class defined in the normal way, but its access level cannot be more liberal than that of the class in which it is defined. A nested class should be declared using the
new keyword just in case it has the same name as (and thus overrides) an inherited type.
A class declared as
abstract cannot itself be instanced - it is designed only to be a base class for inheritance.
A class declared as
sealed cannot be inherited from.
C# Base Class
class-base part of the class declaration specifies the name of the class and any classes that it inherits from.
As we noted previously, classes can inherit from just one base class and any number of interfaces. The classes to be inherited from are named following a colon after the class’s name (with any base class preceding any interfaces). The following line declares a public class called
DrawingRectangle which inherits from the base class
Rectangle and the interface
Creating an instance of a class is done with the new operator. Calling new on an object executes that object’s constructor. By default, each class has an empty constructor that can be called. You can also define your own constructors. You do this for two reasons:
- To initialize the type with data
- To run some initialization code
Adding A Constructor To Your Type
As you can see in above example, the constructor takes an argument of int. When you want to instantiate a new instance of this class, you need to pass a value to the constructor:
The constructor also runs some code to make sure that the object is in a usable state. Some good practices when designing your constructors are these:
- Explicitly declare the public default construct in classes if such a constructor is required.
- Ensure that your constructor takes as few parameters as possible.
- Map constructor parameters to properties in your class.
- Throw exceptions from instance constructors if appropriate.
- Do not call virtual members from an object inside its constructor.
A class can also have multiple constructors, which can be chained to each other so you can avoid duplicate code. Below code shows how you can chain multiple constructors.
In production code it’s important that you make clear which constructor users of your class should use. You can do this by picking meaningful parameter names and by adding comments that explain the use of each constructor
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox