Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,34 @@
{
"source_path": "docs/csharp/programming-guide/strings/how-to-compare-strings.md",
"redirect_url": "/dotnet/csharp/how-to/compare-strings"
},
{
"source_path": "docs/csharp/classes.md",
"redirect_url": "/dotnet/csharp/programming-guide/classes-and-structs/classes"
},
{
"source_path": "docs/fsharp/tutorials/type-providers/accessing-a-sql-database-entities.md",
"redirect_url": "/dotnet/fsharp/tutorials/type-providers/index"
},
{
"source_path": "docs/fsharp/tutorials/type-providers/accessing-a-sql-database.md",
"redirect_url": "/dotnet/fsharp/tutorials/type-providers/index"
},
{
"source_path": "docs/fsharp/tutorials/type-providers/accessing-a-web-service.md",
"redirect_url": "/dotnet/fsharp/tutorials/type-providers/index"
},
{
"source_path": "docs/fsharp/tutorials/type-providers/accessing-an-odata-service.md",
"redirect_url": "/dotnet/fsharp/tutorials/type-providers/index"
},
{
"source_path": "docs/fsharp/tutorials/type-providers/generating-fsharp-types-from-dbml.md",
"redirect_url": "/dotnet/fsharp/tutorials/type-providers/index"
},
{
"source_path": "docs/fsharp/tutorials/type-providers/generating-fsharp-types-from-edmx.md",
"redirect_url": "/dotnet/fsharp/tutorials/type-providers/index"
}
]
}
81 changes: 0 additions & 81 deletions docs/csharp/classes.md

This file was deleted.

68 changes: 49 additions & 19 deletions docs/csharp/programming-guide/classes-and-structs/classes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: "Classes (C# Programming Guide)"
ms.date: 07/20/2015
description: Learn about the class types and how to create them
ms.date: 04/05/2018
ms.prod: .net
ms.technology:
- "devlang-csharp"
Expand All @@ -14,40 +15,69 @@ author: "BillWagner"
ms.author: "wiwagn"
---
# Classes (C# Programming Guide)
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](../../../csharp/language-reference/keywords/static.md), 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](../../../csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members.md).

Unlike structs, classes support *inheritance*, a fundamental characteristic of object-oriented programming. For more information, see [Inheritance](../../../csharp/programming-guide/classes-and-structs/inheritance.md).
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 create *instances* of it. These instances are *objects* which are assigned to a variable. The instance of a class 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](../../../csharp/language-reference/keywords/static.md), you cannot create instances, and client code can only access it through the class itself. For more information, see [Static Classes and Static Class Members](../../../csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members.md).

## Reference types
A type that is defined as a [class](../../../csharp/language-reference/keywords/class.md) is a *reference type*. At run time, when you declare a variable of a reference type, the variable contains the value [null](../../../csharp/language-reference/keywords/null.md) until you explicitly create an instance of the class by using the [new](../../../csharp/language-reference/keywords/new.md) operator, or assign it an object that has been created elsewhere, as shown in the following example:

```csharp
MyClass mc = new MyClass();
MyClass mc2 = mc;
```

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](../../../standard/garbage-collection/gc.md).

## Declaring Classes
Classes are declared by using the [class](../../../csharp/language-reference/keywords/class.md) keyword, as shown in the following example:

[!code-csharp[csProgGuideObjects#79](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_1.cs)]

The `class` keyword is preceded by the access level. Because [public](../../../csharp/language-reference/keywords/public.md) 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*.
Classes are declared by using the [class](../../../csharp/language-reference/keywords/class.md) keyword, as shown in the following example:

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

The `class` keyword is preceded by the access level. Because [public](../../../csharp/language-reference/keywords/public.md) is used in this case, anyone can create instances of 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 referred to as an instance of a class.

Objects can be created by using the [new](../../../csharp/language-reference/keywords/new.md) keyword followed by the name of the class that the object will be based on, like this:

[!code-csharp[csProgGuideObjects#80](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_2.cs)]

```csharp
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 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-csharp[csProgGuideObjects#81](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_3.cs)]
```csharp
Customer object2;
```

We don't recommend creating object references such as this one that don't 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:

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

[!code-csharp[csProgGuideObjects#82](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_4.cs)]

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.
This code creates two object references that both refer to the same object. Therefore, any changes to the object made through `object3` are 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

Classes 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](../../../csharp/language-reference/keywords/sealed.md), and other classes can inherit from your class and override class virtual methods.

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:

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

[!code-csharp[csProgGuideObjects#83](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_5.cs)]

When a class declares a base class, it inherits all the members of the base class except the constructors.
When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](../../../csharp/programming-guide/classes-and-structs/inheritance.md).

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](../../../csharp/programming-guide/interfaces/index.md).

Expand All @@ -59,7 +89,7 @@ A *class* is a construct that enables you to create your own custom types by gro
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](../../../csharp/programming-guide/classes-and-structs/constructors.md). The class is then instantiated with the `new` keyword.

## Example
[!code-csharp[csProgGuideObjects#84](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_6.cs)]
[!code-csharp[Class Example](~/samples/snippets/csharp/programming-guide/classes-and-structs/class-example.cs)]

## C# Language Specification
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading