Share via


Classes (C# Programming Guide) 

A class is the most powerful data type in C#. Like structures, a class defines the data and behavior of the data type. Programmers can then create objects that are instances of this class. Unlike structures, classes support inheritance, a fundamental part of object-oriented programming. For more information, see Inheritance.

Declaring Classes

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

public class Customer
{
    //Fields, properties, methods and events go here...
}

The class keyword is preceded by the access level. In this case public is used, meaning anyone can create objects from this class. The name of the class follows the class keyword. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.

Creating Objects

Although they are sometimes used interchangeably, a class and an object are different things. A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes called an instance of a class.

Objects can be created using the new keyword followed by the name of the class that the object will be based upon, like this:

Customer object1 = new Customer();

When an instance of a class is created, a reference to the object is passed back to the programmer. In the example above, object1 is a reference to an object based on Customer. This reference refers to the new object, but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:

Customer object2;

Creating object references like this one that does not refer to an object is not recommended because attempting to access an object through such a reference will fail at run time. However, such a reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, like this:

Customer object3 = new Customer();
Customer object4 = object3;

This code creates two object references that both refer to the same object. Therefore, any changes to the object made through object3 will be reflected in subsequent uses of object4. It is because objects based on classes are referred to by reference that classes are known as reference types.

Class Inheritance

Inheritance is accomplished through the use of a derivation, which means a class is declared using a base class from which it inherits data and behavior. A base class is specified by appending a colon and the name of the base class following the derived class name, like this:

public class Manager : Employee
{
    // Employee fields, properties, methods and events are inherited
    // New Manager fields, properties, methods and events go here...
}

When a class declares a base class, all of the class members defined for the base class become part of the new class as well. Because a base class may itself inherit from another class, which inherited from another class, and so on, a class may end up with many base classes.

Example

In the following example, a public class is defined, containing a single field, a method and a special method called a constructor. For more information, see Constructors. The class is then instantiated with the new keyword.

public class Person
{
    // Field
    public string name;

    // Constructor
    public Person()
    {
        name = "unknown";
    }

    // Method
    public void SetName(string newName)
    {
        name = newName;
    }
}
class TestPerson
{
    static void Main()
    {
        Person person1 = new Person();
        System.Console.WriteLine(person1.name);

        person1.SetName("John Smith");
        System.Console.WriteLine(person1.name);
    }
}

Output

unknown

John Smith

Classes Overview

Classes have the following properties:

  • Unlike C++, only single inheritance is supported: a class can inherit implementation from only one base class.

  • A class can implement more than one interface. For more information, see Interfaces.

  • Class definitions can be split between different source files. For more information, see Partial Class Definitions.

  • Static classes are sealed classes containing only static methods. For more information, see Static Classes and Static Class Members.

C# Language Specification

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

  • 1.6 Classes and objects

  • 10 Classes

See Also

Reference

Members (C# Programming Guide)
Methods (C# Programming Guide)
Destructors (C# Programming Guide)
Objects (C# Programming Guide)

Concepts

C# Programming Guide

Other Resources

Constructors (C# Programming Guide)