diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 6c9b9aad456fa..a92b90c725b03 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -1126,6 +1126,10 @@ "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": "docs/fsharp/tutorials/type-providers/index" diff --git a/docs/csharp/classes.md b/docs/csharp/classes.md deleted file mode 100644 index 259ab900984b7..0000000000000 --- a/docs/csharp/classes.md +++ /dev/null @@ -1,81 +0,0 @@ ---- -title: Classes - C# Guide -description: Learn about the class types and how you create them -keywords: .NET, .NET Core, C# -author: BillWagner -ms.author: wiwagn -ms.date: 10/10/2016 -ms.topic: article -ms.prod: .net -ms.technology: devlang-csharp -ms.devlang: csharp -ms.assetid: 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](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](programming-guide/classes-and-structs/static-classes-and-static-class-members.md). - -## Reference types -A type that is defined as a [class](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](language-reference/keywords/null.md) until you explicitly create an instance of the object by using the [new](language-reference/keywords/new.md) operator, or assign it an object that has been created elsewhere by using [new](language-reference/keywords/new.md), as shown in the following example: - -[!code-csharp[Reference Types](../../samples/snippets/csharp/concepts/classes/reference-type.cs)] - -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). - -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](language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information, see [Inheritance](programming-guide/classes-and-structs/inheritance.md). - -## Declaring classes -Classes are declared by using the [class](language-reference/keywords/class.md) keyword, as shown in the following example: - -[!code-csharp[Declaring Classes](../../samples/snippets/csharp/concepts/classes/declaring-classes.cs)] - -The **class** keyword is preceded by the access modifier. Because [public](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*. - -## 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](language-reference/keywords/new.md) keyword followed by the name of the class that the object will be based on, like this: - -[!code-csharp[Creating Objects](../../samples/snippets/csharp/concepts/classes/creating-objects.cs)] - -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[Creating Objects](../../samples/snippets/csharp/concepts/classes/creating-objects2.cs)] - -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-csharp[Creating Objects](../../samples/snippets/csharp/concepts/classes/creating-objects3.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. - -## 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-csharp[Inheritance](../../samples/snippets/csharp/concepts/classes/inheritance.cs)] - -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](programming-guide/interfaces/index.md). - -A class can be declared [abstract](language-reference/keywords/abstract.md). 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](language-reference/keywords/sealed.md) class does not allow other classes to derive from it. For more information, see [Abstract and sealed classes and class members](programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md). - -Class definitions can be split between different source files. For more information, see [Partial class definitions](programming-guide/classes-and-structs/partial-classes-and-methods.md). - - -## 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](programming-guide/classes-and-structs/constructors.md). The class is then instantiated with the **new** keyword. - -[!code-csharp[Class Example](../../samples/snippets/csharp/concepts/classes/class-example.cs)] - -## C# language specification -For more information, see the [C# language specification](language-reference/language-specification/index.md). The language specification is the definitive source for C# syntax and usage. - -## See also -[C# programming guide](programming-guide/index.md) -[Polymorphism](programming-guide/classes-and-structs/polymorphism.md) -[Class and struct members](programming-guide/classes-and-structs/members.md) -[Class and struct methods](programming-guide/classes-and-structs/methods.md) -[Constructors](programming-guide/classes-and-structs/constructors.md) -[Finalizers](programming-guide/classes-and-structs/destructors.md) -[Objects](programming-guide/classes-and-structs/objects.md) - diff --git a/docs/csharp/programming-guide/classes-and-structs/classes.md b/docs/csharp/programming-guide/classes-and-structs/classes.md index 4fd2175ed17de..2709af86178c1 100644 --- a/docs/csharp/programming-guide/classes-and-structs/classes.md +++ b/docs/csharp/programming-guide/classes-and-structs/classes.md @@ -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" @@ -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). @@ -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)] diff --git a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_1.cs b/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_1.cs deleted file mode 100644 index e7b746c3a3d3f..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_1.cs +++ /dev/null @@ -1,4 +0,0 @@ - public class Customer - { - //Fields, properties, methods and events go here... - } \ No newline at end of file diff --git a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_2.cs b/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_2.cs deleted file mode 100644 index 4afc9d65b9bbd..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_2.cs +++ /dev/null @@ -1 +0,0 @@ - Customer object1 = new Customer(); \ No newline at end of file diff --git a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_3.cs b/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_3.cs deleted file mode 100644 index 4378df1b76abb..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_3.cs +++ /dev/null @@ -1 +0,0 @@ - Customer object2; \ No newline at end of file diff --git a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_4.cs b/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_4.cs deleted file mode 100644 index 9abd143587cd6..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_4.cs +++ /dev/null @@ -1,2 +0,0 @@ - Customer object3 = new Customer(); - Customer object4 = object3; \ No newline at end of file diff --git a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_5.cs b/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_5.cs deleted file mode 100644 index cf54ec38c86a1..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_5.cs +++ /dev/null @@ -1,5 +0,0 @@ - public class Manager : Employee - { - // Employee fields, properties, methods and events are inherited - // New Manager fields, properties, methods and events go here... - } \ No newline at end of file diff --git a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_6.cs b/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_6.cs deleted file mode 100644 index 2a0bb5bbda340..0000000000000 --- a/docs/csharp/programming-guide/classes-and-structs/codesnippet/CSharp/classes_6.cs +++ /dev/null @@ -1,47 +0,0 @@ - public class Person - { - // Field - public string name; - - // Constructor that takes no arguments. - public Person() - { - name = "unknown"; - } - - // Constructor that takes one argument. - public Person(string nm) - { - name = nm; - } - - // Method - public void SetName(string newName) - { - name = newName; - } - } - class TestPerson - { - static void Main() - { - // Call the constructor that has no parameters. - Person person1 = new Person(); - Console.WriteLine(person1.name); - - person1.SetName("John Smith"); - Console.WriteLine(person1.name); - - // Call the constructor that has one parameter. - Person person2 = new Person("Sarah Jones"); - Console.WriteLine(person2.name); - - // Keep the console window open in debug mode. - Console.WriteLine("Press any key to exit."); - Console.ReadKey(); - } - } - // Output: - // unknown - // John Smith - // Sarah Jones \ No newline at end of file diff --git a/docs/toc.md b/docs/toc.md index e9401a487d098..d7a64c6750e8c 100644 --- a/docs/toc.md +++ b/docs/toc.md @@ -226,7 +226,7 @@ ### [C# Type system](csharp/programming-guide/types/index.md) ### [Namespaces](csharp/programming-guide/namespaces/index.md) ### [Basic Types](csharp/basic-types.md) -### [Classes](csharp/classes.md) +### [Classes](csharp/programming-guide/classes-and-structs/classes.md) ### [Structs](csharp/structs.md) ### [Tuples](csharp/tuples.md) ### [Deconstructing tuples and other types](csharp/deconstruct.md)