Declaring Classes

Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents


A class is an abstract representation for some particular type of object. It can be described as a template or blueprint for an object, as opposed to the actual object itself. Thus, objects are an instance of a class - they come into existence at some specific time, persist for some duration, and then disappear when they are no longer needed. Classes are the abstract descriptions used by the system to create objects when called upon to do so.


Declaration

[attributes] [modifiers] class identifier [:base-type]
{
  body [;]
}

The attributes is optional and is used to hold additional declarative information.

The modifier is optional. The allowed modifiers are static, sealed, abstract, unsafe, public and internal. If no modifier is supplied then a default of internal is used.

The keyword class must be followed by an identifier that names the class.

base-type may define any class other than System.Array, System.Delegate, System.MulticastDelegate, System.Enum or System.ValueType as the base class for this class. If a base class is not supplied, then the class will inherit from System.Object.

base-type may also specify the interfaces implemented by this class. The interfaces must be listed after the base class name (if specified) and must be separated by commas.

The body contains the member declarations.

A simple class

The following code contains the minimum code for a class. Although this class will not actually do anything.

class SimpleObject
{
  public SimpleObject()
  {
  }
}


Modifiers

abstract - the class is created solely for the purpose of inheritance. You cannot create an instance of an abstract class.

sealed - the class cannot be inherited from.

static – the class can only contain static members.

unsafe - allows for unsafe constructs such as pointers. Requires the unsafe compiler option.

public - any item in the current assembly or any assembly that references it, can access this class.

internal - Any item in the current assembly can access this class.

The access levels protected and private are only allowed on nested classes.

MSDN references


prevpp.png  nextpp.png
C# Online.NET