Επεξεργασία

Κοινή χρήση μέσω


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

Remarks

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 { }

Classes that you declare directly within a namespace, not nested within other classes, can be either public or internal. Classes are internal by default.

Class members, including nested classes, can be public, protected internal, protected, internal, private, or private protected. Members are private by default.

For more information, see Access Modifiers.

You can declare generic classes that have type parameters. For more information, see Generic Classes.

A class can contain declarations of the following members:

Example

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 first class, Child, contains two private fields (name and age), two public constructors and one public method. 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.
*/

Comments

Notice that in the previous example the private fields (name and age) can only be accessed through the public method 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 parameterless constructor (child3), the age field was initialized to zero by default.

C# language specification

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

See also