class (C# Reference)

Classes are declared using the keyword class, as shown in the following example:

class TestClass
{
    // Methods, properties, fields, events, delegates 
    // and nested classes go here.
}

Unlike C++, only single inheritance is allowed in C#. In other words, a class can inherit implementation from one base class only. However, a class can implement more than one interface. The following table shows examples of class inheritance and interface implementation:

Inheritance

Example

None

class ClassA { }

Single

class DerivedClass: BaseClass { }

None, implements two interfaces

class ImplClass: IFace1, IFace2 { }

Single, implements one interface

class ImplDerivedClass: BaseClass, IFace1 { }

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

You can also declare generic classes that have type parameters; see Generic Classes for more information.

A class can contain declarations of the following members:

The following example demonstrates declaring class fields, constructors, and methods. It also demonstrates object instantiation and printing instance data. In this example, two classes are declared, the Child class, which contains two private fields (name and age) and two public methods. The second class, StringTest, is used to contain Main.


class Child
{
    private int age;
    private string name;

    // Default constructor:
    public Child()
    {
        name = "N/A";
    }

    // Constructor:
    public Child(string name, int age)
    {
        this.name = name;
        this.age = age;
    }

    // Printing method:
    public void PrintChild()
    {
        Console.WriteLine("{0}, {1} years old.", name, age);
    }
}

class StringTest
{
    static void Main()
    {
        // Create objects by using the new operator:
        Child child1 = new Child("Craig", 11);
        Child child2 = new Child("Sally", 10);

        // Create an object using the default constructor:
        Child child3 = new Child();

        // Display results:
        Console.Write("Child #1: ");
        child1.PrintChild();
        Console.Write("Child #2: ");
        child2.PrintChild();
        Console.Write("Child #3: ");
        child3.PrintChild();
    }
}
/* Output:
    Child #1: Craig, 11 years old.
    Child #2: Sally, 10 years old.
    Child #3: N/A, 0 years old.
*/


Notice, in the preceding example, that the private fields (name and age) can only be accessed through the public methods of the Child class. For example, you cannot print the child's name, from the Main method, using a statement like this:

Console.Write(child1.name);   // Error

Accessing private members of Child from Main would only be possible if Main were a member of the class.

Types declared inside a class without an Access Modifier default to private, so the data members in this example would still be private if the keyword were removed.

Finally, notice that for the object created using the default constructor (child3), the age field was initialized to zero by default.

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft