C# Tutorial Lesson 12: Class Declaration

Class declarations can have up to four different parts, surrounding the 'class' keyword:

attributes class-modifiers class class-base class-body

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:

1.

public class Shape

2.

{

3.

    // class-body

4.

}


We now consider the other parts of a class declaration.

Attributes

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. The example given in the C# language reference is this: one might define a 'HelpAttribute' attribute in order to map classes to their documentation. Attributes are covered in more detail in lesson 18.

Class Modifiers

There are seven different - optional - class modifiers. Four of these - 'public', 'internal', 'protected' and '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

The 'public' keyword identifies a type as fully accessible to all other types. This is the implicit accessibility of enumeration members (lesson 7) and interface members (lesson 11).

internal

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.

protected

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).

protected internal

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).

private

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

The '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.

abstract

A class declared as 'abstract' cannot itself be instanced - it is designed only to be a base class for inheritance.

sealed

A class declared as 'sealed' cannot be inherited from.

Class Base

The '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 own 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 'Drawing':

public class DrawingRectangle : Rectangle, Drawing

Interface Declarations

Interfaces (described in Lesson 11) are declared in much the same way as standard classes, except that they use the keyword 'interface' in place of the keyword 'class'. For instance:

public interface Drawing

The other important difference is that the class modifiers 'abstract' and 'sealed' cannot be used with interface declarations (it would be unnecessary to use the former and illegitimate to use the latter).

Link Building Information