Skip to content

Latest commit

 

History

History
81 lines (53 loc) · 7.83 KB

classes.md

File metadata and controls

81 lines (53 loc) · 7.83 KB
title description keywords author ms.author ms.date ms.topic ms.prod ms.technology ms.devlang ms.assetid
Classes - C# Guide
Learn about the class types and how you create them
.NET, .NET Core, C#
BillWagner
wiwagn
10/10/2016
article
.net
devlang-csharp
csharp
95c686ba-ae4f-440e-8e94-0dbd6e04d11f

Classes

A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, client code can use it by creating objects or instances which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as static, then only one copy exists in memory and client code can only access it through the class itself, not an instance variable. For more information, see Static classes and static class members.

Reference types

A type that is defined as a class is a reference type. At run time, when you declare a variable of a reference type, the variable contains the value null until you explicitly create an instance of the object by using the new operator, or assign it an object that has been created elsewhere by using new, as shown in the following example:

[!code-csharpReference Types]

When the object is created, the memory is allocated on the managed heap, and the variable holds only a reference to the location of the object. Types on the managed heap require overhead both when they are allocated and when they are reclaimed by the automatic memory management functionality of the CLR, which is known as garbage collection. However, garbage collection is also highly optimized, and in most scenarios, it does not create a performance issue. For more information about garbage collection, see Automatic memory management and garbage collection.

Reference types fully support inheritance, a fundamental characteristic of object-oriented programming. When you create a class, you can inherit from any other interface or class that is not defined as sealed, and other classes can inherit from your class and override your virtual methods. For more information, see Inheritance.

Declaring classes

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

[!code-csharpDeclaring Classes]

The class keyword is preceded by the access modifier. Because public is used in this case, 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

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 referred to as an instance of a class.

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

[!code-csharpCreating Objects]

When an instance of a class is created, a reference to the object is passed back to the programmer. In the previous example, object1 is a reference to an object that is 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:

[!code-csharpCreating Objects]

We do not recommend creating object references such as this one that does not refer to an object because trying 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, such as this:

[!code-csharpCreating Objects]

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. Because objects that are based on classes are referred to by reference, classes are known as reference types.

Class inheritance

Inheritance is accomplished by using a derivation, which means a class is declared by 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:

[!code-csharpInheritance]

When a class declares a base class, it inherits all the members of the base class except the constructors.

Unlike C++, a class in C# can only directly inherit from one base class. However, because a base class may itself inherit from another class, a class may indirectly inherit multiple base classes. Furthermore, a class can directly implement more than one interface. For more information, see Interfaces.

A class can be declared abstract. An abstract class contains abstract methods that have a signature definition but no implementation. Abstract classes cannot be instantiated. They can only be used through derived classes that implement the abstract methods. By contrast, a sealed class does not allow other classes to derive from it. For more information, see Abstract and sealed classes and class members.

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

Example

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

[!code-csharpClass Example]

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

C# programming guide
Polymorphism
Class and struct members
Class and struct methods
Constructors
Finalizers
Objects