共用方式為


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

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:

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 Kid class, which contains two private fields (name and age) and two public methods. The second class, MainClass, is used to contain Main.

// keyword_class.cs
// class example
using System;
class Kid
{
    private int age;
    private string name;

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

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

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

class MainClass
{
    static void Main()
    {
        // Create objects
        // Objects must be created using the new operator:
        Kid kid1 = new Kid("Craig", 11);
        Kid kid2 = new Kid("Sally", 10);

        // Create an object using the default constructor:
        Kid kid3 = new Kid();

        // Display results:
        Console.Write("Kid #1: ");
        kid1.PrintKid();
        Console.Write("Kid #2: ");
        kid2.PrintKid();
        Console.Write("Kid #3: ");
        kid3.PrintKid();
    }
}

Output

Kid #1: Craig, 11 years old.
Kid #2: Sally, 10 years old.
Kid #3: N/A, 0 years old.

Comments

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

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

Accessing private members of Kid 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 (kid3), the age field was initialized to zero by default.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.6 Classes and objects

  • 3.4.4 Class members

  • 4.2.1 Class types

  • 10 Classes

See Also

Reference

C# Keywords
Reference Types (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference