From 077956df08a04ca3bdc1dd14949cb9e29e8b836e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 23 Mar 2021 17:58:02 -0400 Subject: [PATCH 01/60] Update navigation In this commit do the following: 1. Update the landing page. 1. Move articles to the new "Fundamentals" section (in the TOC only). --- docs/csharp/index.yml | 29 ++++++++++++++++++---------- docs/csharp/toc.yml | 44 +++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/docs/csharp/index.yml b/docs/csharp/index.yml index 39007765f8704..000c29e9b3e0a 100644 --- a/docs/csharp/index.yml +++ b/docs/csharp/index.yml @@ -70,25 +70,34 @@ landingContent: links: - text: "What's new in C# 9.0" url: whats-new/csharp-9.md - - - title: "New features in C# 9.0" - linkLists: - - linkListType: whats-new - links: - - text: "What's new in C# 9.0" - url: whats-new/csharp-9.md + - text: "What's new in C# 8.0" + url: whats-new/csharp-8.md + - text: "What's new in C# 7" + url: whats-new/csharp-7.md - linkListType: tutorial links: - text: Explore record types - url: ./whats-new/tutorials/records.md + url: whats-new/tutorials/records.md - text: Explore top level statements - url: ./whats-new/tutorials/top-level-statements.md + url: whats-new/tutorials/top-level-statements.md - text: Explore new patterns - url: ./whats-new/tutorials/patterns-objects.md + url: whats-new/tutorials/patterns-objects.md + - text: Safely update interfaces + url: whats-new/tutorials/default-interface-methods-versions.md + - text: Create mixins with interfaces + url: whats-new/tutorials/mixins-with-default-interface-methods.md + - text: Explore indexes and ranges + url: whats-new/tutorials/ranges-indexes.md + - text: Nullable reference types + url: whats-new/tutorials/nullable-reference-types.md + - text: Explore async streams + url: whats-new/tutorials/generate-consume-asynchronous-stream.md - linkListType: reference links: - text: Breaking changes in the C# compiler url: whats-new/breaking-changes.md + - text: Version compatibility + url: whats-new/version-update-considerations.md - title: "C# language reference" linkLists: diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 4a0a0db065cfd..2d6bd51ba005c 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -35,6 +35,28 @@ items: href: tour-of-csharp/tutorials/branches-and-loops-local.md - name: List collections href: tour-of-csharp/tutorials/arrays-and-collections.md +- name: Fundamental concepts + items: + - name: Type system + items: + - name: Overview + href: programming-guide/types/index.md + - name: Namespaces + href: programming-guide/namespaces/index.md + - name: Basic Types + href: basic-types.md + # TODO: tuples + - name: Records + href: programming-guide/classes-and-structs/records.md + - name: Classes + href: programming-guide/classes-and-structs/classes.md + # TODO: structs + - name: Interfaces + href: programming-guide/interfaces/index.md + - name: Generics + href: programming-guide/generics/index.md + - name: Anonymous Types + href: programming-guide/classes-and-structs/anonymous-types.md - name: What's new in C# items: - name: C# 9.0 @@ -98,22 +120,12 @@ items: href: tutorials/pattern-matching.md - name: C# concepts items: - - name: C# type system - href: programming-guide/types/index.md - name: Nullable reference types href: nullable-references.md - name: Choose a strategy for enabling nullable reference types href: nullable-migration-strategies.md - - name: Namespaces - href: programming-guide/namespaces/index.md - - name: Basic Types - href: basic-types.md - - name: Classes - href: programming-guide/classes-and-structs/classes.md - name: Deconstructing tuples and other types href: deconstruct.md - - name: Interfaces - href: programming-guide/interfaces/index.md - name: Methods href: methods.md - name: Properties @@ -541,8 +553,6 @@ items: href: programming-guide/statements-expressions-operators/how-to-test-for-reference-equality-identity.md - name: Types items: - - name: Use and define types - href: programming-guide/types/index.md - name: Casting and Type Conversions href: programming-guide/types/casting-and-type-conversions.md - name: Boxing and Unboxing @@ -562,10 +572,6 @@ items: - name: Overview displayName: classes, structs, records href: programming-guide/classes-and-structs/index.md - - name: Classes - href: programming-guide/classes-and-structs/classes.md - - name: Records - href: programming-guide/classes-and-structs/records.md - name: Objects href: programming-guide/classes-and-structs/objects.md - name: Inheritance @@ -679,9 +685,6 @@ items: href: programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md - name: Interfaces items: - - name: Overview - displayName: interfaces - href: programming-guide/interfaces/index.md - name: Explicit Interface Implementation href: programming-guide/interfaces/explicit-interface-implementation.md - name: "How to explicitly implement interface members" @@ -779,9 +782,6 @@ items: href: programming-guide/generics/generics-and-attributes.md - name: Namespaces items: - - name: Overview - displayName: namespaces - href: programming-guide/namespaces/index.md - name: Using namespaces href: programming-guide/namespaces/using-namespaces.md - name: "How to use the My namespace" From a46655ff8c12b978ece0d389aab78b9faa233b88 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 24 Mar 2021 12:05:30 -0400 Subject: [PATCH 02/60] Move files In this commit do the following: 1. Update the TOC and breadcrumb. 1. Move existing content into the new fundamentals section. --- docs/breadcrumb/toc.yml | 13 ++++++++++--- .../types}/anonymous-types.md | 0 .../{ => fundamentals/types}/basic-types.md | 0 .../types}/classes.md | 0 .../types/generics.md} | 0 .../types/index.md | 0 .../types/interfaces.md} | 0 .../types/namespaces.md} | 0 .../types}/records.md | 0 docs/csharp/toc.yml | 18 +++++++++--------- 10 files changed, 19 insertions(+), 12 deletions(-) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/types}/anonymous-types.md (100%) rename docs/csharp/{ => fundamentals/types}/basic-types.md (100%) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/types}/classes.md (100%) rename docs/csharp/{programming-guide/generics/index.md => fundamentals/types/generics.md} (100%) rename docs/csharp/{programming-guide => fundamentals}/types/index.md (100%) rename docs/csharp/{programming-guide/interfaces/index.md => fundamentals/types/interfaces.md} (100%) rename docs/csharp/{programming-guide/namespaces/index.md => fundamentals/types/namespaces.md} (100%) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/types}/records.md (100%) diff --git a/docs/breadcrumb/toc.yml b/docs/breadcrumb/toc.yml index b533118e4fe6d..3d863a83fa7fb 100644 --- a/docs/breadcrumb/toc.yml +++ b/docs/breadcrumb/toc.yml @@ -318,12 +318,19 @@ items: - name: Tutorials tocHref: /dotnet/csharp/tour-of-csharp/tutorials/ topicHref: /dotnet/csharp/tour-of-csharp/tutorials/index - - name: Tutorials - tocHref: /dotnet/csharp/tutorials/ - topicHref: /dotnet/csharp/tutorials/index + - name: Fundamentals + tocHref: /dotnet/csharp/fundamentals/ + topicHref: /dotnet/csharp/fundamentals/type-system/index - name: What's new tocHref: /dotnet/csharp/whats-new/ topicHref: /dotnet/csharp/whats-new/index + items: + - name: Tutorials + tocHref: /dotnet/csharp/whats-new/tutorials/ + topicHref: /dotnet/csharp/whats-new/tutorials/records + - name: Tutorials + tocHref: /dotnet/csharp/tutorials/ + topicHref: /dotnet/csharp/tutorials/index - name: .NET Compiler Platform SDK tocHref: /dotnet/csharp/roslyn-sdk/ topicHref: /dotnet/csharp/roslyn-sdk/index diff --git a/docs/csharp/programming-guide/classes-and-structs/anonymous-types.md b/docs/csharp/fundamentals/types/anonymous-types.md similarity index 100% rename from docs/csharp/programming-guide/classes-and-structs/anonymous-types.md rename to docs/csharp/fundamentals/types/anonymous-types.md diff --git a/docs/csharp/basic-types.md b/docs/csharp/fundamentals/types/basic-types.md similarity index 100% rename from docs/csharp/basic-types.md rename to docs/csharp/fundamentals/types/basic-types.md diff --git a/docs/csharp/programming-guide/classes-and-structs/classes.md b/docs/csharp/fundamentals/types/classes.md similarity index 100% rename from docs/csharp/programming-guide/classes-and-structs/classes.md rename to docs/csharp/fundamentals/types/classes.md diff --git a/docs/csharp/programming-guide/generics/index.md b/docs/csharp/fundamentals/types/generics.md similarity index 100% rename from docs/csharp/programming-guide/generics/index.md rename to docs/csharp/fundamentals/types/generics.md diff --git a/docs/csharp/programming-guide/types/index.md b/docs/csharp/fundamentals/types/index.md similarity index 100% rename from docs/csharp/programming-guide/types/index.md rename to docs/csharp/fundamentals/types/index.md diff --git a/docs/csharp/programming-guide/interfaces/index.md b/docs/csharp/fundamentals/types/interfaces.md similarity index 100% rename from docs/csharp/programming-guide/interfaces/index.md rename to docs/csharp/fundamentals/types/interfaces.md diff --git a/docs/csharp/programming-guide/namespaces/index.md b/docs/csharp/fundamentals/types/namespaces.md similarity index 100% rename from docs/csharp/programming-guide/namespaces/index.md rename to docs/csharp/fundamentals/types/namespaces.md diff --git a/docs/csharp/programming-guide/classes-and-structs/records.md b/docs/csharp/fundamentals/types/records.md similarity index 100% rename from docs/csharp/programming-guide/classes-and-structs/records.md rename to docs/csharp/fundamentals/types/records.md diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 2d6bd51ba005c..472ea85b17bae 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -35,28 +35,28 @@ items: href: tour-of-csharp/tutorials/branches-and-loops-local.md - name: List collections href: tour-of-csharp/tutorials/arrays-and-collections.md -- name: Fundamental concepts +- name: Fundamentals items: - name: Type system items: - name: Overview - href: programming-guide/types/index.md + href: fundamentals/types/index.md - name: Namespaces - href: programming-guide/namespaces/index.md + href: fundamentals/types/namespaces.md - name: Basic Types - href: basic-types.md + href: fundamentals/types/basic-types.md # TODO: tuples - name: Records - href: programming-guide/classes-and-structs/records.md + href: fundamentals/types/records.md - name: Classes - href: programming-guide/classes-and-structs/classes.md + href: fundamentals/types/classes.md # TODO: structs - name: Interfaces - href: programming-guide/interfaces/index.md + href: fundamentals/types/interfaces.md - name: Generics - href: programming-guide/generics/index.md + href: fundamentals/types/generics.md - name: Anonymous Types - href: programming-guide/classes-and-structs/anonymous-types.md + href: fundamentals/types/anonymous-types.md - name: What's new in C# items: - name: C# 9.0 From 28e7194037fb3308d7e2d5e30117ad843c22b13b Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 24 Mar 2021 16:56:06 -0400 Subject: [PATCH 03/60] fix warnings in the fundamentals area --- .../fundamentals/types/anonymous-types.md | 11 +--- docs/csharp/fundamentals/types/basic-types.md | 42 +++++++-------- docs/csharp/fundamentals/types/classes.md | 24 +++------ docs/csharp/fundamentals/types/generics.md | 16 ------ docs/csharp/fundamentals/types/index.md | 52 +++++++++---------- docs/csharp/fundamentals/types/interfaces.md | 31 ++--------- docs/csharp/fundamentals/types/namespaces.md | 11 +--- docs/csharp/fundamentals/types/records.md | 18 +------ docs/csharp/index.yml | 2 +- docs/csharp/toc.yml | 7 --- 10 files changed, 59 insertions(+), 155 deletions(-) diff --git a/docs/csharp/fundamentals/types/anonymous-types.md b/docs/csharp/fundamentals/types/anonymous-types.md index 79ba44e481568..7a10873ff613b 100644 --- a/docs/csharp/fundamentals/types/anonymous-types.md +++ b/docs/csharp/fundamentals/types/anonymous-types.md @@ -11,7 +11,7 @@ ms.assetid: 59c9d7a4-3b0e-475e-b620-0ab86c088e9b Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler. - You create anonymous types by using the [new](../../language-reference/operators/new-operator.md) operator together with an object initializer. For more information about object initializers, see [Object and Collection Initializers](./object-and-collection-initializers.md). + You create anonymous types by using the [new](../../language-reference/operators/new-operator.md) operator together with an object initializer. For more information about object initializers, see [Object and Collection Initializers](../../programming-guide/classes-and-structs/object-and-collection-initializers.md). The following example shows an anonymous type that is initialized with two properties named `Amount` and `Message`. @@ -33,7 +33,7 @@ Console.WriteLine(v.Amount + v.Message); [!code-csharp[csRef30Features#81](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csRef30Features/CS/csref30.cs#81)] - Typically, when you use an anonymous type to initialize a variable, you declare the variable as an implicitly typed local variable by using [var](../../language-reference/keywords/var.md). The type name cannot be specified in the variable declaration because only the compiler has access to the underlying name of the anonymous type. For more information about `var`, see [Implicitly Typed Local Variables](./implicitly-typed-local-variables.md). + Typically, when you use an anonymous type to initialize a variable, you declare the variable as an implicitly typed local variable by using [var](../../language-reference/keywords/var.md). The type name cannot be specified in the variable declaration because only the compiler has access to the underlying name of the anonymous type. For more information about `var`, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). You can create an array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example. @@ -50,10 +50,3 @@ var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as type object. However, doing this defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type. Because the and methods on anonymous types are defined in terms of the `Equals` and `GetHashCode` methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal. - -## See also - -- [C# Programming Guide](../index.md) -- [Object and Collection Initializers](./object-and-collection-initializers.md) -- [Getting Started with LINQ in C#](../concepts/linq/index.md) -- [LINQ in C#](../../linq/index.md) diff --git a/docs/csharp/fundamentals/types/basic-types.md b/docs/csharp/fundamentals/types/basic-types.md index ea6f3f6a3a1c3..605240bacd0b1 100644 --- a/docs/csharp/fundamentals/types/basic-types.md +++ b/docs/csharp/fundamentals/types/basic-types.md @@ -26,63 +26,57 @@ The information stored in a type can include the following: - The kinds of operations that are permitted. -The compiler uses type information to make sure that all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [int](language-reference/builtin-types/integral-numeric-types.md), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [bool](language-reference/builtin-types/bool.md), the compiler generates an error, as shown in the following example: +The compiler uses type information to make sure that all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [int](../../language-reference/builtin-types/integral-numeric-types.md), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [bool](../../language-reference/builtin-types/bool.md), the compiler generates an error, as shown in the following example: -[!code-csharp[Type Safety](../../samples/snippets/csharp/concepts/basic-types/type-safety.cs)] +[!code-csharp[Type Safety](../../../../samples/snippets/csharp/concepts/basic-types/type-safety.cs)] > [!NOTE] -> C and C++ developers, notice that in C#, [bool](language-reference/builtin-types/bool.md) is not convertible to [int](language-reference/builtin-types/integral-numeric-types.md). +> C and C++ developers, notice that in C#, [bool](../../language-reference/builtin-types/bool.md) is not convertible to [int](../../language-reference/builtin-types/integral-numeric-types.md). The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory. ## Specifying types in variable declarations -When you declare a variable or constant in a program, you must either specify its type or use the [var](language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: +When you declare a variable or constant in a program, you must either specify its type or use the [var](../../language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: -[!code-csharp[Variable Declaration](../../samples/snippets/csharp/concepts/basic-types/variable-declaration.cs)] +[!code-csharp[Variable Declaration](../../../../samples/snippets/csharp/concepts/basic-types/variable-declaration.cs)] -The types of method parameters and return values are specified in the method signature. The following signature shows a method that requires an [int](language-reference/builtin-types/integral-numeric-types.md) as an input argument and returns a string: +The types of method parameters and return values are specified in the method signature. The following signature shows a method that requires an [int](../../language-reference/builtin-types/integral-numeric-types.md) as an input argument and returns a string: -[!code-csharp[Method Signature](../../samples/snippets/csharp/concepts/basic-types/method-signature.cs)] +[!code-csharp[Method Signature](../../../../samples/snippets/csharp/concepts/basic-types/method-signature.cs)] -After a variable is declared, it cannot be re-declared with a new type, and it cannot be assigned a value that is not compatible with its declared type. For example, you cannot declare an [int](language-reference/builtin-types/integral-numeric-types.md) and then assign it a Boolean value of `true`. However, values can be converted to other types, for example when they are assigned to new variables or passed as method arguments. A *type conversion* that does not cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code. +After a variable is declared, it cannot be re-declared with a new type, and it cannot be assigned a value that is not compatible with its declared type. For example, you cannot declare an [int](../../language-reference/builtin-types/integral-numeric-types.md) and then assign it a Boolean value of `true`. However, values can be converted to other types, for example when they are assigned to new variables or passed as method arguments. A *type conversion* that does not cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code. -For more information, see [Casting and type conversions](programming-guide/types/casting-and-type-conversions.md). +For more information, see [Casting and type conversions](../../programming-guide/types/casting-and-type-conversions.md). ## Built-in types -C# provides a standard set of built-in numeric types to represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. There are also built-in **string** and **object** types. These are available for you to use in any C# program. For the complete list of the built-in types, see [Built-in types](language-reference/builtin-types/built-in-types.md). +C# provides a standard set of built-in numeric types to represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. There are also built-in **string** and **object** types. These are available for you to use in any C# program. For the complete list of the built-in types, see [Built-in types](../../language-reference/builtin-types/built-in-types.md). ## Custom types -You use the [struct](language-reference/builtin-types/struct.md), [class](language-reference/keywords/class.md), [record](language-reference/builtin-types/record.md), [interface](language-reference/keywords/interface.md), and [enum](language-reference/builtin-types/enum.md) constructs to create your own custom types. The .NET class library itself is a collection of custom types provided by Microsoft that you can use in your own applications. By default, the most frequently used types in the class library are available in any C# program. Others become available only when you explicitly add a project reference to the assembly in which they are defined. After the compiler has a reference to the assembly, you can declare variables (and constants) of the types declared in that assembly in source code. +You use the [struct](../../language-reference/builtin-types/struct.md), [class](../../language-reference/keywords/class.md), [record](../../language-reference/builtin-types/record.md), [interface](../../language-reference/keywords/interface.md), and [enum](../../language-reference/builtin-types/enum.md) constructs to create your own custom types. The .NET class library itself is a collection of custom types provided by Microsoft that you can use in your own applications. By default, the most frequently used types in the class library are available in any C# program. Others become available only when you explicitly add a project reference to the assembly in which they are defined. After the compiler has a reference to the assembly, you can declare variables (and constants) of the types declared in that assembly in source code. ## Generic types A type can be declared with one or more *type parameters* that serve as a placeholder for the actual type (the *concrete type*) that client code will provide when it creates an instance of the type. Such types are called *generic types*. For example, has one type parameter that by convention is given the name *T*. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, string: -[!code-csharp[Generic types](../../samples/snippets/csharp/concepts/basic-types/generic-type.cs)] +[!code-csharp[Generic types](../../../../samples/snippets/csharp/concepts/basic-types/generic-type.cs)] -The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to [object](language-reference/builtin-types/reference-types.md#the-object-type). Generic collection classes are called *strongly typed collections* because the compiler knows the specific type of the collection's elements and can raise an error at compile-time if, for example, you try to add an integer to the `strings` object in the previous example. For more information, see [Generics](programming-guide/generics/index.md). +The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to [object](../../language-reference/builtin-types/reference-types.md#the-object-type). Generic collection classes are called *strongly typed collections* because the compiler knows the specific type of the collection's elements and can raise an error at compile-time if, for example, you try to add an integer to the `strings` object in the previous example. For more information, see [Generics](generics.md). ## Implicit types, anonymous types, and tuple types -As stated previously, you can implicitly type a local variable (but not class members) by using the [var](language-reference/keywords/var.md) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly typed local variables](programming-guide/classes-and-structs/implicitly-typed-local-variables.md). +As stated previously, you can implicitly type a local variable (but not class members) by using the [var](../../language-reference/keywords/var.md) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly typed local variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). -In some cases, it is inconvenient to create a named type for simple sets of related values that you do not intend to store or pass outside method boundaries. You can create *anonymous types* for this purpose. For more information, see [Anonymous types](programming-guide/classes-and-structs/anonymous-types.md). +In some cases, it is inconvenient to create a named type for simple sets of related values that you do not intend to store or pass outside method boundaries. You can create *anonymous types* for this purpose. For more information, see [Anonymous types](anonymous-types.md). -It's common to want to return more than one value from a method. You can create *tuple types* that return multiple values in a single method call. For more information, see [Tuple types](language-reference/builtin-types/value-tuples.md). +It's common to want to return more than one value from a method. You can create *tuple types* that return multiple values in a single method call. For more information, see [Tuple types](../../language-reference/builtin-types/value-tuples.md). ## The Common type system It is important to understand two fundamental points about the type system in .NET: -- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: `object`). This unified type hierarchy is called the [Common type system](../standard/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](programming-guide/classes-and-structs/inheritance.md). +- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: `object`). This unified type hierarchy is called the [Common type system](../../../standard/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). -- Each type in the CTS is defined as either a *value type* or a *reference type*. This includes all custom types in the .NET class library and also your own user-defined types. Types that you define by using the `struct` or `enum` keyword are value types. For more information about value types, see [Value types](language-reference/builtin-types/value-types.md). Types that you define by using the [class](language-reference/keywords/class.md) keyword are reference types. For more information about reference types, see [Classes](programming-guide/classes-and-structs/classes.md). Reference types and value types have different compile-time rules, and different run-time behavior. - -## See also - -- [Structure types](language-reference/builtin-types/struct.md) -- [Enumeration types](language-reference/builtin-types/enum.md) -- [Classes](programming-guide/classes-and-structs/classes.md) +- Each type in the CTS is defined as either a *value type* or a *reference type*. This includes all custom types in the .NET class library and also your own user-defined types. Types that you define by using the `struct` or `enum` keyword are value types. For more information about value types, see [Value types](../../language-reference/builtin-types/value-types.md). Types that you define by using the [class](../../language-reference/keywords/class.md) keyword are reference types. For more information about reference types, see [Classes](classes.md). Reference types and value types have different compile-time rules, and different run-time behavior. diff --git a/docs/csharp/fundamentals/types/classes.md b/docs/csharp/fundamentals/types/classes.md index 6ef71ba726b34..edd0e894ce972 100644 --- a/docs/csharp/fundamentals/types/classes.md +++ b/docs/csharp/fundamentals/types/classes.md @@ -35,7 +35,7 @@ When the object is created, enough memory is allocated on the managed heap for t } ``` - The `class` keyword is preceded by the access level. Because [public](../../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 name of the class must be a valid C# [identifier name](../inside-a-program/identifier-names.md). 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*. + The `class` keyword is preceded by the access level. Because [public](../../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 name of the class must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). 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 @@ -76,32 +76,20 @@ Inheritance is accomplished by using a *derivation*, which means a class is decl } ``` -When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](inheritance.md). +When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](../../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 one or more interfaces. For more information, see [Interfaces](../interfaces/index.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 one or more interfaces. For more information, see [Interfaces](interfaces.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](abstract-and-sealed-classes-and-class-members.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 Classes and Methods](partial-classes-and-methods.md). +Class definitions can be split between different source files. For more information, see [Partial Classes and Methods](../../programming-guide/classes-and-structs/partial-classes-and-methods.md). ## Example -The following example defines a public class that contains an [auto-implemented property](auto-implemented-properties.md), a method, and a special method called a constructor. For more information, see [Properties](properties.md), [Methods](methods.md), and [Constructors](constructors.md) topics. The instances of the class are then instantiated with the `new` keyword. +The following example defines a public class that contains an [auto-implemented property](../../programming-guide/classes-and-structs/auto-implemented-properties.md), a method, and a special method called a constructor. For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md), [Methods](../../programming-guide/classes-and-structs/methods.md), and [Constructors](../../programming-guide/classes-and-structs/constructors.md) topics. The instances of the class are then instantiated with the `new` keyword. [!code-csharp[Class Example](~/samples/snippets/csharp/programming-guide/classes-and-structs/class-example.cs)] ## C# Language Specification [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See also - -- [C# Programming Guide](../index.md) -- [Object-Oriented Programming](../../tutorials/intro-to-csharp/object-oriented-programming.md) -- [Polymorphism](polymorphism.md) -- [Identifier names](../inside-a-program/identifier-names.md) -- [Members](members.md) -- [Methods](methods.md) -- [Constructors](constructors.md) -- [Finalizers](destructors.md) -- [Objects](objects.md) diff --git a/docs/csharp/fundamentals/types/generics.md b/docs/csharp/fundamentals/types/generics.md index 81165a0f70771..1e0199663efd6 100644 --- a/docs/csharp/fundamentals/types/generics.md +++ b/docs/csharp/fundamentals/types/generics.md @@ -38,18 +38,6 @@ The following code example shows how client code uses the generic `GenericList -- [C# Programming Guide](../index.md) -- [Types](../types/index.md) -- [\](../xmldoc/typeparam.md) -- [\](../xmldoc/typeparamref.md) - [Generics in .NET](../../../standard/generics/index.md) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index e6b0edd1f450f..2709a31ccc49a 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -31,7 +31,7 @@ The information stored in a type can include the following items: The compiler uses type information to make sure all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [int](../../language-reference/builtin-types/integral-numeric-types.md), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [bool](../../language-reference/builtin-types/bool.md), the compiler generates an error, as shown in the following example: -:::code language="csharp" source="snippets/index/Program.cs" id="TypeSafeExample"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="TypeSafeExample"::: > [!NOTE] > C and C++ developers, notice that in C#, [bool](../../language-reference/builtin-types/bool.md) is not convertible to [int](../../language-reference/builtin-types/integral-numeric-types.md). @@ -42,15 +42,15 @@ The compiler embeds the type information into the executable file as metadata. T When you declare a variable or constant in a program, you must either specify its type or use the [var](../../language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: -:::code language="csharp" source="snippets/index/Program.cs" id="Declarations"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="Declarations"::: The types of method parameters and return values are specified in the method declaration. The following signature shows a method that requires an [int](../../language-reference/builtin-types/integral-numeric-types.md) as an input argument and returns a string: -:::code language="csharp" source="snippets/index/Program.cs" id="GetName"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="GetName"::: After you declare a variable, you can't redeclare it with a new type, and you can't assign a value not compatible with its declared type. For example, you can't declare an [int](../../language-reference/builtin-types/integral-numeric-types.md) and then assign it a Boolean value of `true`. However, values can be converted to other types, for example when they're assigned to new variables or passed as method arguments. A *type conversion* that doesn't cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code. -For more information, see [Casting and Type Conversions](./casting-and-type-conversions.md). +For more information, see [Casting and Type Conversions](../../programming-guide/types/casting-and-type-conversions.md). ## Built-in types @@ -64,12 +64,12 @@ You use the [struct](../../language-reference/builtin-types/struct.md), [class]( It's important to understand two fundamental points about the type system in .NET: -- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: [int](../../language-reference/builtin-types/integral-numeric-types.md)), derive ultimately from a single base type, which is (C# keyword: [object](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../classes-and-structs/inheritance.md). +- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: [int](../../language-reference/builtin-types/integral-numeric-types.md)), derive ultimately from a single base type, which is (C# keyword: [object](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). - Each type in the CTS is defined as either a *value type* or a *reference type*. These types include all custom types in the .NET class library and also your own user-defined types. Types that you define by using the [struct](../../language-reference/builtin-types/struct.md) keyword are value types; all the built-in numeric types are `structs`. Types that you define by using the [class](../../language-reference/keywords/class.md) or [record](../../language-reference/builtin-types/record.md) keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior. The following illustration shows the relationship between value types and reference types in the CTS. -![Screenshot that shows CTS value types and reference types.](./media/index/value-reference-types-common-type-system.png) +![Screenshot that shows CTS value types and reference types.](../../programming-guide/types/media/index/value-reference-types-common-type-system.png) > [!NOTE] > You can see that the most commonly used types are all organized in the namespace. However, the namespace in which a type is contained has no relation to whether it is a value type or reference type. @@ -82,23 +82,23 @@ There are two categories of value types: [struct](../../language-reference/built The built-in numeric types are structs, and they have fields and methods that you can access: -:::code language="csharp" source="snippets/index/Program.cs" id="ConstantByte"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="ConstantByte"::: But you declare and assign values to them as if they're simple non-aggregate types: -:::code language="csharp" source="snippets/index/Program.cs" id="NonAggregateTypes"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="NonAggregateTypes"::: -Value types are *sealed*, which means that you can't derive a type from any value type, for example . You can't define a struct to inherit from any user-defined class or struct because a struct can only inherit from . However, a struct can implement one or more interfaces. You can cast a struct type to any interface type that it implements; this cast causes a *boxing* operation to wrap the struct inside a reference type object on the managed heap. Boxing operations occur when you pass a value type to a method that takes a or any interface type as an input parameter. For more information, see [Boxing and Unboxing](./boxing-and-unboxing.md). +Value types are *sealed*, which means that you can't derive a type from any value type, for example . You can't define a struct to inherit from any user-defined class or struct because a struct can only inherit from . However, a struct can implement one or more interfaces. You can cast a struct type to any interface type that it implements; this cast causes a *boxing* operation to wrap the struct inside a reference type object on the managed heap. Boxing operations occur when you pass a value type to a method that takes a or any interface type as an input parameter. For more information, see [Boxing and Unboxing](../../programming-guide/types/boxing-and-unboxing.md). You use the [struct](../../language-reference/builtin-types/struct.md) keyword to create your own custom value types. Typically, a struct is used as a container for a small set of related variables, as shown in the following example: -:::code language="csharp" source="snippets/index/Program.cs" id="Coords"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="Coords"::: For more information about structs, see [Structure types](../../language-reference/builtin-types/struct.md). For more information about value types, see [Value types](../../language-reference/builtin-types/value-types.md). The other category of value types is [enum](../../language-reference/builtin-types/enum.md). An enum defines a set of named integral constants. For example, the enumeration in the .NET class library contains a set of named constant integers that specify how a file should be opened. It's defined as shown in the following example: -:::code language="csharp" source="snippets/index/Program.cs" id="EnumFileMode"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="EnumFileMode"::: The `System.IO.FileMode.Create` constant has a value of 2. However, the name is much more meaningful for humans reading the source code, and for that reason it's better to use enumerations instead of constant literal numbers. For more information, see . @@ -108,17 +108,17 @@ All enums inherit from , which in A type that is defined as a [class](../../language-reference/keywords/class.md), [record](../../language-reference/builtin-types/record.md), [delegate](../../language-reference/builtin-types/reference-types.md), array, or [interface](../../language-reference/keywords/interface.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 object by using the [new](../../language-reference/operators/new-operator.md) operator, or assign it an object that has been created elsewhere by using `new`, as shown in the following example: -:::code language="csharp" source="snippets/index/Program.cs" id="DeclarationAndAssignment"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="DeclarationAndAssignment"::: An interface must be initialized together with a class object that implements it. If `MyClass` implements `IMyInterface`, you create an instance of `IMyInterface` as shown in the following example: -:::code language="csharp" source="snippets/index/Program.cs" id="InterfaceDeclaration"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="InterfaceDeclaration"::: 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're allocated and when they're 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 doesn't create a performance issue. For more information about garbage collection, see [Automatic Memory Management](../../../standard/automatic-memory-management.md). All arrays are reference types, even if their elements are value types. Arrays implicitly derive from the class, but you declare and use them with the simplified syntax that is provided by C#, as shown in the following example: -:::code language="csharp" source="snippets/index/Program.cs" id="ArrayDeclaration"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="ArrayDeclaration"::: Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](../classes-and-structs/index.md). For more information about inheritance and virtual methods, see [Inheritance](../classes-and-structs/inheritance.md). @@ -128,21 +128,21 @@ In C#, literal values receive a type from the compiler. You can specify how a nu Because literals are typed, and all types derive ultimately from , you can write and compile code such as the following code: -:::code language="csharp" source="snippets/index/Program.cs" id="ConvertTypes"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="ConvertTypes"::: ## Generic types A type can be declared with one or more *type parameters* that serve as a placeholder for the actual type (the *concrete type*) that client code will provide when it creates an instance of the type. Such types are called *generic types*. For example, the .NET type has one type parameter that by convention is given the name *T*. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, string: -:::code language="csharp" source="snippets/index/Program.cs" id="GenericType"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="GenericType"::: -The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to [object](../../language-reference/builtin-types/reference-types.md). Generic collection classes are called *strongly typed collections* because the compiler knows the specific type of the collection's elements and can raise an error at compile time if, for example, you try to add an integer to the `stringList` object in the previous example. For more information, see [Generics](../generics/index.md). +The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to [object](../../language-reference/builtin-types/reference-types.md). Generic collection classes are called *strongly typed collections* because the compiler knows the specific type of the collection's elements and can raise an error at compile time if, for example, you try to add an integer to the `stringList` object in the previous example. For more information, see [Generics](generics.md). ## Implicit types, anonymous types, and nullable value types -As stated previously, you can implicitly type a local variable (but not class members) by using the [var](../../language-reference/keywords/var.md) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly Typed Local Variables](../classes-and-structs/implicitly-typed-local-variables.md). +As stated previously, you can implicitly type a local variable (but not class members) by using the [var](../../language-reference/keywords/var.md) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). -It can be inconvenient to create a named type for simple sets of related values that you don't intend to store or pass outside method boundaries. You can create *anonymous types* for this purpose. For more information, see [Anonymous Types](../classes-and-structs/anonymous-types.md). +It can be inconvenient to create a named type for simple sets of related values that you don't intend to store or pass outside method boundaries. You can create *anonymous types* for this purpose. For more information, see [Anonymous Types](anonymous-types.md). Ordinary value types can't have a value of [null](../../language-reference/keywords/null.md). However, you can create nullable value types by appending a `?` after the type. For example, `int?` is an `int` type that can also have the value [null](../../language-reference/keywords/null.md). Nullable value types are instances of the generic struct type . Nullable value types are especially useful when you're passing data to and from databases in which numeric values might be null. For more information, see [Nullable value types](../../language-reference/builtin-types/nullable-value-types.md). @@ -150,16 +150,17 @@ Ordinary value types can't have a value of [null](../../language-reference/keywo A variable can have different compile-time and run-time types. The *compile-time type* is the declared or inferred type of the variable in the source code. The *run-time type* is the type of the instance referred to by that variable. Often those two types are the same, as in the following example: -:::code language="csharp" source="snippets/index/Program.cs" id="CompileTimeType"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="CompileTimeType"::: In other cases, the compile-time type is different, as shown in the following two examples: -:::code language="csharp" source="snippets/index/Program.cs" id="RuntimeTypes"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="RuntimeTypes"::: In both of the preceding examples, the run-time type is a `string`. The compile-time type is `object` in the first line, and `IEnumerable` in the second. If the two types are different for a variable, it's important to understand when the compile-time type and the run-time type apply. The compile-time type determines all the actions taken by the compiler. These compiler actions include method call resolution, overload resolution, and available implicit and explicit casts. The run-time type determines all actions that are resolved at run time. These run-time actions include dispatching virtual method calls, evaluating `is` and `switch` expressions, and other type testing APIs. To better understand how your code interacts with types, recognize which action applies to which type. +<<<<<<< HEAD ## Related sections For more information, see the following articles: @@ -173,13 +174,8 @@ For more information, see the following articles: - [Anonymous Types](../classes-and-structs/anonymous-types.md) - [Generics](../generics/index.md) +======= +>>>>>>> 1087669fbb (fix warnings in the fundamentals area) ## C# language specification [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See also - -- [C# Reference](../../language-reference/index.md) -- [C# Programming Guide](../index.md) -- [Conversion of XML Data Types](../../../standard/data/xml/conversion-of-xml-data-types.md) -- [Integral types](../../language-reference/builtin-types/integral-numeric-types.md) diff --git a/docs/csharp/fundamentals/types/interfaces.md b/docs/csharp/fundamentals/types/interfaces.md index e6f6d57cabb1a..c41019bb79188 100644 --- a/docs/csharp/fundamentals/types/interfaces.md +++ b/docs/csharp/fundamentals/types/interfaces.md @@ -17,15 +17,15 @@ You define an interface by using the [interface](../../language-reference/keywor :::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" id="Equatable"::: -The name of an interface must be a valid C# [identifier name](../inside-a-program/identifier-names.md). By convention, interface names begin with a capital `I`. +The name of an interface must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). By convention, interface names begin with a capital `I`. Any class or struct that implements the interface must contain a definition for an method that matches the signature that the interface specifies. As a result, you can count on a class that implements `IEquatable` to contain an `Equals` method with which an instance of the class can determine whether it's equal to another instance of the same class. The definition of `IEquatable` doesn't provide an implementation for `Equals`. A class or struct can implement multiple interfaces, but a class can only inherit from a single class. -For more information about abstract classes, see [Abstract and Sealed Classes and Class Members](../classes-and-structs/abstract-and-sealed-classes-and-class-members.md). +For more information about abstract classes, see [Abstract and Sealed Classes and Class Members](../../programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md). -Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators. For links to examples, see [Related Sections](./index.md#BKMK_RelatedSections). An interface can't contain instance fields, instance constructors, or finalizers. Interface members are public by default, and you can explicitly specify accessibility modifiers, such as `public`, `protected`, `internal`, `private`, `protected internal`, or `private protected`. A `private` member must have a default implementation. +Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators. An interface can't contain instance fields, instance constructors, or finalizers. Interface members are public by default, and you can explicitly specify accessibility modifiers, such as `public`, `protected`, `internal`, `private`, `protected internal`, or `private protected`. A `private` member must have a default implementation. To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member. @@ -35,11 +35,11 @@ The following example shows an implementation of the Related Sections - -- [Interface Properties](../classes-and-structs/interface-properties.md) -- [Indexers in Interfaces](../indexers/indexers-in-interfaces.md) -- [How to implement interface events](../events/how-to-implement-interface-events.md) -- [Classes, structs, and records](../classes-and-structs/index.md) -- [Inheritance](../classes-and-structs/inheritance.md) -- [Interfaces](../../language-reference/keywords/interface.md) -- [Methods](../classes-and-structs/methods.md) -- [Polymorphism](../classes-and-structs/polymorphism.md) -- [Abstract and Sealed Classes and Class Members](../classes-and-structs/abstract-and-sealed-classes-and-class-members.md) -- [Properties](../classes-and-structs/properties.md) -- [Events](../events/index.md) -- [Indexers](../indexers/index.md) - -## See also - -- [C# Programming Guide](../index.md) -- [Inheritance](../classes-and-structs/inheritance.md) -- [Identifier names](../inside-a-program/identifier-names.md) diff --git a/docs/csharp/fundamentals/types/namespaces.md b/docs/csharp/fundamentals/types/namespaces.md index 28b62c6db1ef0..a6a65790bf7df 100644 --- a/docs/csharp/fundamentals/types/namespaces.md +++ b/docs/csharp/fundamentals/types/namespaces.md @@ -25,7 +25,7 @@ Second, declaring your own namespaces can help you control the scope of class an [!code-csharp[csProgGuideNamespaces#6](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs#6)] -The name of the namespace must be a valid C# [identifier name](../inside-a-program/identifier-names.md). +The name of the namespace must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). ## Namespaces overview @@ -39,12 +39,3 @@ Namespaces have the following properties: ## C# language specification For more information, see the [Namespaces](~/_csharplang/spec/namespaces.md) section of the [C# language specification](~/_csharplang/spec/introduction.md). - -## See also - -- [C# Programming Guide](../index.md) -- [Using Namespaces](using-namespaces.md) -- [How to use the My namespace](how-to-use-the-my-namespace.md) -- [Identifier names](../inside-a-program/identifier-names.md) -- [using Directive](../../language-reference/keywords/using-directive.md) -- [:: Operator](../../language-reference/operators/namespace-alias-qualifier.md) diff --git a/docs/csharp/fundamentals/types/records.md b/docs/csharp/fundamentals/types/records.md index cd98ca8551dbf..e8ac4012d0463 100644 --- a/docs/csharp/fundamentals/types/records.md +++ b/docs/csharp/fundamentals/types/records.md @@ -15,7 +15,7 @@ A [record](../../language-reference/builtin-types/record.md) is a [class](../../ Consider using a record in place of a class in the following scenarios: * You want to define a reference type for which objects are immutable. -* You want to define a data model that depends on [value equality](../statements-expressions-operators/equality-comparisons.md#value-equality). +* You want to define a data model that depends on [value equality](../../programming-guide/statements-expressions-operators/equality-comparisons.md#value-equality). ### Immutability @@ -25,7 +25,7 @@ Immutability isn't appropriate for all data scenarios. [Entity Framework Core](/ ### Value equality -For records, value equality means that two variables of a record type are equal if the types match and all property and field values match. For other reference types such as classes, equality means [reference equality](../statements-expressions-operators/equality-comparisons.md#reference-equality). That is, two variables of a class type are equal if they refer to the same object. Methods and operators that determine equality of two record instances use value equality. +For records, value equality means that two variables of a record type are equal if the types match and all property and field values match. For other reference types such as classes, equality means [reference equality](../../programming-guide/statements-expressions-operators/equality-comparisons.md#reference-equality). That is, two variables of a class type are equal if they refer to the same object. Methods and operators that determine equality of two record instances use value equality. Not all data models work well with value equality. For example, [Entity Framework Core](/ef/core/) depends on reference equality to ensure that it uses only one instance of an entity type for what is conceptually one entity. For this reason, record types aren't appropriate for use as entity types in Entity Framework Core. @@ -58,17 +58,3 @@ For more information, see [Records (C# reference)](../../language-reference/buil ## C# Language Specification [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See also - -- [Classes (C# Programming Guide)](classes.md) -- [Records (C# reference)](../../language-reference/builtin-types/record.md) -- [C# Programming Guide](../index.md) -- [Object-Oriented Programming](../../tutorials/intro-to-csharp/object-oriented-programming.md) -- [Polymorphism](polymorphism.md) -- [Identifier names](../inside-a-program/identifier-names.md) -- [Members](members.md) -- [Methods](methods.md) -- [Constructors](constructors.md) -- [Finalizers](destructors.md) -- [Objects](objects.md) diff --git a/docs/csharp/index.yml b/docs/csharp/index.yml index 000c29e9b3e0a..0bf0363d1e80a 100644 --- a/docs/csharp/index.yml +++ b/docs/csharp/index.yml @@ -48,7 +48,7 @@ landingContent: - linkListType: concept links: - text: "The C# and .NET type system" - url: programming-guide/types/index.md + url: fundamentals/types/index.md - text: Language Integrated Query (LINQ) url: linq/index.md - text: async and await diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 472ea85b17bae..3c26ba3dbf0f9 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -134,8 +134,6 @@ items: href: indexers.md - name: Discards href: discards.md - - name: Generics - href: programming-guide/generics/index.md - name: Iterators href: iterators.md - name: Delegates & events @@ -679,8 +677,6 @@ items: href: programming-guide/classes-and-structs/nested-types.md - name: Partial Classes and Methods href: programming-guide/classes-and-structs/partial-classes-and-methods.md - - name: Anonymous Types - href: programming-guide/classes-and-structs/anonymous-types.md - name: "How to return subsets of element properties in a query" href: programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md - name: Interfaces @@ -755,9 +751,6 @@ items: href: programming-guide/events/how-to-implement-custom-event-accessors.md - name: Generics items: - - name: Overview - displayName: generics - href: programming-guide/generics/index.md - name: Generic Type Parameters href: programming-guide/generics/generic-type-parameters.md - name: Constraints on Type Parameters From c00b425bb2a1762d59a7a23554cf746ef5bb4e6e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 30 Mar 2021 12:16:49 -0400 Subject: [PATCH 04/60] update ms-technology metadata. --- docfx.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docfx.json b/docfx.json index bdd51a558c161..60e2e07b833f4 100644 --- a/docfx.json +++ b/docfx.json @@ -427,6 +427,7 @@ "docs/csharp/whats-new/**/**.md": "csharp-whats-new", "docs/csharp/how-to/**/**.md": "csharp-fundamentals", "docs/csharp/linq/**/**.md": "csharp-linq", + "docs/csharp/fundamentals/**/**.md": "csharp-fundamentals", "docs/csharp/programming-guide/main-and-command-args/**/**.md": "csharp-fundamentals", "docs/csharp/programming-guide/indexers/**/**.md": "csharp-fundamentals", "docs/csharp/programming-guide/generics/**/**.md": "csharp-fundamentals", @@ -452,7 +453,6 @@ "docs/csharp/programming-guide/events/**/**.md": "csharp-fundamentals", "docs/csharp/programming-guide/inside-a-program/**/**.md": "csharp-get-started", "docs/csharp/programming-guide/interfaces/**/**.md": "csharp-fundamentals", - "docs/csharp/getting-started/**/**.md": "csharp-get-started", "docs/csharp/tutorials/**/**.md": "csharp-fundamentals", "docs/csharp/tutorials/exploration/**/**.md": "csharp-get-started", "docs/csharp/tutorials/intro-to-csharp/**/**.md": "csharp-get-started", From 2444296f95fef6eec8f4f3d9a199c49e7077b3fe Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 6 May 2021 17:05:12 -0400 Subject: [PATCH 05/60] Proposed fundamentals TOC This has a good fundamentals TOC. Also, add redirections for articles that have moved already. --- .openpublishing.redirection.json | 32 +++++++++++++ docs/csharp/toc.yml | 80 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 64b062b20a007..cc728cba58df4 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -875,6 +875,10 @@ "source_path": "docs/core/windows-prerequisites.md", "redirect_url": "/dotnet/core/install/windows#dependencies" }, + { + "source_path": "docs/csharp/basic-types.md", + "redirect_url": "/dotnet/csharp/fundamentals/types/basic-types" + }, { "source_path": "docs/csharp/classes.md", "redirect_url": "/dotnet/csharp/programming-guide/classes-and-structs/classes" @@ -1788,10 +1792,22 @@ "source_path": "docs/csharp/programming-guide/arrays/passing-arrays-using-ref-and-out.md", "redirect_url": "/dotnet/csharp/programming-guide/arrays" }, + { + "source_path": "docs/csharp/programming-guide/classes-and-structs/anonymous-types.md", + "redirect_url": "/dotnet/csharp/fundamentals/types/anonymous-types" + }, + { + "source_path": "docs/csharp/programming-guide/classes-and-structs/classes.md", + "redirect_url": "/dotnet/csharp/fundamentals/types/classes" + }, { "source_path": "docs/csharp/programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md", "redirect_url": "/dotnet/csharp/language-reference/keywords/foreach-in" }, + { + "source_path": "docs/csharp/programming-guide/classes-and-structs/records.md", + "redirect_url": "/dotnet/csharp/fundamentals/types/records" + }, { "source_path": "docs/csharp/programming-guide/classes-and-structs/structs.md", "redirect_url": "/dotnet/csharp/language-reference/builtin-types/struct" @@ -2785,6 +2801,10 @@ "source_path": "docs/csharp/programming-guide/generics/generics-in-the-net-framework-class-library.md", "redirect_url": "/dotnet/standard/generics/index" }, + { + "source_path": "docs/csharp/programming-guide/generics/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/types/generics" + }, { "source_path": "docs/csharp/programming-guide/generics/introduction-to-generics.md", "redirect_url": "/dotnet/standard/generics/index" @@ -2989,6 +3009,18 @@ "source_path": "docs/csharp/programming-guide/types/how-to-safely-cast-using-as-and-is-operators.md", "redirect_url": "/dotnet/csharp/how-to/safely-cast-using-pattern-matching-is-and-as-operators" }, + { + "source_path": "docs/csharp/programming-guide/interfaces/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/types/interfaces" + }, + { + "source_path": "docs/csharp/programming-guide/namespaces/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/types/namespaces" + }, + { + "source_path": "docs/csharp/programming-guide/types/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/types" + }, { "source_path": "docs/csharp/programming-guide/unsafe-code-pointers/arithmetic-operations-on-pointers.md", "redirect_url": "/dotnet/csharp/language-reference/operators/pointer-related-operators#pointer-arithmetic-operators" diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 3c26ba3dbf0f9..365bfdee89499 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -57,6 +57,86 @@ items: href: fundamentals/types/generics.md - name: Anonymous Types href: fundamentals/types/anonymous-types.md + - name: Tutorials + items: + - name: Cast using is and as operators + href: how-to/safely-cast-using-pattern-matching-is-and-as-operators.md + # DTOs (tuples, anonymous types and records) + # Values (record structs and structs) + # Classes and objects (interfaces and classes) + # Generics (methods and classes) + - name: Program structure + items: + - name: Overview + href: programming-guide/inside-a-program/general-structure-of-a-csharp-program.md + - name: Top-level statements + href: programming-guide/main-and-command-args/top-level-statements.md + - name: Main method + href: programming-guide/main-and-command-args/index.md + - name: Main() Return Values + href: programming-guide/main-and-command-args/main-return-values.md + - name: Command-Line Arguments + href: programming-guide/main-and-command-args/command-line-arguments.md + - name: Tutorials + # Command line processing tutorials + - name: Object Oriented programming + items: + - name: Encapsulation + href: programming-guide/classes-and-structs/index.md + - name: Inheritance + href: programming-guide/classes-and-structs/inheritance.md + - name: Polymorphism + href: programming-guide/classes-and-structs/polymorphism.md + - name: Tutorials + items: + - name: Introduction to classes + href: tutorials/intro-to-csharp/introduction-to-classes.md + - name: Object-Oriented C# + href: tutorials/intro-to-csharp/object-oriented-programming.md + # Composing objects from parts + # Inheritance + # programming to interfaces + - name: Functional techniques + items: + # - Conceptual overview + # - Expressions (how different than OO and expressions?) + # - separation of data & algorithms + # - immutability (emphasize constructors & passing data to constructor, get/init) + # - Functional purity / functions as arguments or return values + # In F#, discussion of "object programming" -> objects become more sophisticated records. + # - language features: + # - Lambdas + # - Positional records as DTOs + - name: Pattern Matching + href: pattern-matching.md + - name: Discards + href: discards.md + - name: Deconstructing tuples and other types + href: deconstruct.md + - name: Tutorials + # separating data and algorithms + # Data transformations + - name: Exceptions and errors + items: + - name: Overview + displayName: exceptions + href: programming-guide/exceptions/index.md + - name: Using Exceptions + href: programming-guide/exceptions/using-exceptions.md + - name: Exception Handling + href: programming-guide/exceptions/exception-handling.md + - name: Creating and Throwing Exceptions + href: programming-guide/exceptions/creating-and-throwing-exceptions.md + - name: Compiler-Generated Exceptions + href: programming-guide/exceptions/compiler-generated-exceptions.md + - name: How to + items: + - name: "How to handle an exception using try-catch" + href: programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md + - name: "How to execute cleanup code using finally" + href: programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md + - name: "How to catch a non-CLS exception" + href: programming-guide/exceptions/how-to-catch-a-non-cls-exception.md - name: What's new in C# items: - name: C# 9.0 From 8d5ddfdc83ac23747d8cc5679b728f4f252781f8 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 6 May 2021 17:08:19 -0400 Subject: [PATCH 06/60] fix a bad merge --- docs/csharp/fundamentals/types/index.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 2709a31ccc49a..9387baea7b310 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -160,7 +160,6 @@ In both of the preceding examples, the run-time type is a `string`. The compile- If the two types are different for a variable, it's important to understand when the compile-time type and the run-time type apply. The compile-time type determines all the actions taken by the compiler. These compiler actions include method call resolution, overload resolution, and available implicit and explicit casts. The run-time type determines all actions that are resolved at run time. These run-time actions include dispatching virtual method calls, evaluating `is` and `switch` expressions, and other type testing APIs. To better understand how your code interacts with types, recognize which action applies to which type. -<<<<<<< HEAD ## Related sections For more information, see the following articles: @@ -174,8 +173,6 @@ For more information, see the following articles: - [Anonymous Types](../classes-and-structs/anonymous-types.md) - [Generics](../generics/index.md) -======= ->>>>>>> 1087669fbb (fix warnings in the fundamentals area) ## C# language specification [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] From b19ab3fdab8b4672a39f8bf53695daa0d843b656 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 6 May 2021 17:50:40 -0400 Subject: [PATCH 07/60] update see also section --- docs/csharp/fundamentals/types/index.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 9387baea7b310..259eb915c311c 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -164,14 +164,9 @@ If the two types are different for a variable, it's important to understand when For more information, see the following articles: -- [Casting and Type Conversions](./casting-and-type-conversions.md) -- [Boxing and Unboxing](./boxing-and-unboxing.md) -- [Using Type dynamic](./using-type-dynamic.md) - [Value Types](../../language-reference/builtin-types/value-types.md) - [Reference Types](../../language-reference/keywords/reference-types.md) -- [Classes, structs, and records](../classes-and-structs/index.md) -- [Anonymous Types](../classes-and-structs/anonymous-types.md) -- [Generics](../generics/index.md) +TODO: More language reference links ## C# language specification From e47a5eab1b53cbe1bca1474e741b5f4d6ad8f7e0 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 7 May 2021 11:31:22 -0400 Subject: [PATCH 08/60] One more set of TOC moves --- docs/csharp/toc.yml | 84 +++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 61 deletions(-) diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 365bfdee89499..f163ad3761760 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -77,10 +77,22 @@ items: href: programming-guide/main-and-command-args/main-return-values.md - name: Command-Line Arguments href: programming-guide/main-and-command-args/command-line-arguments.md + - name: What's inside a C# program + href: programming-guide/inside-a-program/index.md - name: Tutorials + items: + - name: "How to display command-line arguments" + href: programming-guide/main-and-command-args/how-to-display-command-line-arguments.md # Command line processing tutorials - name: Object Oriented programming items: + - name: Overview + displayName: classes, structs, records + href: programming-guide/classes-and-structs/index.md + - name: Objects + href: programming-guide/classes-and-structs/objects.md + - name: Inheritance + href: programming-guide/classes-and-structs/inheritance.md - name: Encapsulation href: programming-guide/classes-and-structs/index.md - name: Inheritance @@ -94,7 +106,8 @@ items: - name: Object-Oriented C# href: tutorials/intro-to-csharp/object-oriented-programming.md # Composing objects from parts - # Inheritance + - name: Inheritance in C# and .NET + href: tutorials/inheritance.md # programming to interfaces - name: Functional techniques items: @@ -114,6 +127,9 @@ items: - name: Deconstructing tuples and other types href: deconstruct.md - name: Tutorials + items: + - name: Build data-driven algorithms with pattern matching + href: tutorials/pattern-matching.md # separating data and algorithms # Data transformations - name: Exceptions and errors @@ -137,6 +153,12 @@ items: href: programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md - name: "How to catch a non-CLS exception" href: programming-guide/exceptions/how-to-catch-a-non-cls-exception.md + - name: Coding style + items: + - name: Identifier names + href: programming-guide/inside-a-program/identifier-names.md + - name: C# Coding Conventions + href: programming-guide/inside-a-program/coding-conventions.md - name: What's new in C# items: - name: C# 9.0 @@ -176,10 +198,6 @@ items: href: whats-new/tutorials/generate-consume-asynchronous-stream.md - name: Tutorials items: - - name: Introduction to classes - href: tutorials/intro-to-csharp/introduction-to-classes.md - - name: Object-Oriented programming - href: tutorials/intro-to-csharp/object-oriented-programming.md - name: Explore string interpolation - interactive href: tutorials/exploration/interpolated-strings.yml - name: Explore string interpolation - in your environment @@ -190,14 +208,10 @@ items: href: tutorials/console-teleprompter.md - name: REST Client href: tutorials/console-webapiclient.md - - name: Inheritance in C# and .NET - href: tutorials/inheritance.md - name: Work with LINQ href: tutorials/working-with-linq.md - name: Use Attributes href: tutorials/attributes.md - - name: Build data-driven algorithms with pattern matching - href: tutorials/pattern-matching.md - name: C# concepts items: - name: Nullable reference types @@ -371,29 +385,6 @@ items: - name: Overview displayName: programming guide href: programming-guide/index.md - - name: Inside a C# program - items: - - name: What's inside a C# program - href: programming-guide/inside-a-program/index.md - - name: General Structure of a C# Program - href: programming-guide/inside-a-program/general-structure-of-a-csharp-program.md - - name: Identifier names - href: programming-guide/inside-a-program/identifier-names.md - - name: C# Coding Conventions - href: programming-guide/inside-a-program/coding-conventions.md - - name: Main() and command-line arguments - items: - - name: Overview - displayName: main method - href: programming-guide/main-and-command-args/index.md - - name: Command-Line Arguments - href: programming-guide/main-and-command-args/command-line-arguments.md - - name: "How to display command-line arguments" - href: programming-guide/main-and-command-args/how-to-display-command-line-arguments.md - - name: Main() Return Values - href: programming-guide/main-and-command-args/main-return-values.md - - name: Top-level statements - href: programming-guide/main-and-command-args/top-level-statements.md - name: Programming concepts items: - name: Overview @@ -647,18 +638,8 @@ items: href: programming-guide/types/walkthrough-creating-and-using-dynamic-objects.md - name: Classes, Structs, and Records items: - - name: Overview - displayName: classes, structs, records - href: programming-guide/classes-and-structs/index.md - - name: Objects - href: programming-guide/classes-and-structs/objects.md - - name: Inheritance - href: programming-guide/classes-and-structs/inheritance.md - name: Polymorphism items: - - name: Overview - displayName: polymorphism - href: programming-guide/classes-and-structs/polymorphism.md - name: Versioning with the Override and New Keywords href: programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md - name: Knowing When to Use Override and New Keywords @@ -914,25 +895,6 @@ items: href: programming-guide/xmldoc/typeparamref.md - name: href: programming-guide/xmldoc/value.md - - name: Exceptions and Exception Handling - items: - - name: Overview - displayName: exceptions - href: programming-guide/exceptions/index.md - - name: Using Exceptions - href: programming-guide/exceptions/using-exceptions.md - - name: Exception Handling - href: programming-guide/exceptions/exception-handling.md - - name: Creating and Throwing Exceptions - href: programming-guide/exceptions/creating-and-throwing-exceptions.md - - name: Compiler-Generated Exceptions - href: programming-guide/exceptions/compiler-generated-exceptions.md - - name: "How to handle an exception using try-catch" - href: programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md - - name: "How to execute cleanup code using finally" - href: programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md - - name: "How to catch a non-CLS exception" - href: programming-guide/exceptions/how-to-catch-a-non-cls-exception.md - name: File System and the Registry items: - name: Overview From e5be4b5d26a682c70b56ca4a6ad2e0fe20ad9465 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 7 May 2021 12:20:32 -0400 Subject: [PATCH 09/60] Move program structure section Move and redirect all the program structure section articles. --- .openpublishing.redirection.json | 28 ++ .../program-structure/index.md} | 0 .../program-structure/main-command-line.md | 246 ++++++++++++++++++ .../top-level-statements.md | 0 .../how-to-display-command-line-arguments.md | 0 .../inside-a-program/index.md | 38 --- .../command-line-arguments.md | 93 ------- .../main-and-command-args/index.md | 63 ----- .../main-return-values.md | 111 -------- docs/csharp/toc.yml | 27 +- 10 files changed, 285 insertions(+), 321 deletions(-) rename docs/csharp/{programming-guide/inside-a-program/general-structure-of-a-csharp-program.md => fundamentals/program-structure/index.md} (100%) create mode 100644 docs/csharp/fundamentals/program-structure/main-command-line.md rename docs/csharp/{programming-guide/main-and-command-args => fundamentals/program-structure}/top-level-statements.md (100%) rename docs/csharp/{programming-guide/main-and-command-args => fundamentals/program-structure/tutorials}/how-to-display-command-line-arguments.md (100%) delete mode 100644 docs/csharp/programming-guide/inside-a-program/index.md delete mode 100644 docs/csharp/programming-guide/main-and-command-args/command-line-arguments.md delete mode 100644 docs/csharp/programming-guide/main-and-command-args/index.md delete mode 100644 docs/csharp/programming-guide/main-and-command-args/main-return-values.md diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index cc728cba58df4..79f23911d240e 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -2788,6 +2788,10 @@ "source_path": "docs/csharp/programming-guide/exceptions/exceptions-and-exception-handling.md", "redirect_url": "/dotnet/csharp/programming-guide/exceptions/index" }, + { + "source_path": "docs/csharp/programming-guide/inside-a-program/general-structure-of-a-csharp-program.md", + "redirect_url": "/dotnet/csharp/fundamentals/program-structure" + }, { "source_path": "docs/csharp/programming-guide/generics/benefits-of-generics.md", "redirect_url": "/dotnet/csharp/programming-guide/generics" @@ -2813,6 +2817,10 @@ "source_path": "docs/csharp/programming-guide/inside-a-program/hello-world-your-first-program.md", "redirect_url": "/dotnet/csharp/tutorials/intro-to-csharp/hello-world" }, + { + "source_path": "docs/csharp/programming-guide/inside-a-program/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/program-structure" + }, { "source_path": "docs/csharp/programming-guide/interop/interoperability.md", "redirect_url": "/dotnet/csharp/programming-guide/interop/index" @@ -2893,14 +2901,34 @@ "source_path": "docs/csharp/programming-guide/linq-query-expressions/query-expression-basics.md", "redirect_url": "/dotnet/csharp/linq/query-expression-basics" }, + { + "source_path": "docs/csharp/programming-guide/main-and-command-args/command-line-arguments.md", + "redirect_url": "/dotnet/csharp/fundamentals/program-structure/main-command-line" + }, { "source_path": "docs/csharp/programming-guide/main-and-command-args/how-to-access-command-line-arguments-using-foreach.md", "redirect_url": "/dotnet/csharp/programming-guide/arrays/using-foreach-with-arrays" }, + { + "source_path": "docs/csharp/programming-guide/main-and-command-args/how-to-display-command-line-arguments.md", + "redirect_url": "/dotnet/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments" + }, + { + "source_path": "docs/csharp/programming-guide/main-and-command-args/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/program-structure/main-command-line" + }, { "source_path": "docs/csharp/programming-guide/main-and-command-args/main-and-command-line-arguments.md", "redirect_url": "/dotnet/csharp/programming-guide/main-and-command-args/index" }, + { + "source_path": "docs/csharp/programming-guide/main-and-command-args/main-return-values.md", + "redirect_url": "/dotnet/csharp/fundamentals/program-structure/main-command-line" + }, + { + "source_path": "docs/csharp/programming-guide/main-and-command-args/top-level-statements.md", + "redirect_url": "/dotnet/csharp/fundamentals/program-structure/top-level-statements" + }, { "source_path": "docs/csharp/programming-guide/namespaces/how-to-use-the-global-namespace-alias.md", "redirect_url": "/dotnet/csharp/language-reference/operators/namespace-alias-qualifier" diff --git a/docs/csharp/programming-guide/inside-a-program/general-structure-of-a-csharp-program.md b/docs/csharp/fundamentals/program-structure/index.md similarity index 100% rename from docs/csharp/programming-guide/inside-a-program/general-structure-of-a-csharp-program.md rename to docs/csharp/fundamentals/program-structure/index.md diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md new file mode 100644 index 0000000000000..dfe0ffb137f28 --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -0,0 +1,246 @@ +--- +title: "Main() and command-line arguments - C# Programming Guide" +description: Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program. +ms.date: 03/08/2021 +f1_keywords: + - "main_CSharpKeyword" + - "Main" +helpviewer_keywords: + - "Main method [C#]" + - "C# language, command-line arguments" + - "arguments [C#], command-line" + - "command line [C#], arguments" + - "command-line arguments [C#], Main method" +ms.assetid: 73a17231-cf96-44ea-aa8a-54807c6fb1f4 +--- +# Main() and command-line arguments (C# Programming Guide) + +The `Main` method is the entry point of a C# application. (Libraries and services do not require a `Main` method as an entry point.) When the application is started, the `Main` method is the first method that is invoked. + +There can only be one entry point in a C# program. If you have more than one class that has a `Main` method, you must compile your program with the **StartupObject** compiler option to specify which `Main` method to use as the entry point. For more information, see [**StartupObject** (C# Compiler Options)](../../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject). + +[!code-csharp[csProgGuideMain#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#17)] + +Starting in C# 9, you can omit the `Main` method, and write C# statements as if they were in the `Main` method, as in the following example: + +:::code language="csharp" source="snippets/top-level-statements-1/Program.cs"::: + +For information about how to write application code with an implicit entry point method, see [Top-level statements](top-level-statements.md). + +## Overview + +- The `Main` method is the entry point of an executable program; it is where the program control starts and ends. +- `Main` is declared inside a class or struct. `Main` must be [static](../../language-reference/keywords/static.md) and it doesn't need to be [public](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [private](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. +- `Main` can either have a `void`, `int`, or, starting with C# 7.1, `Task`, or `Task` return type. +- If and only if `Main` returns a `Task` or `Task`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. Note that this specifically excludes an `async void Main` method. +- The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the method to obtain the [command-line arguments](command-line-arguments.md). Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the method. + +The following is a list of valid `Main` signatures: + +```csharp +public static void Main() { } +public static int Main() { } +public static void Main(string[] args) { } +public static int Main(string[] args) { } +public static async Task Main() { } +public static async Task Main() { } +public static async Task Main(string[] args) { } +public static async Task Main(string[] args) { } +``` + +The preceding examples all use the public accessor modifier. That is typical, but not required. + +The addition of `async` and `Task`, `Task` return types simplifies program code when console applications need to start and `await` asynchronous operations in `Main`. + +## Main() return values (C# Programming Guide) + +You can return an `int` from the `Main` method by defining the method in one of the following ways: + +| `Main` method code | `Main` signature | +|--------------------------------|----------------------------------------------| +| No use of `args` or `await` | `static int Main()` | +| Uses `args`, no use of `await` | `static int Main(string[] args)` | +| No use of `args`, uses `await` | `static async Task Main()` | +| Uses `args` and `await` | `static async Task Main(string[] args)` | + +If the return value from `Main` is not used, returning `void` or `Task` allows for slightly simpler code. + +| `Main` method code | `Main` signature | +|--------------------------------|-----------------------------------------| +| No use of `args` or `await` | `static void Main()` | +| Uses `args`, no use of `await` | `static void Main(string[] args)` | +| No use of `args`, uses `await` | `static async Task Main()` | +| Uses `args` and `await` | `static async Task Main(string[] args)` | + +However, returning `int` or `Task` enables the program to communicate status information to other programs or scripts that invoke the executable file. + +The following example shows how the exit code for the process can be accessed. + +## Example + +This example uses [.NET Core](../../../core/introduction.md) command-line tools. If you are unfamiliar with .NET Core command-line tools, you can learn about them in this [get-started article](../../../core/tutorials/with-visual-studio-code.md). + +Modify the `Main` method in *program.cs* as follows: + + [!code-csharp[csProgGuideMain#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs#14)] + +When a program is executed in Windows, any value returned from the `Main` function is stored in an environment variable. This environment variable can be retrieved using `ERRORLEVEL` from a batch file, or `$LastExitCode` from PowerShell. + +You can build the application using the [dotnet CLI](../../../core/tools/dotnet.md) `dotnet build` command. + +Next, create a PowerShell script to run the application and display the result. Paste the following code into a text file and save it as `test.ps1` in the folder that contains the project. Run the PowerShell script by typing `test.ps1` at the PowerShell prompt. + +Because the code returns zero, the batch file will report success. However, if you change MainReturnValTest.cs to return a non-zero value and then recompile the program, subsequent execution of the PowerShell script will report failure. + +```dotnetcli +dotnet run +``` + +```powershell +if ($LastExitCode -eq 0) { + Write-Host "Execution succeeded" +} else +{ + Write-Host "Execution Failed" +} +Write-Host "Return value = " $LastExitCode +``` + +## Sample output + +```txt +Execution succeeded +Return value = 0 +``` + +## Async Main return values + +Async Main return values move the boilerplate code necessary for calling asynchronous methods in `Main` to code generated by the compiler. Previously, you would need to write this construct to call asynchronous code and ensure your program ran until the asynchronous operation completed: + +```csharp +public static void Main() +{ + AsyncConsoleWork().GetAwaiter().GetResult(); +} + +private static async Task AsyncConsoleWork() +{ + // Main body here + return 0; +} +``` + +Now, this can be replaced by: + +[!code-csharp[AsyncMain](../../../../samples/snippets/csharp/main-arguments/program.cs#AsyncMain)] + +The advantage of the new syntax is that the compiler always generates the correct code. + +## Compiler-generated code + +When the application entry point returns a `Task` or `Task`, the compiler generates a new entry point that calls the entry point method declared in the application code. Assuming that this entry point is called `$GeneratedMain`, the compiler generates the following code for these entry points: + +- `static Task Main()` results in the compiler emitting the equivalent of `private static void $GeneratedMain() => Main().GetAwaiter().GetResult();` +- `static Task Main(string[])` results in the compiler emitting the equivalent of `private static void $GeneratedMain(string[] args) => Main(args).GetAwaiter().GetResult();` +- `static Task Main()` results in the compiler emitting the equivalent of `private static int $GeneratedMain() => Main().GetAwaiter().GetResult();` +- `static Task Main(string[])` results in the compiler emitting the equivalent of `private static int $GeneratedMain(string[] args) => Main(args).GetAwaiter().GetResult();` + +> [!NOTE] +>If the examples used `async` modifier on the `Main` method, the compiler would generate the same code. + +## Command-Line Arguments (C# Programming Guide) + +You can send arguments to the `Main` method by defining the method in one of the following ways: + +| `Main` method code | `Main` signature | +|------------------------------------|----------------------------------------------| +| No return value, no use of `await` | `static void Main(string[] args)` | +| Return value, no use of `await` | `static int Main(string[] args)` | +| No return value, uses `await` | `static async Task Main(string[] args)` | +| Return value, uses `await` | `static async Task Main(string[] args)` | + +If the arguments are not used, you can omit `args` from the method signature for slightly simpler code: + +| `Main` method code | `Main` signature | +|------------------------------------|---------------------------------| +| No return value, no use of `await` | `static void Main()` | +| Return value, no use of `await` | `static int Main()` | +| No return value, uses `await` | `static async Task Main()` | +| Return value, uses `await` | `static async Task Main()` | + +> [!NOTE] +> To enable command-line arguments in the `Main` method in a Windows Forms application, you must manually modify the signature of `Main` in *program.cs*. The code generated by the Windows Forms designer creates a `Main` without an input parameter. You can also use or to access the command-line arguments from any point in a console or Windows application. + +The parameter of the `Main` method is a array that represents the command-line arguments. Usually you determine whether arguments exist by testing the `Length` property, for example: + +[!code-csharp[csProgGuideMain#4](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs#4)] + +> [!TIP] +> The `args` array cannot be null. So, it's safe to access the `Length` property without null checking. + +You can also convert the string arguments to numeric types by using the class or the `Parse` method. For example, the following statement converts the `string` to a `long` number by using the method: + +```csharp +long num = Int64.Parse(args[0]); +``` + +It is also possible to use the C# type `long`, which aliases `Int64`: + +```csharp +long num = long.Parse(args[0]); +``` + +You can also use the `Convert` class method `ToInt64` to do the same thing: + +```csharp +long num = Convert.ToInt64(s); +``` + +For more information, see and . + +## Example + +The following example shows how to use command-line arguments in a console application. The application takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number. If no arguments are supplied, the application issues a message that explains the correct usage of the program. + +To compile and run the application from a command prompt, follow these steps: + +1. Paste the following code into any text editor, and then save the file as a text file with the name *Factorial.cs*. + + [!code-csharp[csProgGuideMain#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#16)] + +2. From the **Start** screen or **Start** menu, open a Visual Studio **Developer Command Prompt** window, and then navigate to the folder that contains the file that you just created. + +3. Enter the following command to compile the application. + + `csc Factorial.cs` + + If your application has no compilation errors, an executable file that's named *Factorial.exe* is created. + +4. Enter the following command to calculate the factorial of 3: + + `Factorial 3` + +5. The command produces this output: `The factorial of 3 is 6.` + +> [!NOTE] +> When running an application in Visual Studio, you can specify command-line arguments in the [Debug Page, Project Designer](/visualstudio/ide/reference/debug-page-project-designer). + +## C# language specification + +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] + +## See also + +- [C# Programming Guide](../index.md) +- [Methods](../classes-and-structs/methods.md) +- [Inside a C# Program](../inside-a-program/index.md) +- +- [C# Programming Guide](../index.md) +- [Main() and Command-Line Arguments](index.md) +- [How to display command line arguments](how-to-display-command-line-arguments.md) +- [Main() Return Values](main-return-values.md) +- [Classes](../classes-and-structs/classes.md) +- [C# Programming Guide](../index.md) +- [C# Reference](../../language-reference/index.md) +- [Main() and Command-Line Arguments](index.md) +- [How to display command line arguments](./how-to-display-command-line-arguments.md) diff --git a/docs/csharp/programming-guide/main-and-command-args/top-level-statements.md b/docs/csharp/fundamentals/program-structure/top-level-statements.md similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/top-level-statements.md rename to docs/csharp/fundamentals/program-structure/top-level-statements.md diff --git a/docs/csharp/programming-guide/main-and-command-args/how-to-display-command-line-arguments.md b/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/how-to-display-command-line-arguments.md rename to docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md diff --git a/docs/csharp/programming-guide/inside-a-program/index.md b/docs/csharp/programming-guide/inside-a-program/index.md deleted file mode 100644 index e9bf3cac873ee..0000000000000 --- a/docs/csharp/programming-guide/inside-a-program/index.md +++ /dev/null @@ -1,38 +0,0 @@ ---- -title: "Inside a C# program" -description: Use these resources to understand the general structure of a C# program, including the standard example, Hello, World! -ms.date: 07/20/2015 -helpviewer_keywords: - - "C# language, about C# program" - - "Visual C#, program structure" -ms.assetid: 9551354b-33f0-4e11-bbf0-1a35e3702b22 ---- -# Inside a C# program - -The section discusses the general structure of a C# program, and includes the standard "Hello, World!" example. - -## In this section - -- [General Structure of a C# Program](general-structure-of-a-csharp-program.md) - -- [Identifier names](identifier-names.md) - -- [C# Coding Conventions](coding-conventions.md) - -## Related sections - -- [Get Started with C#](../../tour-of-csharp/index.md) - -- [C# Programming Guide](../index.md) - -- [C# Reference](../../language-reference/index.md) - -- [Samples and tutorials](../../../samples-and-tutorials/index.md) - -## C# language specification - -[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See also - -- [C# Programming Guide](../index.md) diff --git a/docs/csharp/programming-guide/main-and-command-args/command-line-arguments.md b/docs/csharp/programming-guide/main-and-command-args/command-line-arguments.md deleted file mode 100644 index 23f870251a775..0000000000000 --- a/docs/csharp/programming-guide/main-and-command-args/command-line-arguments.md +++ /dev/null @@ -1,93 +0,0 @@ ---- -title: "Command-Line Arguments - C# Programming Guide" -description: Learn about command-line arguments. See an example that uses command-line arguments in a console application. -ms.date: 03/11/2021 -helpviewer_keywords: - - "command-line arguments [C#]" -ms.assetid: 0e597e0d-ea7a-41ba-a38a-0198122f3c26 ---- -# Command-Line Arguments (C# Programming Guide) - -You can send arguments to the `Main` method by defining the method in one of the following ways: - -| `Main` method code | `Main` signature | -|------------------------------------|----------------------------------------------| -| No return value, no use of `await` | `static void Main(string[] args)` | -| Return value, no use of `await` | `static int Main(string[] args)` | -| No return value, uses `await` | `static async Task Main(string[] args)` | -| Return value, uses `await` | `static async Task Main(string[] args)` | - -If the arguments are not used, you can omit `args` from the method signature for slightly simpler code: - -| `Main` method code | `Main` signature | -|------------------------------------|---------------------------------| -| No return value, no use of `await` | `static void Main()` | -| Return value, no use of `await` | `static int Main()` | -| No return value, uses `await` | `static async Task Main()` | -| Return value, uses `await` | `static async Task Main()` | - -> [!NOTE] -> To enable command-line arguments in the `Main` method in a Windows Forms application, you must manually modify the signature of `Main` in *program.cs*. The code generated by the Windows Forms designer creates a `Main` without an input parameter. You can also use or to access the command-line arguments from any point in a console or Windows application. - -The parameter of the `Main` method is a array that represents the command-line arguments. Usually you determine whether arguments exist by testing the `Length` property, for example: - -[!code-csharp[csProgGuideMain#4](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs#4)] - -> [!TIP] -> The `args` array cannot be null. So, it's safe to access the `Length` property without null checking. - -You can also convert the string arguments to numeric types by using the class or the `Parse` method. For example, the following statement converts the `string` to a `long` number by using the method: - -```csharp -long num = Int64.Parse(args[0]); -``` - -It is also possible to use the C# type `long`, which aliases `Int64`: - -```csharp -long num = long.Parse(args[0]); -``` - -You can also use the `Convert` class method `ToInt64` to do the same thing: - -```csharp -long num = Convert.ToInt64(s); -``` - -For more information, see and . - -## Example - -The following example shows how to use command-line arguments in a console application. The application takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number. If no arguments are supplied, the application issues a message that explains the correct usage of the program. - -To compile and run the application from a command prompt, follow these steps: - -1. Paste the following code into any text editor, and then save the file as a text file with the name *Factorial.cs*. - - [!code-csharp[csProgGuideMain#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#16)] - -2. From the **Start** screen or **Start** menu, open a Visual Studio **Developer Command Prompt** window, and then navigate to the folder that contains the file that you just created. - -3. Enter the following command to compile the application. - - `csc Factorial.cs` - - If your application has no compilation errors, an executable file that's named *Factorial.exe* is created. - -4. Enter the following command to calculate the factorial of 3: - - `Factorial 3` - -5. The command produces this output: `The factorial of 3 is 6.` - -> [!NOTE] -> When running an application in Visual Studio, you can specify command-line arguments in the [Debug Page, Project Designer](/visualstudio/ide/reference/debug-page-project-designer). - -## See also - -- -- [C# Programming Guide](../index.md) -- [Main() and Command-Line Arguments](index.md) -- [How to display command line arguments](how-to-display-command-line-arguments.md) -- [Main() Return Values](main-return-values.md) -- [Classes](../classes-and-structs/classes.md) diff --git a/docs/csharp/programming-guide/main-and-command-args/index.md b/docs/csharp/programming-guide/main-and-command-args/index.md deleted file mode 100644 index c86e888a6a571..0000000000000 --- a/docs/csharp/programming-guide/main-and-command-args/index.md +++ /dev/null @@ -1,63 +0,0 @@ ---- -title: "Main() and command-line arguments - C# Programming Guide" -description: Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program. -ms.date: 03/08/2021 -f1_keywords: - - "main_CSharpKeyword" - - "Main" -helpviewer_keywords: - - "Main method [C#]" - - "C# language, command-line arguments" - - "arguments [C#], command-line" - - "command line [C#], arguments" - - "command-line arguments [C#], Main method" -ms.assetid: 73a17231-cf96-44ea-aa8a-54807c6fb1f4 ---- -# Main() and command-line arguments (C# Programming Guide) - -The `Main` method is the entry point of a C# application. (Libraries and services do not require a `Main` method as an entry point.) When the application is started, the `Main` method is the first method that is invoked. - -There can only be one entry point in a C# program. If you have more than one class that has a `Main` method, you must compile your program with the **StartupObject** compiler option to specify which `Main` method to use as the entry point. For more information, see [**StartupObject** (C# Compiler Options)](../../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject). - -[!code-csharp[csProgGuideMain#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#17)] - -Starting in C# 9, you can omit the `Main` method, and write C# statements as if they were in the `Main` method, as in the following example: - -:::code language="csharp" source="snippets/top-level-statements-1/Program.cs"::: - -For information about how to write application code with an implicit entry point method, see [Top-level statements](top-level-statements.md). - -## Overview - -- The `Main` method is the entry point of an executable program; it is where the program control starts and ends. -- `Main` is declared inside a class or struct. `Main` must be [static](../../language-reference/keywords/static.md) and it doesn't need to be [public](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [private](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. -- `Main` can either have a `void`, `int`, or, starting with C# 7.1, `Task`, or `Task` return type. -- If and only if `Main` returns a `Task` or `Task`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. Note that this specifically excludes an `async void Main` method. -- The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the method to obtain the [command-line arguments](command-line-arguments.md). Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the method. - -The following is a list of valid `Main` signatures: - -```csharp -public static void Main() { } -public static int Main() { } -public static void Main(string[] args) { } -public static int Main(string[] args) { } -public static async Task Main() { } -public static async Task Main() { } -public static async Task Main(string[] args) { } -public static async Task Main(string[] args) { } -``` - -The preceding examples all use the public accessor modifier. That is typical, but not required. - -The addition of `async` and `Task`, `Task` return types simplifies program code when console applications need to start and `await` asynchronous operations in `Main`. - -## C# language specification - -[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See also - -- [C# Programming Guide](../index.md) -- [Methods](../classes-and-structs/methods.md) -- [Inside a C# Program](../inside-a-program/index.md) diff --git a/docs/csharp/programming-guide/main-and-command-args/main-return-values.md b/docs/csharp/programming-guide/main-and-command-args/main-return-values.md deleted file mode 100644 index fdacfaab2a168..0000000000000 --- a/docs/csharp/programming-guide/main-and-command-args/main-return-values.md +++ /dev/null @@ -1,111 +0,0 @@ ---- -title: "Main() Return Values - C# Programming Guide" -description: Learn about Main() return values. See code examples, compiler-generated code, and view additional available resources. -ms.date: 03/11/2021 -helpviewer_keywords: - - "Main method [C#], return values" -ms.assetid: c2f5a1d8-1676-4bea-bc7e-44a97e72d5bc ---- - -# Main() return values (C# Programming Guide) - -You can return an `int` from the `Main` method by defining the method in one of the following ways: - -| `Main` method code | `Main` signature | -|--------------------------------|----------------------------------------------| -| No use of `args` or `await` | `static int Main()` | -| Uses `args`, no use of `await` | `static int Main(string[] args)` | -| No use of `args`, uses `await` | `static async Task Main()` | -| Uses `args` and `await` | `static async Task Main(string[] args)` | - -If the return value from `Main` is not used, returning `void` or `Task` allows for slightly simpler code. - -| `Main` method code | `Main` signature | -|--------------------------------|-----------------------------------------| -| No use of `args` or `await` | `static void Main()` | -| Uses `args`, no use of `await` | `static void Main(string[] args)` | -| No use of `args`, uses `await` | `static async Task Main()` | -| Uses `args` and `await` | `static async Task Main(string[] args)` | - -However, returning `int` or `Task` enables the program to communicate status information to other programs or scripts that invoke the executable file. - -The following example shows how the exit code for the process can be accessed. - -## Example - -This example uses [.NET Core](../../../core/introduction.md) command-line tools. If you are unfamiliar with .NET Core command-line tools, you can learn about them in this [get-started article](../../../core/tutorials/with-visual-studio-code.md). - -Modify the `Main` method in *program.cs* as follows: - - [!code-csharp[csProgGuideMain#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs#14)] - -When a program is executed in Windows, any value returned from the `Main` function is stored in an environment variable. This environment variable can be retrieved using `ERRORLEVEL` from a batch file, or `$LastExitCode` from PowerShell. - -You can build the application using the [dotnet CLI](../../../core/tools/dotnet.md) `dotnet build` command. - -Next, create a PowerShell script to run the application and display the result. Paste the following code into a text file and save it as `test.ps1` in the folder that contains the project. Run the PowerShell script by typing `test.ps1` at the PowerShell prompt. - -Because the code returns zero, the batch file will report success. However, if you change MainReturnValTest.cs to return a non-zero value and then recompile the program, subsequent execution of the PowerShell script will report failure. - -```dotnetcli -dotnet run -``` - -```powershell -if ($LastExitCode -eq 0) { - Write-Host "Execution succeeded" -} else -{ - Write-Host "Execution Failed" -} -Write-Host "Return value = " $LastExitCode -``` - -## Sample output - -```txt -Execution succeeded -Return value = 0 -``` - -## Async Main return values - -Async Main return values move the boilerplate code necessary for calling asynchronous methods in `Main` to code generated by the compiler. Previously, you would need to write this construct to call asynchronous code and ensure your program ran until the asynchronous operation completed: - -```csharp -public static void Main() -{ - AsyncConsoleWork().GetAwaiter().GetResult(); -} - -private static async Task AsyncConsoleWork() -{ - // Main body here - return 0; -} -``` - -Now, this can be replaced by: - -[!code-csharp[AsyncMain](../../../../samples/snippets/csharp/main-arguments/program.cs#AsyncMain)] - -The advantage of the new syntax is that the compiler always generates the correct code. - -## Compiler-generated code - -When the application entry point returns a `Task` or `Task`, the compiler generates a new entry point that calls the entry point method declared in the application code. Assuming that this entry point is called `$GeneratedMain`, the compiler generates the following code for these entry points: - -- `static Task Main()` results in the compiler emitting the equivalent of `private static void $GeneratedMain() => Main().GetAwaiter().GetResult();` -- `static Task Main(string[])` results in the compiler emitting the equivalent of `private static void $GeneratedMain(string[] args) => Main(args).GetAwaiter().GetResult();` -- `static Task Main()` results in the compiler emitting the equivalent of `private static int $GeneratedMain() => Main().GetAwaiter().GetResult();` -- `static Task Main(string[])` results in the compiler emitting the equivalent of `private static int $GeneratedMain(string[] args) => Main(args).GetAwaiter().GetResult();` - -> [!NOTE] ->If the examples used `async` modifier on the `Main` method, the compiler would generate the same code. - -## See also - -- [C# Programming Guide](../index.md) -- [C# Reference](../../language-reference/index.md) -- [Main() and Command-Line Arguments](index.md) -- [How to display command line arguments](./how-to-display-command-line-arguments.md) diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index f163ad3761760..462f1b938ab65 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -59,30 +59,22 @@ items: href: fundamentals/types/anonymous-types.md - name: Tutorials items: - - name: Cast using is and as operators - href: how-to/safely-cast-using-pattern-matching-is-and-as-operators.md - # DTOs (tuples, anonymous types and records) - # Values (record structs and structs) - # Classes and objects (interfaces and classes) - # Generics (methods and classes) + # TODO: DTOs (tuples, anonymous types and records) + # TODO: Values (record structs and structs) + # TODO: Classes and objects (interfaces and classes) + # TODO: Generics (methods and classes) - name: Program structure items: - name: Overview - href: programming-guide/inside-a-program/general-structure-of-a-csharp-program.md + href: fundamentals/program-structure/index.md - name: Top-level statements - href: programming-guide/main-and-command-args/top-level-statements.md + href: fundamentals/program-structure/top-level-statements.md - name: Main method - href: programming-guide/main-and-command-args/index.md - - name: Main() Return Values - href: programming-guide/main-and-command-args/main-return-values.md - - name: Command-Line Arguments - href: programming-guide/main-and-command-args/command-line-arguments.md - - name: What's inside a C# program - href: programming-guide/inside-a-program/index.md + href: fundamentals/program-structure/main-command-line.md - name: Tutorials items: - name: "How to display command-line arguments" - href: programming-guide/main-and-command-args/how-to-display-command-line-arguments.md + href: fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md # Command line processing tutorials - name: Object Oriented programming items: @@ -109,6 +101,9 @@ items: - name: Inheritance in C# and .NET href: tutorials/inheritance.md # programming to interfaces + - name: Converting types + displayName: cast, is, as + href: how-to/safely-cast-using-pattern-matching-is-and-as-operators.md - name: Functional techniques items: # - Conceptual overview From f0105d1e552362b6a0a3a7d59082497423d0c8d6 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 7 May 2021 14:47:26 -0400 Subject: [PATCH 10/60] clean warnings in structure section --- .../fundamentals/program-structure/index.md | 35 ++++++++----------- .../program-structure/main-command-line.md | 26 +++----------- .../snippets/structure/Program.cs | 5 ++- .../snippets/structure/structure.csproj} | 0 .../top-level-statements-1/Program.cs | 0 .../top-level-statements.csproj | 0 .../top-level-statements-2/Program.cs | 0 .../top-level-statements.csproj | 0 .../top-level-statements-3/Program.cs | 0 .../top-level-statements.csproj | 0 .../top-level-statements-4/Program.cs | 0 .../top-level-statements.csproj | 0 .../top-level-statements-5/Program.cs | 0 .../top-level-statements.csproj | 8 +++++ .../snippets/toplevel-structure/Program.cs | 33 +++++++++++++++++ .../toplevel-structure.csproj | 9 +++++ .../program-structure/top-level-statements.md | 6 ---- .../how-to-display-command-line-arguments.md | 8 +---- 18 files changed, 71 insertions(+), 59 deletions(-) rename samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/class2.cs => docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs (84%) rename docs/csharp/{programming-guide/main-and-command-args/snippets/top-level-statements-1/top-level-statements.csproj => fundamentals/program-structure/snippets/structure/structure.csproj} (100%) rename docs/csharp/{programming-guide/main-and-command-args => fundamentals/program-structure}/snippets/top-level-statements-1/Program.cs (100%) rename docs/csharp/{programming-guide/main-and-command-args/snippets/top-level-statements-2 => fundamentals/program-structure/snippets/top-level-statements-1}/top-level-statements.csproj (100%) rename docs/csharp/{programming-guide/main-and-command-args => fundamentals/program-structure}/snippets/top-level-statements-2/Program.cs (100%) rename docs/csharp/{programming-guide/main-and-command-args/snippets/top-level-statements-3 => fundamentals/program-structure/snippets/top-level-statements-2}/top-level-statements.csproj (100%) rename docs/csharp/{programming-guide/main-and-command-args => fundamentals/program-structure}/snippets/top-level-statements-3/Program.cs (100%) rename docs/csharp/{programming-guide/main-and-command-args/snippets/top-level-statements-4 => fundamentals/program-structure/snippets/top-level-statements-3}/top-level-statements.csproj (100%) rename docs/csharp/{programming-guide/main-and-command-args => fundamentals/program-structure}/snippets/top-level-statements-4/Program.cs (100%) rename docs/csharp/{programming-guide/main-and-command-args/snippets/top-level-statements-5 => fundamentals/program-structure/snippets/top-level-statements-4}/top-level-statements.csproj (100%) rename docs/csharp/{programming-guide/main-and-command-args => fundamentals/program-structure}/snippets/top-level-statements-5/Program.cs (100%) create mode 100644 docs/csharp/fundamentals/program-structure/snippets/top-level-statements-5/top-level-statements.csproj create mode 100644 docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/Program.cs create mode 100644 docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/toplevel-structure.csproj diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index 2ca7734ac25ae..c7d867e99df4f 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -8,30 +8,23 @@ ms.assetid: 5ae964a5-0ef0-40fe-88fb-6d1793371d0d --- # General Structure of a C# Program (C# Programming Guide) -C# programs can consist of one or more files. Each file can contain zero or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces. The following is the skeleton of a C# program that contains all of these elements. - - [!code-csharp[csProgGuide#34](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/class2.cs#34)] - -## Related Sections +C# programs can consist of one or more files. Each file can contain zero or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces. The following is the skeleton of a C# program that contains all of these elements. - For more information: - -- [Classes](../classes-and-structs/classes.md) - +:::code language="{language}" source="{source}" range="{range}"::: + +The preceding example uses *top level statements* for the program's entry point. This feature was added in C# 9. Prior to C# 9, the entry point was a static method named `Main`, as shown in the following example: + +:::code language="csharp" source="snippets/structure/Program.cs"::: + +## Related Sections + +You learned about these program elements in the [types](../types/index.md) section of the fundamentals guide: + +- [Classes](../types/classes.md) - [Structs](../../language-reference/builtin-types/struct.md) - -- [Namespaces](../namespaces/index.md) - -- [Interfaces](../interfaces/index.md) - -- [Delegates](../delegates/index.md) +- [Namespaces](../types/namespaces.md) +- [Interfaces](../types/interfaces.md) ## C# Language Specification For more information, see [Basic concepts](~/_csharplang/spec/basic-concepts.md) in the [C# Language Specification](/dotnet/csharp/language-reference/language-specification/introduction). The language specification is the definitive source for C# syntax and usage. - -## See also - -- [C# Programming Guide](../index.md) -- [Inside a C# Program](./index.md) -- [C# Reference](../../language-reference/index.md) diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index dfe0ffb137f28..8746fd6a34b19 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -52,7 +52,7 @@ The preceding examples all use the public accessor modifier. That is typical, bu The addition of `async` and `Task`, `Task` return types simplifies program code when console applications need to start and `await` asynchronous operations in `Main`. -## Main() return values (C# Programming Guide) +## Main() return values You can return an `int` from the `Main` method by defining the method in one of the following ways: @@ -76,8 +76,6 @@ However, returning `int` or `Task` enables the program to communicate statu The following example shows how the exit code for the process can be accessed. -## Example - This example uses [.NET Core](../../../core/introduction.md) command-line tools. If you are unfamiliar with .NET Core command-line tools, you can learn about them in this [get-started article](../../../core/tutorials/with-visual-studio-code.md). Modify the `Main` method in *program.cs* as follows: @@ -106,14 +104,13 @@ if ($LastExitCode -eq 0) { Write-Host "Return value = " $LastExitCode ``` -## Sample output ```txt Execution succeeded Return value = 0 ``` -## Async Main return values +### Async Main return values Async Main return values move the boilerplate code necessary for calling asynchronous methods in `Main` to code generated by the compiler. Previously, you would need to write this construct to call asynchronous code and ensure your program ran until the asynchronous operation completed: @@ -136,8 +133,6 @@ Now, this can be replaced by: The advantage of the new syntax is that the compiler always generates the correct code. -## Compiler-generated code - When the application entry point returns a `Task` or `Task`, the compiler generates a new entry point that calls the entry point method declared in the application code. Assuming that this entry point is called `$GeneratedMain`, the compiler generates the following code for these entry points: - `static Task Main()` results in the compiler emitting the equivalent of `private static void $GeneratedMain() => Main().GetAwaiter().GetResult();` @@ -148,7 +143,7 @@ When the application entry point returns a `Task` or `Task`, the compiler g > [!NOTE] >If the examples used `async` modifier on the `Main` method, the compiler would generate the same code. -## Command-Line Arguments (C# Programming Guide) +## Command-Line Arguments You can send arguments to the `Main` method by defining the method in one of the following ways: @@ -198,8 +193,6 @@ long num = Convert.ToInt64(s); For more information, see and . -## Example - The following example shows how to use command-line arguments in a console application. The application takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number. If no arguments are supplied, the application issues a message that explains the correct usage of the program. To compile and run the application from a command prompt, follow these steps: @@ -231,16 +224,5 @@ To compile and run the application from a command prompt, follow these steps: ## See also -- [C# Programming Guide](../index.md) -- [Methods](../classes-and-structs/methods.md) -- [Inside a C# Program](../inside-a-program/index.md) - -- [C# Programming Guide](../index.md) -- [Main() and Command-Line Arguments](index.md) -- [How to display command line arguments](how-to-display-command-line-arguments.md) -- [Main() Return Values](main-return-values.md) -- [Classes](../classes-and-structs/classes.md) -- [C# Programming Guide](../index.md) -- [C# Reference](../../language-reference/index.md) -- [Main() and Command-Line Arguments](index.md) -- [How to display command line arguments](./how-to-display-command-line-arguments.md) +- [How to display command line arguments](tutorials/how-to-display-command-line-arguments.md) diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/class2.cs b/docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs similarity index 84% rename from samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/class2.cs rename to docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs index 3845a11e90ed8..3797a45f35c38 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/class2.cs +++ b/docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs @@ -1,5 +1,4 @@ -// -// A skeleton of a C# program +// A skeleton of a C# program using System; namespace YourNamespace { @@ -33,7 +32,7 @@ class YourMainClass static void Main(string[] args) { //Your program starts here... + Console.WriteLine("Hello world!"); } } } -// \ No newline at end of file diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-1/top-level-statements.csproj b/docs/csharp/fundamentals/program-structure/snippets/structure/structure.csproj similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-1/top-level-statements.csproj rename to docs/csharp/fundamentals/program-structure/snippets/structure/structure.csproj diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-1/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-1/Program.cs similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-1/Program.cs rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-1/Program.cs diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-2/top-level-statements.csproj b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-1/top-level-statements.csproj similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-2/top-level-statements.csproj rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-1/top-level-statements.csproj diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-2/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-2/Program.cs similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-2/Program.cs rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-2/Program.cs diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-3/top-level-statements.csproj b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-2/top-level-statements.csproj similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-3/top-level-statements.csproj rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-2/top-level-statements.csproj diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-3/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-3/Program.cs similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-3/Program.cs rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-3/Program.cs diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-4/top-level-statements.csproj b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-3/top-level-statements.csproj similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-4/top-level-statements.csproj rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-3/top-level-statements.csproj diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-4/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-4/Program.cs similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-4/Program.cs rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-4/Program.cs diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-5/top-level-statements.csproj b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-4/top-level-statements.csproj similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-5/top-level-statements.csproj rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-4/top-level-statements.csproj diff --git a/docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-5/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-5/Program.cs similarity index 100% rename from docs/csharp/programming-guide/main-and-command-args/snippets/top-level-statements-5/Program.cs rename to docs/csharp/fundamentals/program-structure/snippets/top-level-statements-5/Program.cs diff --git a/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-5/top-level-statements.csproj b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-5/top-level-statements.csproj new file mode 100644 index 0000000000000..2082704286ba4 --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/top-level-statements-5/top-level-statements.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/Program.cs new file mode 100644 index 0000000000000..e38e306b1e87d --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/Program.cs @@ -0,0 +1,33 @@ +// A skeleton of a C# program +using System; + +// Your program starts here: +Console.WriteLine("Hello world!"); + +namespace YourNamespace +{ + class YourClass + { + } + + struct YourStruct + { + } + + interface IYourInterface + { + } + + delegate int YourDelegate(); + + enum YourEnum + { + } + + namespace YourNestedNamespace + { + struct YourStruct + { + } + } +} diff --git a/docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/toplevel-structure.csproj b/docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/toplevel-structure.csproj new file mode 100644 index 0000000000000..bc5296ed7f765 --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/toplevel-structure/toplevel-structure.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + toplevel_structure + + + diff --git a/docs/csharp/fundamentals/program-structure/top-level-statements.md b/docs/csharp/fundamentals/program-structure/top-level-statements.md index 0cfd60d9277df..29a4606ebcada 100644 --- a/docs/csharp/fundamentals/program-structure/top-level-statements.md +++ b/docs/csharp/fundamentals/program-structure/top-level-statements.md @@ -83,9 +83,3 @@ The compiler generates a method to serve as the program entry point for a projec [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] [Top-level statements](~/_csharplang/proposals/csharp-9.0/top-level-statements.md) - -## See also - -- [C# Programming Guide](../index.md) -- [Methods](../classes-and-structs/methods.md) -- [Inside a C# Program](../inside-a-program/index.md) diff --git a/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md b/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md index d258c50e05980..98a05d1cab941 100644 --- a/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md +++ b/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md @@ -9,7 +9,7 @@ ms.assetid: b8479f2d-9e05-4d38-82da-2e61246e5437 --- # How to display command-line arguments (C# Programming Guide) -Arguments provided to an executable on the command line are accessible in [top-level statements](top-level-statements.md) or through an optional parameter to `Main`. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable: +Arguments provided to an executable on the command line are accessible in [top-level statements](../top-level-statements.md) or through an optional parameter to `Main`. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable: |Input on command line|Array of strings passed to Main| |----------------------------|-------------------------------------| @@ -25,9 +25,3 @@ Arguments provided to an executable on the command line are accessible in [top-l This example displays the command-line arguments passed to a command-line application. The output shown is for the first entry in the table above. [!code-csharp[csProgGuideMain#9](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#9)] - -## See also - -- [C# Programming Guide](../index.md) -- [Main() and Command-Line Arguments](./index.md) -- [Main() Return Values](./main-return-values.md) From 7dad5626abb769f9a943c9abd1964a2fbd5dc6ca Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 7 May 2021 14:53:43 -0400 Subject: [PATCH 11/60] warnings take 2 --- docs/csharp/fundamentals/program-structure/index.md | 2 +- .../csharp/fundamentals/program-structure/main-command-line.md | 3 +-- docs/csharp/fundamentals/types/index.md | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index c7d867e99df4f..0fa31546ff5cf 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -10,7 +10,7 @@ ms.assetid: 5ae964a5-0ef0-40fe-88fb-6d1793371d0d C# programs can consist of one or more files. Each file can contain zero or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces. The following is the skeleton of a C# program that contains all of these elements. -:::code language="{language}" source="{source}" range="{range}"::: +:::code language="csharp" source="snippets/toplevel-structure/Program.cs"::: The preceding example uses *top level statements* for the program's entry point. This feature was added in C# 9. Prior to C# 9, the entry point was a static method named `Main`, as shown in the following example: diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index 8746fd6a34b19..e55d94bd5675f 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -33,7 +33,7 @@ For information about how to write application code with an implicit entry point - `Main` is declared inside a class or struct. `Main` must be [static](../../language-reference/keywords/static.md) and it doesn't need to be [public](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [private](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. - `Main` can either have a `void`, `int`, or, starting with C# 7.1, `Task`, or `Task` return type. - If and only if `Main` returns a `Task` or `Task`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. Note that this specifically excludes an `async void Main` method. -- The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the method to obtain the [command-line arguments](command-line-arguments.md). Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the method. +- The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the method to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the method. The following is a list of valid `Main` signatures: @@ -104,7 +104,6 @@ if ($LastExitCode -eq 0) { Write-Host "Return value = " $LastExitCode ``` - ```txt Execution succeeded Return value = 0 diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 259eb915c311c..d34186a594372 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -120,7 +120,7 @@ All arrays are reference types, even if their elements are value types. Arrays i :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="ArrayDeclaration"::: -Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](../classes-and-structs/index.md). For more information about inheritance and virtual methods, see [Inheritance](../classes-and-structs/inheritance.md). +Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md). For more information about inheritance and virtual methods, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). ## Types of literal values From a229ba2a59a957bfddc8d667da4c68bd886e40fa Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 7 May 2021 16:16:17 -0400 Subject: [PATCH 12/60] move oo section files. --- .openpublishing.redirection.json | 33 ++++++++ .../object-oriented}/index.md | 54 ++++++------- .../object-oriented}/inheritance.md | 16 ++-- .../inheritance/class-inheritance-diagram.png | Bin .../object-oriented}/objects.md | 22 +++--- .../object-oriented}/polymorphism.md | 19 +---- .../object-oriented/tutorials/classes.md} | 18 ++--- .../object-oriented}/tutorials/inheritance.md | 74 +++++++++--------- .../media/inheritance}/book-class.jpg | Bin .../media/inheritance}/publication-class.jpg | Bin .../object-oriented/tutorials/oop.md} | 4 +- ...ng-pattern-matching-is-and-as-operators.md | 12 +-- .../snippets}/inheritance/base-and-derived.cs | 0 .../tutorials/snippets}/inheritance/basics.cs | 0 .../tutorials/snippets}/inheritance/is-a.cs | 0 .../snippets}/inheritance/private.cs | 0 .../tutorials/snippets}/inheritance/shape.cs | 0 .../snippets}/inheritance/simpleclass.cs | 0 .../snippets}/inheritance/simpleclass2.cs | 0 .../snippets}/inheritance/use-publication.cs | 0 .../introduction-to-classes/BankAccount.cs | 0 .../introduction-to-classes/Program.cs | 0 .../introduction-to-classes/classes.csproj | 0 .../introduction-to-classes/transaction.cs | 0 .../BankAccount.cs | 0 .../GiftCardAccount.cs | 0 .../InterestEarningAccount.cs | 0 .../LineOfCreditAccount.cs | 0 .../object-oriented-programming/Program.cs | 0 .../object-oriented-programming.csproj | 0 .../transaction.cs | 0 docs/csharp/toc.yml | 21 +++-- 32 files changed, 131 insertions(+), 142 deletions(-) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/object-oriented}/index.md (78%) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/object-oriented}/inheritance.md (89%) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/object-oriented}/media/inheritance/class-inheritance-diagram.png (100%) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/object-oriented}/objects.md (88%) rename docs/csharp/{programming-guide/classes-and-structs => fundamentals/object-oriented}/polymorphism.md (90%) rename docs/csharp/{tutorials/intro-to-csharp/introduction-to-classes.md => fundamentals/object-oriented/tutorials/classes.md} (90%) rename docs/csharp/{ => fundamentals/object-oriented}/tutorials/inheritance.md (74%) rename docs/csharp/{tutorials/media => fundamentals/object-oriented/tutorials/media/inheritance}/book-class.jpg (100%) rename docs/csharp/{tutorials/media => fundamentals/object-oriented/tutorials/media/inheritance}/publication-class.jpg (100%) rename docs/csharp/{tutorials/intro-to-csharp/object-oriented-programming.md => fundamentals/object-oriented/tutorials/oop.md} (96%) rename docs/csharp/{how-to => fundamentals/object-oriented/tutorials}/safely-cast-using-pattern-matching-is-and-as-operators.md (63%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/base-and-derived.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/basics.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/is-a.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/private.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/shape.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/simpleclass.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/simpleclass2.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/inheritance/use-publication.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/introduction-to-classes/BankAccount.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/introduction-to-classes/Program.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/introduction-to-classes/classes.csproj (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/introduction-to-classes/transaction.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/object-oriented-programming/BankAccount.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/object-oriented-programming/GiftCardAccount.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/object-oriented-programming/InterestEarningAccount.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/object-oriented-programming/LineOfCreditAccount.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/object-oriented-programming/Program.cs (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/object-oriented-programming/object-oriented-programming.csproj (100%) rename docs/csharp/{tutorials/intro-to-csharp => fundamentals/object-oriented/tutorials}/snippets/object-oriented-programming/transaction.cs (100%) diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 79f23911d240e..38db9f687c3af 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -979,6 +979,11 @@ "source_path": "docs/csharp/getting-started/with-visual-studio.md", "redirect_url": "/dotnet/core/tutorials/with-visual-studio" }, + { + "source_path": "docs/csharp/how-to/safely-cast-using-pattern-matching-is-and-as-operators.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md" + }, + { "source_path": "docs/csharp/implicitly-typed-lambda-expressions.md", "redirect_url": "/dotnet/csharp/language-reference/operators/lambda-expressions" @@ -1804,6 +1809,22 @@ "source_path": "docs/csharp/programming-guide/classes-and-structs/how-to-access-a-collection-class-with-foreach.md", "redirect_url": "/dotnet/csharp/language-reference/keywords/foreach-in" }, + { + "source_path": "docs/csharp/programming-guide/classes-and-structs/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented" + }, + { + "source_path": "docs/csharp/programming-guide/classes-and-structs/inheritance.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/inheritance" + }, + { + "source_path": "docs/csharp/programming-guide/classes-and-structs/objects.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/objects" + }, + { + "source_path": "docs/csharp/programming-guide/classes-and-structs/polymorphism.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/polymorphism" + }, { "source_path": "docs/csharp/programming-guide/classes-and-structs/records.md", "redirect_url": "/dotnet/csharp/fundamentals/types/records" @@ -3308,6 +3329,10 @@ "source_path": "docs/csharp/tutorials/index.md", "redirect_url": "/dotnet/csharp/tutorials/intro-to-csharp/introduction-to-classes" }, + { + "source_path": "docs/csharp/tutorials/inheritance.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/inheritance" + }, { "source_path": "docs/csharp/tutorials/intro-to-csharp/interpolated-strings-local.md", "redirect_url": "/dotnet/csharp/tutorials/exploration/interpolated-strings-local" @@ -3316,6 +3341,14 @@ "source_path": "docs/csharp/tutorials/intro-to-csharp/interpolated-strings.yml", "redirect_url": "/dotnet/csharp/tutorials/exploration/interpolated-strings" }, + { + "source_path": "docs/csharp/tutorials/intro-to-csharp/introduction-to-classes.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/tutorials/classes" + }, + { + "source_path": "docs/csharp/tutorials/intro-to-csharp/object-oriented-programming.md", + "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/tutorials/oop" + }, { "source_path": "docs/csharp/tutorials/microservices.md", "redirect_url": "/dotnet/core/docker/" diff --git a/docs/csharp/programming-guide/classes-and-structs/index.md b/docs/csharp/fundamentals/object-oriented/index.md similarity index 78% rename from docs/csharp/programming-guide/classes-and-structs/index.md rename to docs/csharp/fundamentals/object-oriented/index.md index 6052bc0e4e1d0..519667a67a742 100644 --- a/docs/csharp/programming-guide/classes-and-structs/index.md +++ b/docs/csharp/fundamentals/object-oriented/index.md @@ -32,24 +32,24 @@ In general, classes are used to model more complex behavior, or data that is int ## Encapsulation - *Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that are not intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see [Object-oriented programming](../../tutorials/intro-to-csharp/object-oriented-programming.md). + *Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that are not intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see [Object-oriented programming](./tutorials/oop.md). ## Members -All methods, fields, constants, properties, and events must be declared within a type; these are called the *members* of the type. In C#, there are no global variables or methods as there are in some other languages. Even a program's entry point, the `Main` method, must be declared within a class or struct (implicitly in the case of [top-level statements](../main-and-command-args/top-level-statements.md)). +All methods, fields, constants, properties, and events must be declared within a type; these are called the *members* of the type. In C#, there are no global variables or methods as there are in some other languages. Even a program's entry point, the `Main` method, must be declared within a class or struct (implicitly in the case of [top-level statements](../program-structure/top-level-statements.md)). The following list includes all the various kinds of members that may be declared in a class, struct, or record. -- [Fields](./fields.md) -- [Constants](./constants.md) -- [Properties](./properties.md) -- [Methods](./methods.md) -- [Constructors](./constructors.md) -- [Events](../events/index.md) -- [Finalizers](./destructors.md) -- [Indexers](../indexers/index.md) -- [Operators](../../language-reference/operators/index.md) -- [Nested Types](./nested-types.md) +- Fields +- Constants +- Properties +- Methods +- Constructors +- Events +- Finalizers +- Indexers +- Operators +- Nested Types ## Accessibility @@ -62,49 +62,49 @@ The following list includes all the various kinds of members that may be declare - [private](../../language-reference/keywords/private.md) - [private protected](../../language-reference/keywords/private-protected.md). -The default accessibility is `private`. For more information, see [Access Modifiers](./access-modifiers.md). +The default accessibility is `private`. ## Inheritance Classes (but not structs) support the concept of inheritance. A class that derives from another class (the *base class*) automatically contains all the public, protected, and internal members of the base class except its constructors and finalizers. For more information, see [Inheritance](./inheritance.md) and [Polymorphism](./polymorphism.md). -Classes may be declared as [abstract](../../language-reference/keywords/abstract.md), which means that one or more of their methods have no implementation. Although abstract classes cannot be instantiated directly, they can serve as base classes for other classes that provide the missing implementation. Classes can also be declared as [sealed](../../language-reference/keywords/sealed.md) to prevent other classes from inheriting from them. For more information, see [Abstract and sealed classes and class members](./abstract-and-sealed-classes-and-class-members.md). +Classes may be declared as [abstract](../../language-reference/keywords/abstract.md), which means that one or more of their methods have no implementation. Although abstract classes cannot be instantiated directly, they can serve as base classes for other classes that provide the missing implementation. Classes can also be declared as [sealed](../../language-reference/keywords/sealed.md) to prevent other classes from inheriting from them. ## Interfaces -Classes, structs, and records can inherit multiple interfaces. To inherit from an interface means that the type implements all the methods defined in the interface. For more information, see [Interfaces](../interfaces/index.md). +Classes, structs, and records can inherit multiple interfaces. To inherit from an interface means that the type implements all the methods defined in the interface. For more information, see [Interfaces](../types/interfaces.md). ## Generic Types - Classes, structs, and records can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example The class in the namespace is defined with one type parameter. Client code creates an instance of a `List` or `List` to specify the type that the list will hold. For more information, see [Generics](../generics/index.md). + Classes, structs, and records can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example The class in the namespace is defined with one type parameter. Client code creates an instance of a `List` or `List` to specify the type that the list will hold. For more information, see [Generics](../types/generics.md). ## Static Types -Classes (but not structs or records) can be declared as [static](../../language-reference/keywords/static.md). A static class can contain only static members and can't be instantiated with the `new` keyword. One copy of the class is loaded into memory when the program loads, and its members are accessed through the class name. Classes, structs, and records can contain static members. For more information, see [Static classes and static class members](./static-classes-and-static-class-members.md). +Classes (but not structs or records) can be declared as `static`. A static class can contain only static members and can't be instantiated with the `new` keyword. One copy of the class is loaded into memory when the program loads, and its members are accessed through the class name. Classes, structs, and records can contain static members. ## Nested Types -A class, struct, or record can be nested within another class, struct, or record. For more information, see [Nested Types](./nested-types.md). +A class, struct, or record can be nested within another class, struct, or record. ## Partial Types -You can define part of a class, struct or method in one code file and another part in a separate code file. For more information, see [Partial Classes and methods](./partial-classes-and-methods.md). +You can define part of a class, struct or method in one code file and another part in a separate code file. ## Object Initializers -You can instantiate and initialize class or struct objects, and collections of objects, without explicitly calling their constructor. For more information, see [Object and collection initializers](./object-and-collection-initializers.md). +You can instantiate and initialize class or struct objects, and collections of objects, without explicitly calling their constructor. ## Anonymous Types -In situations where it is not convenient or necessary to create a named class, for example when you are populating a list with data structures that you do not have to persist or pass to another method, you use anonymous types. For more information, see [Anonymous types](./anonymous-types.md). +In situations where it is not convenient or necessary to create a named class, for example when you are populating a list with data structures that you do not have to persist or pass to another method, you use anonymous types. ## Extension Methods -You can "extend" a class without creating a derived class by creating a separate type whose methods can be called as if they belonged to the original type. For more information, see [Extension methods](./extension-methods.md). +You can "extend" a class without creating a derived class by creating a separate type whose methods can be called as if they belonged to the original type. ## Implicitly Typed Local Variables -Within a class or struct method, you can use implicit typing to instruct the compiler to determine a variable's type at compile time. For more information, see [Implicitly Typed Local Variables](./implicitly-typed-local-variables.md). +Within a class or struct method, you can use implicit typing to instruct the compiler to determine a variable's type at compile time. ## Records @@ -133,11 +133,3 @@ For more information, see [Records](../../language-reference/builtin-types/recor ## C# Language Specification [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See also - -- [Classes](./classes.md) -- [Objects](./objects.md) -- [Structure types](../../language-reference/builtin-types/struct.md) -- [Records](../../language-reference/builtin-types/record.md) -- [C# Programming Guide](../index.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/inheritance.md b/docs/csharp/fundamentals/object-oriented/inheritance.md similarity index 89% rename from docs/csharp/programming-guide/classes-and-structs/inheritance.md rename to docs/csharp/fundamentals/object-oriented/inheritance.md index 9d6594827d392..d0f9b391390ff 100644 --- a/docs/csharp/programming-guide/classes-and-structs/inheritance.md +++ b/docs/csharp/fundamentals/object-oriented/inheritance.md @@ -16,7 +16,7 @@ ms.assetid: 81d64ee4-50f9-4d6c-a8dc-257c348d2eea Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the *base class*, and the class that inherits those members is called the *derived class*. A derived class can have only one direct base class. However, inheritance is transitive. If `ClassC` is derived from `ClassB`, and `ClassB` is derived from `ClassA`, `ClassC` inherits the members declared in `ClassB` and `ClassA`. > [!NOTE] -> Structs do not support inheritance, but they can implement interfaces. For more information, see [Interfaces](../interfaces/index.md). +> Structs do not support inheritance, but they can implement interfaces. Conceptually, a derived class is a specialization of the base class. For example, if you have a base class `Animal`, you might have one derived class that is named `Mammal` and another derived class that is named `Reptile`. A `Mammal` is an `Animal`, and a `Reptile` is an `Animal`, but each derived class represents different specializations of the base class. @@ -42,24 +42,18 @@ When a base class declares a method as [`virtual`](../../language-reference/keyw ## Abstract base classes -You can declare a class as [abstract](../../language-reference/keywords/abstract.md) if you want to prevent direct instantiation by using the [new](../../language-reference/operators/new-operator.md) operator. An abstract class can be used only if a new class is derived from it. An abstract class can contain one or more method signatures that themselves are declared as abstract. These signatures specify the parameters and return value but have no implementation (method body). An abstract class doesn't have to contain abstract members; however, if a class does contain an abstract member, the class itself must be declared as abstract. Derived classes that aren't abstract themselves must provide the implementation for any abstract methods from an abstract base class. For more information, see [Abstract and Sealed Classes and Class Members](abstract-and-sealed-classes-and-class-members.md). +You can declare a class as [abstract](../../language-reference/keywords/abstract.md) if you want to prevent direct instantiation by using the [new](../../language-reference/operators/new-operator.md) operator. An abstract class can be used only if a new class is derived from it. An abstract class can contain one or more method signatures that themselves are declared as abstract. These signatures specify the parameters and return value but have no implementation (method body). An abstract class doesn't have to contain abstract members; however, if a class does contain an abstract member, the class itself must be declared as abstract. Derived classes that aren't abstract themselves must provide the implementation for any abstract methods from an abstract base class. ## Interfaces An *interface* is a reference type that defines a set of members. All classes and structs that implement that interface must implement that set of members. An interface may define a default implementation for any or all of these members. A class can implement multiple interfaces even though it can derive from only a single direct base class. -Interfaces are used to define specific capabilities for classes that don't necessarily have an "is a" relationship. For example, the interface can be implemented by any class or struct to determine whether two objects of the type are equivalent (however the type defines equivalence). doesn't imply the same kind of "is a" relationship that exists between a base class and a derived class (for example, a `Mammal` is an `Animal`). For more information, see [Interfaces](../interfaces/index.md). +Interfaces are used to define specific capabilities for classes that don't necessarily have an "is a" relationship. For example, the interface can be implemented by any class or struct to determine whether two objects of the type are equivalent (however the type defines equivalence). doesn't imply the same kind of "is a" relationship that exists between a base class and a derived class (for example, a `Mammal` is an `Animal`). For more information, see [Interfaces](../types/interfaces.md). ## Preventing further derivation -A class can prevent other classes from inheriting from it, or from any of its members, by declaring itself or the member as [`sealed`](../../language-reference/keywords/sealed.md). For more information, see [Abstract and Sealed Classes and Class Members](./abstract-and-sealed-classes-and-class-members.md). +A class can prevent other classes from inheriting from it, or from any of its members, by declaring itself or the member as [`sealed`](../../language-reference/keywords/sealed.md). ## Derived class hiding of base class members -A derived class can hide base class members by declaring members with the same name and signature. The [`new`](../../language-reference/keywords/new-modifier.md) modifier can be used to explicitly indicate that the member isn't intended to be an override of the base member. The use of [`new`](../../language-reference/keywords/new-modifier.md) isn't required, but a compiler warning will be generated if [`new`](../../language-reference/keywords/new-modifier.md) isn't used. For more information, see [Versioning with the Override and New Keywords](./versioning-with-the-override-and-new-keywords.md) and [Knowing When to Use Override and New Keywords](./knowing-when-to-use-override-and-new-keywords.md). - -## See also - -- [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) -- [class](../../language-reference/keywords/class.md) +A derived class can hide base class members by declaring members with the same name and signature. The [`new`](../../language-reference/keywords/new-modifier.md) modifier can be used to explicitly indicate that the member isn't intended to be an override of the base member. The use of [`new`](../../language-reference/keywords/new-modifier.md) isn't required, but a compiler warning will be generated if [`new`](../../language-reference/keywords/new-modifier.md) isn't used. For more information, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md) and [Knowing When to Use Override and New Keywords](../../programming-guide/classes-and-structs//knowing-when-to-use-override-and-new-keywords.md). diff --git a/docs/csharp/programming-guide/classes-and-structs/media/inheritance/class-inheritance-diagram.png b/docs/csharp/fundamentals/object-oriented/media/inheritance/class-inheritance-diagram.png similarity index 100% rename from docs/csharp/programming-guide/classes-and-structs/media/inheritance/class-inheritance-diagram.png rename to docs/csharp/fundamentals/object-oriented/media/inheritance/class-inheritance-diagram.png diff --git a/docs/csharp/programming-guide/classes-and-structs/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md similarity index 88% rename from docs/csharp/programming-guide/classes-and-structs/objects.md rename to docs/csharp/fundamentals/object-oriented/objects.md index e1b8d1bc8dcf9..bd7f141ada088 100644 --- a/docs/csharp/programming-guide/classes-and-structs/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -12,7 +12,7 @@ ms.assetid: af4a5230-fbf3-4eea-95e1-8b883c2f845c A class or struct definition is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection. Client code is the code that uses these variables to call the methods and access the public properties of the object. In an object-oriented language such as C#, a typical program consists of multiple objects interacting dynamically. > [!NOTE] -> Static types behave differently than what is described here. For more information, see [Static Classes and Static Class Members](./static-classes-and-static-class-members.md). +> Static types behave differently than what is described here. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md). ## Struct Instances vs. Class Instances @@ -41,25 +41,21 @@ A class or struct definition is like a blueprint that specifies what the type ca [!code-csharp[csProgGuideStatements#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#32)] - The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../statements-expressions-operators/how-to-define-value-equality-for-a-type.md) + The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) -- To determine whether the values of the fields in two class instances are equal, you might be able to use the method or the [== operator](../../language-reference/operators/equality-operators.md#equality-operator-). However, only use them if the class has overridden or overloaded them to provide a custom definition of what "equality" means for objects of that type. The class might also implement the interface or the interface. Both interfaces provide methods that can be used to test value equality. When designing your own classes that override `Equals`, make sure to follow the guidelines stated in [How to define value equality for a type](../statements-expressions-operators/how-to-define-value-equality-for-a-type.md) and . +- To determine whether the values of the fields in two class instances are equal, you might be able to use the method or the [== operator](../../language-reference/operators/equality-operators.md#equality-operator-). However, only use them if the class has overridden or overloaded them to provide a custom definition of what "equality" means for objects of that type. The class might also implement the interface or the interface. Both interfaces provide methods that can be used to test value equality. When designing your own classes that override `Equals`, make sure to follow the guidelines stated in [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) and . ## Related Sections - For more information: - -- [Classes](./classes.md) - -- [Constructors](./constructors.md) - -- [Finalizers](./destructors.md) - -- [Events](../events/index.md) + For more information: +- [Classes](../../programming-guide/classes-and-structs/classes.md) +- [Constructors](../../programming-guide/classes-and-structs/constructors.md) +- [Finalizers](../../programming-guide/classes-and-structs/destructors.md) +- [Events](../../programming-guide/events/index.md) + ## See also -- [C# Programming Guide](../index.md) - [object](../../language-reference/builtin-types/reference-types.md) - [Inheritance](./inheritance.md) - [class](../../language-reference/keywords/class.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/polymorphism.md b/docs/csharp/fundamentals/object-oriented/polymorphism.md similarity index 90% rename from docs/csharp/programming-guide/classes-and-structs/polymorphism.md rename to docs/csharp/fundamentals/object-oriented/polymorphism.md index 04b4a5b9446fe..809a65b09808b 100644 --- a/docs/csharp/programming-guide/classes-and-structs/polymorphism.md +++ b/docs/csharp/fundamentals/object-oriented/polymorphism.md @@ -47,7 +47,7 @@ Fields cannot be virtual; only methods, properties, events, and indexers can be [!code-csharp[Virtual overview example](~/samples/snippets/csharp/objectoriented/Inheritance.cs#SnippetTestVirtualMethods)] -Virtual methods and properties enable derived classes to extend a base class without needing to use the base class implementation of a method. For more information, see [Versioning with the Override and New Keywords](./versioning-with-the-override-and-new-keywords.md). An interface provides another way to define a method or set of methods whose implementation is left to derived classes. For more information, see [Interfaces](../interfaces/index.md). +Virtual methods and properties enable derived classes to extend a base class without needing to use the base class implementation of a method. For more information, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md). An interface provides another way to define a method or set of methods whose implementation is left to derived classes. ### Hide base class members with new members @@ -100,20 +100,3 @@ For more information, see [base](../../language-reference/keywords/base.md). > [!NOTE] > It is recommended that virtual members use `base` to call the base class implementation of that member in their own implementation. Letting the base class behavior occur enables the derived class to concentrate on implementing behavior specific to the derived class. If the base class implementation is not called, it is up to the derived class to make their behavior compatible with the behavior of the base class. - -## In this section - -- [Versioning with the Override and New Keywords](./versioning-with-the-override-and-new-keywords.md) -- [Knowing When to Use Override and New Keywords](./knowing-when-to-use-override-and-new-keywords.md) -- [How to override the ToString method](./how-to-override-the-tostring-method.md) - -## See also - -- [C# Programming Guide](../index.md) -- [Inheritance](./inheritance.md) -- [Abstract and Sealed Classes and Class Members](./abstract-and-sealed-classes-and-class-members.md) -- [Methods](./methods.md) -- [Events](../events/index.md) -- [Properties](./properties.md) -- [Indexers](../indexers/index.md) -- [Types](../types/index.md) diff --git a/docs/csharp/tutorials/intro-to-csharp/introduction-to-classes.md b/docs/csharp/fundamentals/object-oriented/tutorials/classes.md similarity index 90% rename from docs/csharp/tutorials/intro-to-csharp/introduction-to-classes.md rename to docs/csharp/fundamentals/object-oriented/tutorials/classes.md index aa2937c8bd33d..abca66b1fb1c9 100644 --- a/docs/csharp/tutorials/intro-to-csharp/introduction-to-classes.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/classes.md @@ -10,7 +10,7 @@ In this tutorial, you'll build a console application and see the basic object-or ## Prerequisites -The tutorial expects that you have a machine set up for local development. On Windows, Linux, or macOS, you can use the .NET CLI to create, build, and run applications. On Windows, you can use Visual Studio 2019. For setup instructions, see [Set up your local environment](../../tour-of-csharp/tutorials/local-environment.md). +The tutorial expects that you have a machine set up for local development. On Windows, Linux, or macOS, you can use the .NET CLI to create, build, and run applications. On Windows, you can use Visual Studio 2019. For setup instructions, see [Set up your local environment](../../../tour-of-csharp/tutorials/local-environment.md). ## Create your application @@ -87,7 +87,7 @@ public BankAccount(string name, decimal initialBalance) } ``` -Constructors are called when you create an object using [`new`](../../language-reference/operators/new-operator.md). Replace the line `Console.WriteLine("Hello World!");` in *Program.cs* with the following code (replace `` with your name): +Constructors are called when you create an object using [`new`](../../../language-reference/operators/new-operator.md). Replace the line `Console.WriteLine("Hello World!");` in *Program.cs* with the following code (replace `` with your name): ```csharp var account = new BankAccount("", 1000); @@ -143,7 +143,7 @@ This introduces the concept of ***exceptions***. The standard way of indicating :::code language="csharp" source="./snippets/introduction-to-classes/BankAccount.cs" id="DepositAndWithdrawal"::: -The [`throw`](../../language-reference/keywords/throw.md) statement **throws** an exception. Execution of the current block ends, and control transfers to the first matching `catch` block found in the call stack. You'll add a `catch` block to test this code a little later on. +The [`throw`](../../../language-reference/keywords/throw.md) statement **throws** an exception. Execution of the current block ends, and control transfers to the first matching `catch` block found in the call stack. You'll add a `catch` block to test this code a little later on. The constructor should get one change so that it adds an initial transaction, rather than updating the balance directly. Since you already wrote the `MakeDeposit` method, call it from your constructor. The finished constructor should look like this: @@ -173,7 +173,7 @@ catch (ArgumentOutOfRangeException e) } ``` -You use the [`try` and `catch` statements](../../language-reference/keywords/try-catch.md) to mark a block of code that may throw exceptions and to catch those errors that you expect. You can use the same technique to test the code that throws an exception for a negative balance. Add the following code at the end of your `Main` method: +You use the [`try` and `catch` statements](../../../language-reference/keywords/try-catch.md) to mark a block of code that may throw exceptions and to catch those errors that you expect. You can use the same technique to test the code that throws an exception for a negative balance. Add the following code at the end of your `Main` method: ```csharp // Test for a negative balance. @@ -210,11 +210,11 @@ Run your program to see the results. If you got stuck, you can see the source for this tutorial [in our GitHub repo](https://github.com/dotnet/docs/tree/main/docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes). -You can continue with the [object oriented programming](object-oriented-programming.md) tutorial. +You can continue with the [object oriented programming](oop.md) tutorial. You can learn more about these concepts in these articles: -- [If and else statement](../../language-reference/keywords/if-else.md) -- [While statement](../../language-reference/keywords/while.md) -- [Do statement](../../language-reference/keywords/do.md) -- [For statement](../../language-reference/keywords/for.md) +- [If and else statement](../../../language-reference/keywords/if-else.md) +- [While statement](../../../language-reference/keywords/while.md) +- [Do statement](../../../language-reference/keywords/do.md) +- [For statement](../../../language-reference/keywords/for.md) diff --git a/docs/csharp/tutorials/inheritance.md b/docs/csharp/fundamentals/object-oriented/tutorials/inheritance.md similarity index 74% rename from docs/csharp/tutorials/inheritance.md rename to docs/csharp/fundamentals/object-oriented/tutorials/inheritance.md index be4c567cbcf8c..3c79d4c10a1b5 100644 --- a/docs/csharp/tutorials/inheritance.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/inheritance.md @@ -15,16 +15,16 @@ This tutorial assumes that you've installed the .NET Core SDK. Visit the [.NET C ## Running the examples -To create and run the examples in this tutorial, you use the [dotnet](../../core/tools/dotnet.md) utility from the command line. Follow these steps for each example: +To create and run the examples in this tutorial, you use the [dotnet](../../../../core/tools/dotnet.md) utility from the command line. Follow these steps for each example: 1. Create a directory to store the example. -1. Enter the [dotnet new console](../../core/tools/dotnet-new.md) command at a command prompt to create a new .NET Core project. +1. Enter the [dotnet new console](../../../../core/tools/dotnet-new.md) command at a command prompt to create a new .NET Core project. 1. Copy and paste the code from the example into your code editor. -1. Enter the [dotnet restore](../../core/tools/dotnet-restore.md) command from the command line to load or restore the project's dependencies. +1. Enter the [dotnet restore](../../../../core/tools/dotnet-restore.md) command from the command line to load or restore the project's dependencies. [!INCLUDE[DotNet Restore Note](~/includes/dotnet-restore-note.md)] -1. Enter the [dotnet run](../../core/tools/dotnet-run.md) command to compile and execute the example. +1. Enter the [dotnet run](../../../../core/tools/dotnet-run.md) command to compile and execute the example. ## Background: What is inheritance? @@ -34,27 +34,27 @@ C# and .NET support *single inheritance* only. That is, a class can only inherit Not all members of a base class are inherited by derived classes. The following members are not inherited: -- [Static constructors](../programming-guide/classes-and-structs/static-constructors.md), which initialize the static data of a class. +- [Static constructors](../../../programming-guide/classes-and-structs/static-constructors.md), which initialize the static data of a class. -- [Instance constructors](../programming-guide/classes-and-structs/constructors.md), which you call to create a new instance of the class. Each class must define its own constructors. +- [Instance constructors](../../../programming-guide/classes-and-structs/constructors.md), which you call to create a new instance of the class. Each class must define its own constructors. -- [Finalizers](../programming-guide/classes-and-structs/destructors.md), which are called by the runtime's garbage collector to destroy instances of a class. +- [Finalizers](../../../programming-guide/classes-and-structs/destructors.md), which are called by the runtime's garbage collector to destroy instances of a class. While all other members of a base class are inherited by derived classes, whether they are visible or not depends on their accessibility. A member's accessibility affects its visibility for derived classes as follows: -- [Private](../language-reference/keywords/private.md) members are visible only in derived classes that are nested in their base class. Otherwise, they are not visible in derived classes. In the following example, `A.B` is a nested class that derives from `A`, and `C` derives from `A`. The private `A.value` field is visible in A.B. However, if you remove the comments from the `C.GetValue` method and attempt to compile the example, it produces compiler error CS0122: "'A.value' is inaccessible due to its protection level." +- [Private](../../../language-reference/keywords/private.md) members are visible only in derived classes that are nested in their base class. Otherwise, they are not visible in derived classes. In the following example, `A.B` is a nested class that derives from `A`, and `C` derives from `A`. The private `A.value` field is visible in A.B. However, if you remove the comments from the `C.GetValue` method and attempt to compile the example, it produces compiler error CS0122: "'A.value' is inaccessible due to its protection level." - [!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/private.cs#1)] + [!code-csharp[Inheritance](./snippets/inheritance/private.cs#1)] -- [Protected](../language-reference/keywords/protected.md) members are visible only in derived classes. +- [Protected](../../../language-reference/keywords/protected.md) members are visible only in derived classes. -- [Internal](../language-reference/keywords/internal.md) members are visible only in derived classes that are located in the same assembly as the base class. They are not visible in derived classes located in a different assembly from the base class. +- [Internal](../../../language-reference/keywords/internal.md) members are visible only in derived classes that are located in the same assembly as the base class. They are not visible in derived classes located in a different assembly from the base class. -- [Public](../language-reference/keywords/public.md) members are visible in derived classes and are part of the derived class' public interface. Public inherited members can be called just as if they are defined in the derived class. In the following example, class `A` defines a method named `Method1`, and class `B` inherits from class `A`. The example then calls `Method1` as if it were an instance method on `B`. +- [Public](../../../language-reference/keywords/public.md) members are visible in derived classes and are part of the derived class' public interface. Public inherited members can be called just as if they are defined in the derived class. In the following example, class `A` defines a method named `Method1`, and class `B` inherits from class `A`. The example then calls `Method1` as if it were an instance method on `B`. - [!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/basics.cs#1)] + [!code-csharp[Inheritance](./snippets/inheritance/basics.cs#1)] -Derived classes can also *override* inherited members by providing an alternate implementation. In order to be able to override a member, the member in the base class must be marked with the [virtual](../language-reference/keywords/virtual.md) keyword. By default, base class members are not marked as `virtual` and cannot be overridden. Attempting to override a non-virtual member, as the following example does, generates compiler error CS0506: "\ cannot override inherited member \ because it is not marked virtual, abstract, or override. +Derived classes can also *override* inherited members by providing an alternate implementation. In order to be able to override a member, the member in the base class must be marked with the [virtual](../../../language-reference/keywords/virtual.md) keyword. By default, base class members are not marked as `virtual` and cannot be overridden. Attempting to override a non-virtual member, as the following example does, generates compiler error CS0506: "\ cannot override inherited member \ because it is not marked virtual, abstract, or override. ```csharp public class A @@ -74,7 +74,7 @@ public class B : A } ``` -In some cases, a derived class *must* override the base class implementation. Base class members marked with the [abstract](../language-reference/keywords/abstract.md) keyword require that derived classes override them. Attempting to compile the following example generates compiler error CS0534, "<class> does not implement inherited abstract member <member>", because class `B` provides no implementation for `A.Method1`. +In some cases, a derived class *must* override the base class implementation. Base class members marked with the [abstract](../../../language-reference/keywords/abstract.md) keyword require that derived classes override them. Attempting to compile the following example generates compiler error CS0534, "<class> does not implement inherited abstract member <member>", because class `B` provides no implementation for `A.Method1`. ```csharp public abstract class A @@ -107,11 +107,11 @@ Besides any types that they may inherit from through single inheritance, all typ To see what implicit inheritance means, let's define a new class, `SimpleClass`, that is simply an empty class definition: -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/simpleclass.cs#1)] +[!code-csharp[Inheritance](./snippets/inheritance/simpleclass.cs#1)] You can then use reflection (which lets you inspect a type's metadata to get information about that type) to get a list of the members that belong to the `SimpleClass` type. Although you haven't defined any members in your `SimpleClass` class, output from the example indicates that it actually has nine members. One of these members is a parameterless (or default) constructor that is automatically supplied for the `SimpleClass` type by the C# compiler. The remaining eight are members of , the type from which all classes and interfaces in the .NET type system ultimately implicitly inherit. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/simpleclass.cs#2)] +[!code-csharp[Inheritance](./snippets/inheritance/simpleclass.cs#2)] Implicit inheritance from the class makes these methods available to the `SimpleClass` class: @@ -129,7 +129,7 @@ Implicit inheritance from the class makes these methods ava Because of implicit inheritance, you can call any inherited member from a `SimpleClass` object just as if it was actually a member defined in the `SimpleClass` class. For instance, the following example calls the `SimpleClass.ToString` method, which `SimpleClass` inherits from . -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/simpleclass2.cs#1)] +[!code-csharp[Inheritance](./snippets/inheritance/simpleclass2.cs#1)] The following table lists the categories of types that you can create in C# and the types from which they implicitly inherit. Each base type makes a different set of members available through inheritance to implicitly derived types. @@ -149,11 +149,11 @@ Ordinarily, inheritance is used to express an "is a" relationship between a base Note that "is a" also expresses the relationship between a type and a specific instantiation of that type. In the following example, `Automobile` is a class that has three unique read-only properties: `Make`, the manufacturer of the automobile; `Model`, the kind of automobile; and `Year`, its year of manufacture. Your `Automobile` class also has a constructor whose arguments are assigned to the property values, and it overrides the method to produce a string that uniquely identifies the `Automobile` instance rather than the `Automobile` class. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/is-a.cs#1)] +[!code-csharp[Inheritance](./snippets/inheritance/is-a.cs#1)] In this case, you shouldn't rely on inheritance to represent specific car makes and models. For example, you don't need to define a `Packard` type to represent automobiles manufactured by the Packard Motor Car Company. Instead, you can represent them by creating an `Automobile` object with the appropriate values passed to its class constructor, as the following example does. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/is-a.cs#2)] +[!code-csharp[Inheritance](./snippets/inheritance/is-a.cs#2)] An is-a relationship based on inheritance is best applied to a base class and to derived classes that add additional members to the base class or that require additional functionality not present in the base class. @@ -177,23 +177,23 @@ In designing your `Publication` class, you need to make several design decisions For your example, you'll use the small hierarchy of a `Publication` class and a single derived class, `Book`. You could easily extend the example to create a number of additional classes that derive from `Publication`, such as `Magazine` and `Article`. -- Whether it makes sense to instantiate the base class. If it does not, you should apply the [abstract](../language-reference/keywords/abstract.md) keyword to the class. Otherwise, your `Publication` class can be instantiated by calling its class constructor. If an attempt is made to instantiate a class marked with the `abstract` keyword by a direct call to its class constructor, the C# compiler generates error CS0144, "Cannot create an instance of the abstract class or interface." If an attempt is made to instantiate the class by using reflection, the reflection method throws a . +- Whether it makes sense to instantiate the base class. If it does not, you should apply the [abstract](../../../language-reference/keywords/abstract.md) keyword to the class. Otherwise, your `Publication` class can be instantiated by calling its class constructor. If an attempt is made to instantiate a class marked with the `abstract` keyword by a direct call to its class constructor, the C# compiler generates error CS0144, "Cannot create an instance of the abstract class or interface." If an attempt is made to instantiate the class by using reflection, the reflection method throws a . By default, a base class can be instantiated by calling its class constructor. You do not have to explicitly define a class constructor. If one is not present in the base class' source code, the C# compiler automatically provides a default (parameterless) constructor. - For your example, you'll mark the `Publication` class as [abstract](../language-reference/keywords/abstract.md) so that it cannot be instantiated. An `abstract` class without any `abstract` methods indicates that this class represents an abstract concept that is shared among several concrete classes (like a `Book`, `Journal`). + For your example, you'll mark the `Publication` class as [abstract](../../../language-reference/keywords/abstract.md) so that it cannot be instantiated. An `abstract` class without any `abstract` methods indicates that this class represents an abstract concept that is shared among several concrete classes (like a `Book`, `Journal`). -- Whether derived classes must inherit the base class implementation of particular members, whether they have the option to override the base class implementation, or whether they must provide an implementation. You use the [abstract](../language-reference/keywords/abstract.md) keyword to force derived classes to provide an implementation. You use the [virtual](../language-reference/keywords/virtual.md) keyword to allow derived classes to override a base class method. By default, methods defined in the base class are *not* overridable. +- Whether derived classes must inherit the base class implementation of particular members, whether they have the option to override the base class implementation, or whether they must provide an implementation. You use the [abstract](../../../language-reference/keywords/abstract.md) keyword to force derived classes to provide an implementation. You use the [virtual](../../../language-reference/keywords/virtual.md) keyword to allow derived classes to override a base class method. By default, methods defined in the base class are *not* overridable. The `Publication` class does not have any `abstract` methods, but the class itself is `abstract`. -- Whether a derived class represents the final class in the inheritance hierarchy and cannot itself be used as a base class for additional derived classes. By default, any class can serve as a base class. You can apply the [sealed](../language-reference/keywords/sealed.md) keyword to indicate that a class cannot serve as a base class for any additional classes. Attempting to derive from a sealed class generated compiler error CS0509, "cannot derive from sealed type \". +- Whether a derived class represents the final class in the inheritance hierarchy and cannot itself be used as a base class for additional derived classes. By default, any class can serve as a base class. You can apply the [sealed](../../../language-reference/keywords/sealed.md) keyword to indicate that a class cannot serve as a base class for any additional classes. Attempting to derive from a sealed class generated compiler error CS0509, "cannot derive from sealed type \". For your example, you'll mark your derived class as `sealed`. The following example shows the source code for the `Publication` class, as well as a `PublicationType` enumeration that is returned by the `Publication.PublicationType` property. In addition to the members that it inherits from , the `Publication` class defines the following unique members and member overrides: -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/base-and-derived.cs#1)] +[!code-csharp[Inheritance](./snippets/inheritance/base-and-derived.cs#1)] - A constructor @@ -230,13 +230,13 @@ The following example shows the source code for the `Publication` class, as well The following figure illustrates the relationship between your base `Publication` class and its implicitly inherited class. -![The Object and Publication classes](media/publication-class.jpg) +![The Object and Publication classes](media/inheritance/publication-class.jpg) ### The `Book` class The `Book` class represents a book as a specialized type of publication. The following example shows the source code for the `Book` class. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/base-and-derived.cs#2)] +[!code-csharp[Inheritance](./snippets/inheritance/base-and-derived.cs#2)] In addition to the members that it inherits from `Publication`, the `Book` class defines the following unique members and member overrides: @@ -244,9 +244,9 @@ In addition to the members that it inherits from `Publication`, the `Book` class The two `Book` constructors share three common parameters. Two, *title* and *publisher*, correspond to parameters of the `Publication` constructor. The third is *author*, which is stored to a public immutable `Author` property. One constructor includes an *isbn* parameter, which is stored in the `ISBN` auto-property. - The first constructor uses the [this](../language-reference/keywords/this.md) keyword to call the other constructor. Constructor chaining is a common pattern in defining constructors. Constructors with fewer parameters provide default values when calling the constructor with the greatest number of parameters. + The first constructor uses the [this](../../../language-reference/keywords/this.md) keyword to call the other constructor. Constructor chaining is a common pattern in defining constructors. Constructors with fewer parameters provide default values when calling the constructor with the greatest number of parameters. - The second constructor uses the [base](../language-reference/keywords/base.md) keyword to pass the title and publisher name to the base class constructor. If you don't make an explicit call to a base class constructor in your source code, the C# compiler automatically supplies a call to the base class' default or parameterless constructor. + The second constructor uses the [base](../../../language-reference/keywords/base.md) keyword to pass the title and publisher name to the base class constructor. If you don't make an explicit call to a base class constructor in your source code, the C# compiler automatically supplies a call to the base class' default or parameterless constructor. - A read-only `ISBN` property, which returns the `Book` object's International Standard Book Number, a unique 10- or 13-digit number. The ISBN is supplied as an argument to one of the `Book` constructors. The ISBN is stored in a private backing field, which is auto-generated by the compiler. @@ -264,11 +264,11 @@ In addition to the members that it inherits from `Publication`, the `Book` class The following figure illustrates the relationship between the `Book` class and `Publication`, its base class. -![Publication and Book classes](media/book-class.jpg) +![Publication and Book classes](media/inheritance/book-class.jpg) You can now instantiate a `Book` object, invoke both its unique and inherited members, and pass it as an argument to a method that expects a parameter of type `Publication` or of type `Book`, as the following example shows. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/use-publication.cs#1)] +[!code-csharp[Inheritance](./snippets/inheritance/use-publication.cs#1)] ## Designing abstract base classes and their derived classes @@ -278,18 +278,14 @@ In the previous example, you defined a base class that provided an implementatio For example, each closed two-dimensional geometric shape includes two properties: area, the inner extent of the shape; and perimeter, or the distance along the edges of the shape. The way in which these properties are calculated, however, depends completely on the specific shape. The formula for calculating the perimeter (or circumference) of a circle, for example, is different from that of a triangle. The `Shape` class is an `abstract` class with `abstract` methods. That indicates derived classes share the same functionality, but those derived classes implement that functionality differently. -The following example defines an abstract base class named `Shape` that defines two properties: `Area` and `Perimeter`. In addition to marking the class with the [abstract](../language-reference/keywords/abstract.md) keyword, each instance member is also marked with the [abstract](../language-reference/keywords/abstract.md) keyword. In this case, `Shape` also overrides the method to return the name of the type, rather than its fully qualified name. And it defines two static members, `GetArea` and `GetPerimeter`, that allow callers to easily retrieve the area and perimeter of an instance of any derived class. When you pass an instance of a derived class to either of these methods, the runtime calls the method override of the derived class. +The following example defines an abstract base class named `Shape` that defines two properties: `Area` and `Perimeter`. In addition to marking the class with the [abstract](../../../language-reference/keywords/abstract.md) keyword, each instance member is also marked with the [abstract](../../../language-reference/keywords/abstract.md) keyword. In this case, `Shape` also overrides the method to return the name of the type, rather than its fully qualified name. And it defines two static members, `GetArea` and `GetPerimeter`, that allow callers to easily retrieve the area and perimeter of an instance of any derived class. When you pass an instance of a derived class to either of these methods, the runtime calls the method override of the derived class. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/shape.cs#1)] +[!code-csharp[Inheritance](./snippets/inheritance/shape.cs#1)] You can then derive some classes from `Shape` that represent specific shapes. The following example defines three classes, `Triangle`, `Rectangle`, and `Circle`. Each uses a formula unique for that particular shape to compute the area and perimeter. Some of the derived classes also define properties, such as `Rectangle.Diagonal` and `Circle.Diameter`, that are unique to the shape that they represent. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/shape.cs#2)] +[!code-csharp[Inheritance](./snippets/inheritance/shape.cs#2)] The following example uses objects derived from `Shape`. It instantiates an array of objects derived from `Shape` and calls the static methods of the `Shape` class, which wraps return `Shape` property values. The runtime retrieves values from the overridden properties of the derived types. The example also casts each `Shape` object in the array to its derived type and, if the cast succeeds, retrieves properties of that particular subclass of `Shape`. -[!code-csharp[Inheritance](../../../samples/snippets/csharp/tutorials/inheritance/shape.cs#3)] - -## See also - -- [Inheritance (C# Programming Guide)](../programming-guide/classes-and-structs/inheritance.md) +[!code-csharp[Inheritance](./snippets/inheritance/shape.cs#3)] diff --git a/docs/csharp/tutorials/media/book-class.jpg b/docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/book-class.jpg similarity index 100% rename from docs/csharp/tutorials/media/book-class.jpg rename to docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/book-class.jpg diff --git a/docs/csharp/tutorials/media/publication-class.jpg b/docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/publication-class.jpg similarity index 100% rename from docs/csharp/tutorials/media/publication-class.jpg rename to docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/publication-class.jpg diff --git a/docs/csharp/tutorials/intro-to-csharp/object-oriented-programming.md b/docs/csharp/fundamentals/object-oriented/tutorials/oop.md similarity index 96% rename from docs/csharp/tutorials/intro-to-csharp/object-oriented-programming.md rename to docs/csharp/fundamentals/object-oriented/tutorials/oop.md index f02fe2a04040e..98bdd1d4a98e6 100644 --- a/docs/csharp/tutorials/intro-to-csharp/object-oriented-programming.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/oop.md @@ -12,7 +12,7 @@ C# is an object-oriented programming language. The four basic principles of obje - *Inheritance* Ability to create new abstractions based on existing abstractions. - *Polymorphism* Ability to implement inherited properties or methods in different ways across multiple abstractions. -In the preceding tutorial, [introduction to classes](introduction-to-classes.md) you saw both *abstraction* and *encapsulation*. The `BankAccount` class provided an abstraction for the concept of a bank account. You could modify its implementation without affecting any of the code that used the `BankAccount` class. Both the `BankAccount` and `Transaction` classes provide encapsulation of the components needed to describe those concepts in code. +In the preceding tutorial, [introduction to classes](classes.md) you saw both *abstraction* and *encapsulation*. The `BankAccount` class provided an abstraction for the concept of a bank account. You could modify its implementation without affecting any of the code that used the `BankAccount` class. Both the `BankAccount` and `Transaction` classes provide encapsulation of the components needed to describe those concepts in code. In this tutorial, you'll extend that application to make use of *inheritance* and *polymorphism* to add new features. You'll also add features to the `BankAccount` class, taking advantage of the *abstraction* and *encapsulation* techniques you learned in the preceding tutorial. @@ -185,5 +185,3 @@ This tutorial demonstrated many of the techniques used in Object-Oriented progra - You used *Encapsulation* when you kept many details `private` in each class. - You used *Inheritance* when you leveraged the implementation already created in the `BankAccount` class to save code. - You used *Polymorphism* when you created `virtual` methods that derived classes could override to create specific behavior for that account type. - -Congratulations, you've finished all of our introduction to C# tutorials. To learn more, try more of our [tutorials](introduction-to-classes.md). diff --git a/docs/csharp/how-to/safely-cast-using-pattern-matching-is-and-as-operators.md b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md similarity index 63% rename from docs/csharp/how-to/safely-cast-using-pattern-matching-is-and-as-operators.md rename to docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md index 527ba3e752dae..68cf18294542d 100644 --- a/docs/csharp/how-to/safely-cast-using-pattern-matching-is-and-as-operators.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md @@ -9,17 +9,17 @@ helpviewer_keywords: --- # How to safely cast by using pattern matching and the is and as operators -Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](../programming-guide/types/index.md). To access the derived type's instance members, it's necessary to [cast](../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](../pattern-matching.md) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. +Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](../../../programming-guide/types/index.md). To access the derived type's instance members, it's necessary to [cast](../../../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](../../../pattern-matching.md) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../../../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../../../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. The following example shows how to use the pattern matching `is` statement: -:::code language="csharp" source="../../../samples/snippets/csharp/how-to/safelycast/patternmatching/Program.cs" id="PatternMatchingIs"::: +:::code language="csharp" source="../../../../../samples/snippets/csharp/how-to/safelycast/patternmatching/Program.cs" id="PatternMatchingIs"::: -The preceding sample demonstrates a few features of pattern matching syntax. The `if (a is Mammal m)` statement combines the test with an initialization assignment. The assignment occurs only when the test succeeds. The variable `m` is only in scope in the embedded `if` statement where it has been assigned. You cannot access `m` later in the same method. The preceding example also shows how to use the [`as` operator](../language-reference/operators/type-testing-and-cast.md#as-operator) to convert an object to a specified type. +The preceding sample demonstrates a few features of pattern matching syntax. The `if (a is Mammal m)` statement combines the test with an initialization assignment. The assignment occurs only when the test succeeds. The variable `m` is only in scope in the embedded `if` statement where it has been assigned. You cannot access `m` later in the same method. The preceding example also shows how to use the [`as` operator](../../../language-reference/operators/type-testing-and-cast.md#as-operator) to convert an object to a specified type. -You can also use the same syntax for testing if a [nullable value type](../language-reference/builtin-types/nullable-value-types.md) has a value, as shown in the following example: +You can also use the same syntax for testing if a [nullable value type](../../../language-reference/builtin-types/nullable-value-types.md) has a value, as shown in the following example: -:::code language="csharp" source="../../../samples/snippets/csharp/how-to/safelycast/nullablepatternmatching/Program.cs" id="PatternMatchingNullable"::: +:::code language="csharp" source="../../../../../samples/snippets/csharp/how-to/safelycast/nullablepatternmatching/Program.cs" id="PatternMatchingNullable"::: The preceding sample demonstrates other features of pattern matching to use with conversions. You can test a variable for the null pattern by checking specifically for the `null` value. When the runtime value of the variable is `null`, an `is` statement checking for a type always returns `false`. The pattern matching `is` statement doesn't allow a nullable value type, such as `int?` or `Nullable`, but you can test for any other value type. The `is` patterns from the preceding example are not limited to the nullable value types. You can also use those patterns to test if a variable of a reference type has a value or it's `null`. @@ -27,6 +27,6 @@ The preceding sample also shows how you use the type pattern in a `switch` state If you want to test if a variable is a given type, but not assign it to a new variable, you can use the `is` and `as` operators for reference types and nullable value types. The following code shows how to use the `is` and `as` statements that were part of the C# language before pattern matching was introduced to test if a variable is of a given type: -:::code language="csharp" source="../../../samples/snippets/csharp/how-to/safelycast/asandis/Program.cs" id="IsAndAs"::: +:::code language="csharp" source="../../../../../samples/snippets/csharp/how-to/safelycast/asandis/Program.cs" id="IsAndAs"::: As you can see by comparing this code with the pattern matching code, the pattern matching syntax provides more robust features by combining the test and the assignment in a single statement. Use the pattern matching syntax whenever possible. diff --git a/samples/snippets/csharp/tutorials/inheritance/base-and-derived.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/base-and-derived.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs diff --git a/samples/snippets/csharp/tutorials/inheritance/basics.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/basics.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs diff --git a/samples/snippets/csharp/tutorials/inheritance/is-a.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/is-a.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/is-a.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/is-a.cs diff --git a/samples/snippets/csharp/tutorials/inheritance/private.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/private.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs diff --git a/samples/snippets/csharp/tutorials/inheritance/shape.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/shape.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/shape.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/shape.cs diff --git a/samples/snippets/csharp/tutorials/inheritance/simpleclass.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/simpleclass.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs diff --git a/samples/snippets/csharp/tutorials/inheritance/simpleclass2.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/simpleclass2.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs diff --git a/samples/snippets/csharp/tutorials/inheritance/use-publication.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs similarity index 100% rename from samples/snippets/csharp/tutorials/inheritance/use-publication.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/BankAccount.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/BankAccount.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/BankAccount.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/BankAccount.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/Program.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/Program.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/Program.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/Program.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/classes.csproj b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/classes.csproj similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/classes.csproj rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/classes.csproj diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/transaction.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/transaction.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/introduction-to-classes/transaction.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/transaction.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/BankAccount.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/BankAccount.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/BankAccount.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/BankAccount.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/GiftCardAccount.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/GiftCardAccount.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/GiftCardAccount.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/GiftCardAccount.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/InterestEarningAccount.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/InterestEarningAccount.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/InterestEarningAccount.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/InterestEarningAccount.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/LineOfCreditAccount.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/LineOfCreditAccount.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/LineOfCreditAccount.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/LineOfCreditAccount.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/Program.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/Program.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/Program.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/Program.cs diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/object-oriented-programming.csproj b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/object-oriented-programming.csproj similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/object-oriented-programming.csproj rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/object-oriented-programming.csproj diff --git a/docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/transaction.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/transaction.cs similarity index 100% rename from docs/csharp/tutorials/intro-to-csharp/snippets/object-oriented-programming/transaction.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/transaction.cs diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 462f1b938ab65..91c56bff29472 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -80,30 +80,27 @@ items: items: - name: Overview displayName: classes, structs, records - href: programming-guide/classes-and-structs/index.md + href: fundamentals/object-oriented/index.md - name: Objects - href: programming-guide/classes-and-structs/objects.md + href: fundamentals/object-oriented/objects.md - name: Inheritance - href: programming-guide/classes-and-structs/inheritance.md - - name: Encapsulation - href: programming-guide/classes-and-structs/index.md - - name: Inheritance - href: programming-guide/classes-and-structs/inheritance.md + href: fundamentals/object-oriented/inheritance.md + # Encapsulation & composition - name: Polymorphism - href: programming-guide/classes-and-structs/polymorphism.md + href: fundamentals/object-oriented/polymorphism.md - name: Tutorials items: - name: Introduction to classes - href: tutorials/intro-to-csharp/introduction-to-classes.md + href: fundamentals/object-oriented/tutorials/classes.md - name: Object-Oriented C# - href: tutorials/intro-to-csharp/object-oriented-programming.md + href: fundamentals/object-oriented/tutorials/oop.md # Composing objects from parts - name: Inheritance in C# and .NET - href: tutorials/inheritance.md + href: fundamentals/object-oriented/tutorials/inheritance.md # programming to interfaces - name: Converting types displayName: cast, is, as - href: how-to/safely-cast-using-pattern-matching-is-and-as-operators.md + href: fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md - name: Functional techniques items: # - Conceptual overview From b09e877e23ea1fa276e79bc106bc0262b2a6406c Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 10 May 2021 14:04:13 -0400 Subject: [PATCH 13/60] fix snippet build warnings. --- .../tutorials/snippets/inheritance/Program.cs | 17 ++++ .../snippets/inheritance/base-and-derived.cs | 8 -- .../tutorials/snippets/inheritance/basics.cs | 29 ++++--- .../snippets/inheritance/inheritance.csproj | 10 +++ .../tutorials/snippets/inheritance/private.cs | 4 +- .../snippets/inheritance/simpleclass.cs | 2 +- .../snippets/inheritance/simpleclass2.cs | 8 +- .../snippets/inheritance/use-publication.cs | 79 +------------------ 8 files changed, 51 insertions(+), 106 deletions(-) create mode 100644 docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/Program.cs create mode 100644 docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/inheritance.csproj diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/Program.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/Program.cs new file mode 100644 index 0000000000000..043977539c5e5 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/Program.cs @@ -0,0 +1,17 @@ +using System; + +namespace inheritance +{ + class Program + { + static void Main(string[] args) + { + Basic.Example.Main(); + IsA_Namespace.Example.Main(); + AccessExample.Main(args); + Example.Example.Main(); + ClassNameExample.Main(); + ClassExample.Main(); + } + } +} diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs index 06e48e786e912..936ed4c59bedc 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs @@ -142,11 +142,3 @@ public override bool Equals(object obj) } // } - -public class Program -{ - public static void Main(string[] args) - { - Console.WriteLine("Hello World!"); - } -} diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs index 643e06c83e1dc..6836a9a7674ec 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs @@ -1,23 +1,26 @@ using System; -// -public class A +namespace Basic { - public void Method1() + // + public class A { - // Method implementation. + public void Method1() + { + // Method implementation. + } } -} -public class B : A -{ } + public class B : A + { } -public class Example -{ - public static void Main() + public class Example { - B b = new B(); - b.Method1(); + public static void Main() + { + B b = new B(); + b.Method1(); + } } + // } -// diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/inheritance.csproj b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/inheritance.csproj new file mode 100644 index 0000000000000..6a37b4b4deda7 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/inheritance.csproj @@ -0,0 +1,10 @@ + + + + Exe + net5.0 + enable + inheritance.Program + + + diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs index 641e475b8bcaa..321a6fd1bc76b 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs @@ -22,7 +22,7 @@ public class C : A // } } -public class Example +public class AccessExample { public static void Main(string[] args) { @@ -32,4 +32,4 @@ public static void Main(string[] args) } // The example displays the following output: // 10 -// \ No newline at end of file +// diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs index e5d4a2a08a693..853740845dfdb 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs @@ -2,7 +2,7 @@ using System; using System.Reflection; -public class Example +public class SimpleClassExample { public static void Main() { diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs index 5b473925590fb..1227005ec9b7d 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs @@ -1,17 +1,17 @@ // using System; -public class SimpleClass +public class EmptyClass {} -public class Example +public class ClassNameExample { public static void Main() { - SimpleClass sc = new SimpleClass(); + EmptyClass sc = new EmptyClass(); Console.WriteLine(sc.ToString()); } } // The example displays the following output: -// SimpleClass +// EmptyClass // diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs index 81421dde5d7ac..cfa8284ee42ea 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs +++ b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs @@ -2,7 +2,7 @@ using System; using static System.Console; -public class Example +public class ClassExample { public static void Main() { @@ -30,83 +30,6 @@ public static void ShowPublicationInfo(Publication pub) // The Tempest and The Tempest are the same publication: False // -public enum PublicationType { Misc, Book, Magazine, Article }; - -public abstract class Publication -{ - private bool published = false; - private DateTime datePublished; - private int totalPages; - - public Publication(string title, string publisher, PublicationType type) - { - if (publisher == null) - throw new ArgumentNullException("The publisher cannot be null."); - else if (String.IsNullOrWhiteSpace(publisher)) - throw new ArgumentException("The publisher cannot consist only of white space."); - Publisher = publisher; - - if (title == null) - throw new ArgumentNullException("The title cannot be null."); - else if (String.IsNullOrWhiteSpace(title)) - throw new ArgumentException("The title cannot consist only of white space."); - Title = title; - - Type = type; - } - - public string Publisher { get; } - - public string Title { get; } - - public PublicationType Type { get; } - - public string CopyrightName { get; private set; } - - public int CopyrightDate - { get; private set; } - - public int Pages - { get { return totalPages; } - set - { - if (value <= 0) - throw new ArgumentOutOfRangeException("The number of pages cannot be zero or negative."); - totalPages = value; - } - } - - public string GetPublicationDate() - { - if (!published) - return "NYP"; - else - return datePublished.ToString("d"); - } - - public void Publish(DateTime datePublished) - { - published = true; - this.datePublished = datePublished; - } - - public void Copyright(string copyrightName, int copyrightDate) - { - if (copyrightName == null) - throw new ArgumentNullException("The name of the copyright holder cannot be null."); - else if (String.IsNullOrWhiteSpace(copyrightName)) - throw new ArgumentException("The name of the copyright holder cannot consist only of white space."); - CopyrightName = copyrightName; - - int currentYear = DateTime.Now.Year; - if (copyrightDate < currentYear - 10 || copyrightDate > currentYear + 2) - throw new ArgumentOutOfRangeException($"The copyright year must be between {currentYear -10} and {currentYear + 1}"); - CopyrightDate = copyrightDate; - } - - public override string ToString() => Title; -} - public sealed class Book : Publication { public Book(string title, string author, string publisher) : From f4a6a37562d092a904cabbc9e5bc521c770cdf5b Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 10 May 2021 17:14:15 -0400 Subject: [PATCH 14/60] move articles to functional section --- .openpublishing.redirection.json | 16 +++++ .../functional}/deconstruct.md | 30 ++++----- .../{ => fundamentals/functional}/discards.md | 10 +-- .../functional}/pattern-matching.md | 24 +++---- .../snippets/deconstructing-tuples/Program.cs | 18 ++++++ .../deconstructing-tuples/class-discard1.cs | 64 +++++++++++++++++++ .../deconstruct-class1.cs | 35 ++++++++++ .../deconstruct-class2.cs | 2 +- .../deconstruct-extension1.cs | 2 +- .../deconstruct-tuple1.cs | 4 +- .../deconstruct-tuple2.cs | 5 +- .../deconstruct-tuple3.cs | 5 +- .../deconstruct-tuple4.cs | 5 +- .../deconstruct-tuple5.cs | 4 +- .../deconstruction.csproj | 10 +++ .../deconstructing-tuples/discard-tuple1.cs | 3 +- .../functional}/snippets/discards/Program.cs | 0 .../snippets/discards/discard-class.cs | 0 .../snippets/discards/discard-out1.cs | 0 .../snippets/discards/discard-pattern2.cs | 0 .../snippets/discards/discard-tuple.cs | 0 .../snippets/discards/discards.csproj | 0 .../snippets/discards/standalone-discard1.cs | 0 .../snippets/discards/standalone-discard2.cs | 0 .../snippets/patterns/OrderProcessor.cs | 0 .../functional}/snippets/patterns/Program.cs | 0 .../snippets/patterns/Simulation.cs | 0 .../snippets/patterns/patterns.csproj | 0 .../functional}/tutorials/pattern-matching.md | 22 +++---- .../toll-calculator/ExternalSystems.cs | 0 .../finished/toll-calculator/Program.cs | 0 .../toll-calculator/TollCalculator.cs | 0 .../toll-calculator/toll-calculator.csproj | 0 .../start/toll-calculator/ExternalSystems.cs | 0 .../patterns/start/toll-calculator/Program.cs | 0 .../toll-calculator/toll-calculator.csproj | 0 docs/csharp/toc.yml | 11 ++-- docs/csharp/whats-new/csharp-7.md | 26 +++++++- .../deconstructing-tuples/class-discard1.cs | 61 ------------------ .../deconstruct-class1.cs | 34 ---------- 40 files changed, 225 insertions(+), 166 deletions(-) rename docs/csharp/{ => fundamentals/functional}/deconstruct.md (74%) rename docs/csharp/{ => fundamentals/functional}/discards.md (88%) rename docs/csharp/{ => fundamentals/functional}/pattern-matching.md (55%) create mode 100644 docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/Program.cs create mode 100644 docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/class-discard1.cs create mode 100644 docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-class1.cs rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/deconstruct-class2.cs (97%) rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/deconstruct-extension1.cs (99%) rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/deconstruct-tuple1.cs (91%) rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/deconstruct-tuple2.cs (84%) rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/deconstruct-tuple3.cs (84%) rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/deconstruct-tuple4.cs (84%) rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/deconstruct-tuple5.cs (92%) create mode 100644 docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj rename {samples/snippets/csharp/programming-guide => docs/csharp/fundamentals/functional/snippets}/deconstructing-tuples/discard-tuple1.cs (94%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/Program.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/discard-class.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/discard-out1.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/discard-pattern2.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/discard-tuple.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/discards.csproj (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/standalone-discard1.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/discards/standalone-discard2.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/patterns/OrderProcessor.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/patterns/Program.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/patterns/Simulation.cs (100%) rename docs/csharp/{ => fundamentals/functional}/snippets/patterns/patterns.csproj (100%) rename docs/csharp/{ => fundamentals/functional}/tutorials/pattern-matching.md (85%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/functional/tutorials/snippets}/patterns/finished/toll-calculator/ExternalSystems.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/functional/tutorials/snippets}/patterns/finished/toll-calculator/Program.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/functional/tutorials/snippets}/patterns/finished/toll-calculator/TollCalculator.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/functional/tutorials/snippets}/patterns/finished/toll-calculator/toll-calculator.csproj (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/functional/tutorials/snippets}/patterns/start/toll-calculator/ExternalSystems.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/functional/tutorials/snippets}/patterns/start/toll-calculator/Program.cs (100%) rename {samples/snippets/csharp/tutorials => docs/csharp/fundamentals/functional/tutorials/snippets}/patterns/start/toll-calculator/toll-calculator.csproj (100%) delete mode 100644 samples/snippets/csharp/programming-guide/deconstructing-tuples/class-discard1.cs delete mode 100644 samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class1.cs diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 38db9f687c3af..f8b8cddad30df 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -896,10 +896,18 @@ "source_path": "docs/csharp/csharp.md", "redirect_url": "/dotnet/csharp" }, + { + "source_path": "docs/csharp/deconstruct.md", + "redirect_url": "/dotnet/csharp/fundamentals/functional/deconstruct" + }, { "source_path": "docs/csharp/delegates-events.md", "redirect_url": "/dotnet/csharp/delegates-overview" }, + { + "source_path": "docs/csharp/discards.md", + "redirect_url": "/dotnet/csharp/fundamentals/functional/discards" + }, { "source_path": "docs/csharp/features.md", "redirect_url": "/dotnet/csharp/programming-guide/concepts" @@ -1789,6 +1797,10 @@ "source_path": "docs/csharp/parallel.md", "redirect_url": "/dotnet/standard/parallel-programming/index" }, + { + "source_path": "docs/csharp/pattern-matching.md", + "redirect_url": "/dotnet/csharp/fundamentals/functiona/pattern-matching" + }, { "source_path": "docs/csharp/programming-guide/arrays/arrays-as-objects.md", "redirect_url": "/dotnet/csharp/programming-guide/arrays" @@ -3353,6 +3365,10 @@ "source_path": "docs/csharp/tutorials/microservices.md", "redirect_url": "/dotnet/core/docker/" }, + { + "source_path": "docs/csharp/tutorials/pattern-matching.md", + "redirect_url": "/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching" + }, { "source_path": "docs/csharp/type-system.md", "redirect_url": "/dotnet/csharp/programming-guide/types/index" diff --git a/docs/csharp/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md similarity index 74% rename from docs/csharp/deconstruct.md rename to docs/csharp/fundamentals/functional/deconstruct.md index 4db731ea58d3b..d699c1025657d 100644 --- a/docs/csharp/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -8,7 +8,7 @@ ms.date: 03/22/2021 A tuple provides a lightweight way to retrieve multiple values from a method call. But once you retrieve the tuple, you have to handle its individual elements. Doing this on an element-by-element basis is cumbersome, as the following example shows. The `QueryCityData` method returns a 3-tuple, and each of its elements is assigned to a variable in a separate operation. -[!code-csharp[WithoutDeconstruction](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple1.cs)] +:::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple1.cs"::: Retrieving multiple field and property values from an object can be equally cumbersome: you have to assign a field or property value to a variable on a member-by-member basis. @@ -26,21 +26,21 @@ There are three ways to deconstruct a tuple: - You can explicitly declare the type of each field inside parentheses. The following example uses this approach to deconstruct the 3-tuple returned by the `QueryCityData` method. - [!code-csharp[Deconstruction-Explicit](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple2.cs#1)] +:::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple2.cs" ID="Snippet1"::: - You can use the `var` keyword so that C# infers the type of each variable. You place the `var` keyword outside of the parentheses. The following example uses type inference when deconstructing the 3-tuple returned by the `QueryCityData` method. - [!code-csharp[Deconstruction-Infer](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple3.cs#1)] + :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple3.cs" ID="Snippet1"::: You can also use the `var` keyword individually with any or all of the variable declarations inside the parentheses. - [!code-csharp[Deconstruction-Infer-Some](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple4.cs#1)] + :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple4.cs" ID="Snippet1"::: This is cumbersome and is not recommended. - Lastly, you may deconstruct the tuple into variables that have already been declared. - [!code-csharp[Deconstruction-Declared](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple5.cs#1)] + :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple5.cs" ID="Snippet1"::: Note that you cannot specify a specific type outside the parentheses even if every field in the tuple has the same type. This generates compiler error CS8136, "Deconstruction 'var (...)' form disallows a specific type for 'var'.". @@ -55,17 +55,17 @@ Often when deconstructing a tuple, you're interested in the values of only some The following example illustrates the use of tuples with discards. The `QueryCityDataForYears` method returns a 6-tuple with the name of a city, its area, a year, the city's population for that year, a second year, and the city's population for that second year. The example shows the change in population between those two years. Of the data available from the tuple, we're unconcerned with the city area, and we know the city name and the two dates at design-time. As a result, we're only interested in the two population values stored in the tuple, and can handle its remaining values as discards. -[!code-csharp[Tuple-discard](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/discard-tuple1.cs)] +:::code language="csharp" source="./snippets/deconstructing-tuples/discard-tuple1.cs"::: ## Deconstructing user-defined types -C# does not offer built-in support for deconstructing non-tuple types other than the [`record`](#deconstructing-a-record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: +C# does not offer built-in support for deconstructing non-tuple types other than the [`record`](#deconstructing-a-record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: -[!code-csharp[Class-deconstruct](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class1.cs#1)] +:::code language="csharp" source="./snippets/deconstructing-tuples/discard-tuple2.cs" ID="Snippet1"::: You can then deconstruct an instance of the `Person` class named `p` with an assignment like the following: -[!code-csharp[Class-deconstruct](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class1.cs#2)] +:::code language="csharp" source="./snippets/deconstructing-tuples/discard-tuple2.cs" ID="Snippet2"::: The following example overloads the `Deconstruct` method to return various combinations of properties of a `Person` object. Individual overloads return: @@ -73,7 +73,7 @@ The following example overloads the `Deconstruct` method to return various combi - A first, middle, and last name. - A first name, a last name, a city name, and a state name. -[!code-csharp[Class-deconstruct](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class2.cs)] +:::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-class2.cs"::: Multiple `Deconstruct` methods having the same number of parameters are ambiguous. You must be careful to define `Deconstruct` methods with different numbers of parameters, or "arity". `Deconstruct` methods with the same number of parameters cannot be distinguished during overload resolution. @@ -83,21 +83,21 @@ Just as you do with [tuples](#deconstructing-tuple-elements-with-discards), you The following example deconstructs a `Person` object into four strings (the first and last names, the city, and the state) but discards the last name and the state. -[!code-csharp[Class-discard](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/class-discard1.cs#1)] +:::code language="csharp" source="./snippets/deconstructing-tuples/class-discard1.cs" ID="Snippet1"::: ## Deconstructing a user-defined type with an extension method -If you didn't author a class, struct, or interface, you can still deconstruct objects of that type by implementing one or more `Deconstruct` [extension methods](programming-guide/classes-and-structs/extension-methods.md) to return the values in which you're interested. +If you didn't author a class, struct, or interface, you can still deconstruct objects of that type by implementing one or more `Deconstruct` [extension methods](../../programming-guide/classes-and-structs/extension-methods.md) to return the values in which you're interested. The following example defines two `Deconstruct` extension methods for the class. The first returns a set of values that indicate the characteristics of the property, including its type, whether it's static or instance, whether it's read-only, and whether it's indexed. The second indicates the property's accessibility. Because the accessibility of get and set accessors can differ, Boolean values indicate whether the property has separate get and set accessors and, if it does, whether they have the same accessibility. If there is only one accessor or both the get and the set accessor have the same accessibility, the `access` variable indicates the accessibility of the property as a whole. Otherwise, the accessibility of the get and set accessors are indicated by the `getAccess` and `setAccess` variables. -[!code-csharp[Extension-deconstruct](../../samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-extension1.cs)] +[!code-csharp[Extension-deconstruct](./snippets/deconstructing-tuples/deconstruct-extension1.cs)] ## Deconstructing a `record` type -When you declare a [record](language-reference/builtin-types/record.md) type by using two or more positional parameters, the compiler creates a `Deconstruct` method with an `out` parameter for each positional parameter in the `record` declaration. For more information, see [Positional syntax for property definition](language-reference/builtin-types/record.md#positional-syntax-for-property-definition) and [Deconstructor behavior in derived records](language-reference/builtin-types/record.md#deconstructor-behavior-in-derived-records). +When you declare a [record](../../language-reference/builtin-types/record.md) type by using two or more positional parameters, the compiler creates a `Deconstruct` method with an `out` parameter for each positional parameter in the `record` declaration. For more information, see [Positional syntax for property definition](../../language-reference/builtin-types/record.md#positional-syntax-for-property-definition) and [Deconstructor behavior in derived records](../../language-reference/builtin-types/record.md#deconstructor-behavior-in-derived-records). ## See also - [Discards](discards.md) -- [Tuple types](language-reference/builtin-types/value-tuples.md) +- [Tuple types](../../language-reference/builtin-types/value-tuples.md) diff --git a/docs/csharp/discards.md b/docs/csharp/fundamentals/functional/discards.md similarity index 88% rename from docs/csharp/discards.md rename to docs/csharp/fundamentals/functional/discards.md index 2dfbc761df7c7..8ce1baa6c56f6 100644 --- a/docs/csharp/discards.md +++ b/docs/csharp/fundamentals/functional/discards.md @@ -16,7 +16,7 @@ You indicate that a variable is a discard by assigning it the underscore (`_`) a (_, _, area) = city.GetCityInformation(cityName); ``` -Beginning with C# 9.0, you can use discards to specify unused input parameters of a lambda expression. For more information, see the [Input parameters of a lambda expression](language-reference/operators/lambda-expressions.md#input-parameters-of-a-lambda-expression) section of the [Lambda expressions](language-reference/operators/lambda-expressions.md) article. +Beginning with C# 9.0, you can use discards to specify unused input parameters of a lambda expression. For more information, see the [Input parameters of a lambda expression](../../language-reference/operators/lambda-expressions.md#input-parameters-of-a-lambda-expression) section of the [Lambda expressions](../../language-reference/operators/lambda-expressions.md) article. When `_` is a valid discard, attempting to retrieve its value or use it in an assignment operation generates compiler error CS0301, "The name '\_' doesn't exist in the current context". This error is because `_` isn't assigned a value, and may not even be assigned a storage location. If it were an actual variable, you couldn't discard more than one value, as the previous example did. @@ -36,7 +36,7 @@ For more information on deconstructing user-defined types with discards, see [De ## Pattern matching with `switch` -The *discard pattern* can be used in pattern matching with the [switch expression](language-reference/operators/switch-expression.md). Every expression, including `null`, always matches the discard pattern. +The *discard pattern* can be used in pattern matching with the [switch expression](../../language-reference/operators/switch-expression.md). Every expression, including `null`, always matches the discard pattern. The following example defines a `ProvidesFormatInfo` method that uses a `switch` expression to determine whether an object provides an implementation and tests whether the object is `null`. It also uses the discard pattern to handle non-null objects of any other type. @@ -52,7 +52,7 @@ The following example calls the [DateTime.TryParse(String, out DateTime)]( when the argument is `null`. The code doesn't need the result of the assignment, so it's discarded. The expression forces a null check. The discard clarifies your intent: the result of the assignment isn't needed or used. +You can use a standalone discard to indicate any variable that you choose to ignore. One typical use is to use an assignment to ensure that an argument isn't null. The following code uses a discard to force an assignment. The right side of the assignment uses the [null coalescing operator](../../language-reference/operators/null-coalescing-operator.md) to throw an when the argument is `null`. The code doesn't need the result of the assignment, so it's discarded. The expression forces a null check. The discard clarifies your intent: the result of the assignment isn't needed or used. :::code language="csharp" source="snippets/discards/standalone-discard1.cs" ID="ArgNullCheck" ::: @@ -79,5 +79,5 @@ Without assigning the task to a discard, the following code generates a compiler ## See also - [Deconstructing tuples and other types](deconstruct.md) -- [`is` operator](language-reference/operators/is.md) -- [`switch` keyword](language-reference/keywords/switch.md) +- [`is` operator](../../language-reference/operators/is.md) +- [`switch` keyword](../../language-reference/keywords/switch.md) diff --git a/docs/csharp/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md similarity index 55% rename from docs/csharp/pattern-matching.md rename to docs/csharp/fundamentals/functional/pattern-matching.md index bec55ff697e8e..4d5b2a4498978 100644 --- a/docs/csharp/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -7,9 +7,9 @@ ms.technology: csharp-fundamentals # Pattern matching overview -*Pattern matching* is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The "[`is`](language-reference/operators/is.md) expression" now supports pattern matching to test an expression and conditionally declare a new variable to the result of that expression. The "[`switch`](language-reference/operators/switch-expression.md) expression" enables you to perform actions based on the first matching pattern for an expression. These two expressions support a rich vocabulary of [*patterns*](language-reference/operators/patterns.md). You have a rich vocabulary to express your algorithms. +*Pattern matching* is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The "[`is`](../../language-reference/operators/is.md) expression" now supports pattern matching to test an expression and conditionally declare a new variable to the result of that expression. The "[`switch`](../../language-reference/operators/switch-expression.md) expression" enables you to perform actions based on the first matching pattern for an expression. These two expressions support a rich vocabulary of [*patterns*](../../language-reference/operators/patterns.md). You have a rich vocabulary to express your algorithms. -This article provides an overview of scenarios where you can use pattern matching. These techniques can improve the readability and correctness of your code. For a full discussion of all the patterns you can apply, see the article on [patterns](language-reference/operators/patterns.md) in the language reference. +This article provides an overview of scenarios where you can use pattern matching. These techniques can improve the readability and correctness of your code. For a full discussion of all the patterns you can apply, see the article on [patterns](../../language-reference/operators/patterns.md) in the language reference. ## Null checks @@ -17,11 +17,11 @@ One of the most common scenarios for pattern matching is to ensure values aren't :::code language="csharp" source="snippets/patterns/Program.cs" ID="NullableCheck"::: -The preceding code is a [*declaration pattern*](language-reference/operators/patterns.md#declaration-and-type-patterns) to test the type of the variable, and assign it to a new variable. The language rules make this technique safer than many others. The variable `number` is only accessible and assigned in the true portion of the `if` clause. If you try to access it elsewhere, either in the `else` clause, or after the `if` block, the compiler issues an error. Secondly, because you're not using the `==` operator, this pattern works when a type overloads the `==` operator. That makes it an ideal way to check null reference values, adding the `not` pattern: +The preceding code is a [*declaration pattern*](../../language-reference/operators/patterns.md#declaration-and-type-patterns) to test the type of the variable, and assign it to a new variable. The language rules make this technique safer than many others. The variable `number` is only accessible and assigned in the true portion of the `if` clause. If you try to access it elsewhere, either in the `else` clause, or after the `if` block, the compiler issues an error. Secondly, because you're not using the `==` operator, this pattern works when a type overloads the `==` operator. That makes it an ideal way to check null reference values, adding the `not` pattern: :::code language="csharp" source="snippets/patterns/Program.cs" ID="NullReferenceCheck"::: -The preceding example used a [*constant pattern*](language-reference/operators/patterns.md#constant-pattern) to compare the variable to `null`. The `not` is a [*logical pattern*](language-reference/operators/patterns.md#logical-patterns) that matches when the negated pattern doesn't match. +The preceding example used a [*constant pattern*](../../language-reference/operators/patterns.md#constant-pattern) to compare the variable to `null`. The `not` is a [*logical pattern*](../../language-reference/operators/patterns.md#logical-patterns) that matches when the negated pattern doesn't match. ## Type tests @@ -37,7 +37,7 @@ You can also test a variable to find a match on specific values. The following c :::code language="csharp" source="snippets/patterns/Simulation.cs" ID="PerformOperation"::: -The previous example demonstrates a method dispatch based on the value of an enumeration. The final `_` case is a [*discard pattern*](language-reference/operators/patterns.md#discard-pattern) that matches all values. It handles any error conditions where the value doesn't match one of the defined `enum` values. If you omit that switch arm, the compiler warns that you haven't handled all possible input values. At run time, the `switch` expression throws an exception if the object being examined doesn't match any of the switch arms. You could use numeric constants instead of a set of enum values. You can also use this similar technique for constant string values that represent the commands: +The previous example demonstrates a method dispatch based on the value of an enumeration. The final `_` case is a [*discard pattern*](../../language-reference/operators/patterns.md#discard-pattern) that matches all values. It handles any error conditions where the value doesn't match one of the defined `enum` values. If you omit that switch arm, the compiler warns that you haven't handled all possible input values. At run time, the `switch` expression throws an exception if the object being examined doesn't match any of the switch arms. You could use numeric constants instead of a set of enum values. You can also use this similar technique for constant string values that represent the commands: :::code language="csharp" source="snippets/patterns/Simulation.cs" ID="PerformStringOperation"::: @@ -45,11 +45,11 @@ The preceding example shows the same algorithm, but uses string values instead o ## Relational patterns -You can use [*relational patterns*](language-reference/operators/patterns.md#relational-patterns) to test how a value compares to constants. For example, the following code returns the state of water based on the temperature in Fahrenheit: +You can use [*relational patterns*](../../language-reference/operators/patterns.md#relational-patterns) to test how a value compares to constants. For example, the following code returns the state of water based on the temperature in Fahrenheit: :::code language="csharp" source="snippets/patterns/Simulation.cs" ID="RelationalPattern"::: -The preceding code also demonstrates the conjunctive `and` [*logical pattern*](language-reference/operators/patterns.md#logical-patterns) to check that both relational patterns match. You can also use a disjunctive `or` pattern to check that either pattern matches. The two relational patterns are surrounded by parentheses, which you can use around any pattern for clarity. The final two switch arms handle the cases for the melting point and the boiling point. Without those two arms, the compiler warns you that your logic doesn't cover every possible input. +The preceding code also demonstrates the conjunctive `and` [*logical pattern*](../../language-reference/operators/patterns.md#logical-patterns) to check that both relational patterns match. You can also use a disjunctive `or` pattern to check that either pattern matches. The two relational patterns are surrounded by parentheses, which you can use around any pattern for clarity. The final two switch arms handle the cases for the melting point and the boiling point. Without those two arms, the compiler warns you that your logic doesn't cover every possible input. ## Multiple inputs @@ -57,7 +57,7 @@ All the patterns you've seen so far have been checking one input. You can write :::code language="csharp" source="snippets/patterns/OrderProcessor.cs" ID="OrderRecord"::: -The preceding positional record type declares two members at explicit positions. Appearing first is the `Items`, then the order's `Cost`. For more information, see [Records](language-reference/builtin-types/record.md). +The preceding positional record type declares two members at explicit positions. Appearing first is the `Items`, then the order's `Cost`. For more information, see [Records](../../language-reference/builtin-types/record.md). The following code examines the number of items and the value of an order to calculate a discounted price: @@ -67,12 +67,12 @@ The first two arms examine two properties of the `Order`. The third examines onl :::code language="csharp" source="snippets/patterns/OrderProcessor.cs" ID="DeconstructPattern"::: -The preceding code demonstrates the [*positional pattern*](language-reference/operators/patterns.md#positional-pattern) where the properties are deconstructed for the expression. +The preceding code demonstrates the [*positional pattern*](../../language-reference/operators/patterns.md#positional-pattern) where the properties are deconstructed for the expression. This article provided a tour of the kinds of code you can write with pattern matching in C#. The articles below show more examples of using patterns in scenarios, and the full vocabulary of patterns available to use. ## See also -- [Exploration: Use pattern matching to build your class behavior for better code](whats-new/tutorials/patterns-objects.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](tutorials/pattern-matching.md) -- [Reference: Pattern matching](language-reference/operators/patterns.md) +- [Exploration: Use pattern matching to build your class behavior for better code](../../whats-new/tutorials/patterns-objects.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](./tutorials/pattern-matching.md) +- [Reference: Pattern matching](../../language-reference/operators/patterns.md) diff --git a/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/Program.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/Program.cs new file mode 100644 index 0000000000000..58ed59cdb9e2e --- /dev/null +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/Program.cs @@ -0,0 +1,18 @@ +using System; + +namespace deconstruction +{ + class Program + { + static void Main(string[] args) + { + Example.Main(); + Example2.Main(); + Example3.Main(); + Example4.Main(); + ExampleDiscard.Main(); + ExampleClassDeconstruction.Main(); + ExampleExtension.Main(); + } + } +} diff --git a/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/class-discard1.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/class-discard1.cs new file mode 100644 index 0000000000000..58747a8e78c0c --- /dev/null +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/class-discard1.cs @@ -0,0 +1,64 @@ +using System; + +namespace ClassDiscard +{ + public class Person + { + public string FirstName { get; set; } + public string MiddleName { get; set; } + public string LastName { get; set; } + public string City { get; set; } + public string State { get; set; } + + public Person(string fname, string mname, string lname, + string cityName, string stateName) + { + FirstName = fname; + MiddleName = mname; + LastName = lname; + City = cityName; + State = stateName; + } + + // Return the first and last name. + public void Deconstruct(out string fname, out string lname) + { + fname = FirstName; + lname = LastName; + } + + public void Deconstruct(out string fname, out string mname, out string lname) + { + fname = FirstName; + mname = MiddleName; + lname = LastName; + } + + public void Deconstruct(out string fname, out string lname, + out string city, out string state) + { + fname = FirstName; + lname = LastName; + city = City; + state = State; + } + } + + public class Example + { + public static void Main() + { + var p = new Person("John", "Quincy", "Adams", "Boston", "MA"); + + // + // Deconstruct the person object. + var (fName, _, city, _) = p; + Console.WriteLine($"Hello {fName} of {city}!"); + // The example displays the following output: + // Hello John of Boston! + // + } + } + // The example displays the following output: + // Hello John Adams of Boston, MA! +} diff --git a/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-class1.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-class1.cs new file mode 100644 index 0000000000000..79a53bc483620 --- /dev/null +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-class1.cs @@ -0,0 +1,35 @@ +namespace Deconstruction +{ + public class Person + { + public string FirstName { get; set; } + public string MiddleName { get; set; } + public string LastName { get; set; } + + public Person(string fname, string mname, string lname) + { + FirstName = fname; + MiddleName = mname; + LastName = lname; + } + // + public void Deconstruct(out string fname, out string mname, out string lname) + // + { + fname = FirstName; + mname = MiddleName; + lname = LastName; + } + } + + public class Example + { + public static void Main() + { + var p = new Person("John", "Quincy", "Adams"); + // + var (fName, mName, lName) = p; + // + } + } +} diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class2.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-class2.cs similarity index 97% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class2.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-class2.cs index 4bd4c8ceb1ccd..54c7f9bd08602 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class2.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-class2.cs @@ -42,7 +42,7 @@ public void Deconstruct(out string fname, out string lname, } } -public class Example +public class ExampleClassDeconstruction { public static void Main() { diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-extension1.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-extension1.cs similarity index 99% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-extension1.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-extension1.cs index 51a74a6468612..d5b9db924ede5 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-extension1.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-extension1.cs @@ -86,7 +86,7 @@ public static void Deconstruct(this PropertyInfo p, out bool hasGetAndSet, } } -public class Example +public class ExampleExtension { public static void Main() { diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple1.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple1.cs similarity index 91% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple1.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple1.cs index 21ef04ee45b13..977c1da53e018 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple1.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple1.cs @@ -1,6 +1,4 @@ -using System; - -public class Example +public class Example { public static void Main() { diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple2.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple2.cs similarity index 84% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple2.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple2.cs index e2af479bd2fd3..93a78b35494c6 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple2.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple2.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; - -public class Example +public class Example2 { // public static void Main() diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple3.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple3.cs similarity index 84% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple3.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple3.cs index c876289d89386..4230aaa10e2fe 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple3.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple3.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; - -public class Example +public class Example3 { // public static void Main() diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple4.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple4.cs similarity index 84% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple4.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple4.cs index 50268755083c5..7384ded039242 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple4.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple4.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; - -public class Example +public class Example4 { // public static void Main() diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple5.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple5.cs similarity index 92% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple5.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple5.cs index 8f4e67607a570..231f3c4fbac31 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-tuple5.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruct-tuple5.cs @@ -1,6 +1,4 @@ -using System; - -public class Example +public class Example5 { // public static void Main() diff --git a/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj new file mode 100644 index 0000000000000..5b064a3fa2f37 --- /dev/null +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/deconstruction.csproj @@ -0,0 +1,10 @@ + + + + Exe + net5.0 + enable + deconstruction.Program + + + diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/discard-tuple1.cs b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/discard-tuple1.cs similarity index 94% rename from samples/snippets/csharp/programming-guide/deconstructing-tuples/discard-tuple1.cs rename to docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/discard-tuple1.cs index 7b33847b67027..52d322d7d7854 100644 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/discard-tuple1.cs +++ b/docs/csharp/fundamentals/functional/snippets/deconstructing-tuples/discard-tuple1.cs @@ -1,7 +1,6 @@ using System; -using System.Collections.Generic; -public class Example +public class ExampleDiscard { public static void Main() { diff --git a/docs/csharp/snippets/discards/Program.cs b/docs/csharp/fundamentals/functional/snippets/discards/Program.cs similarity index 100% rename from docs/csharp/snippets/discards/Program.cs rename to docs/csharp/fundamentals/functional/snippets/discards/Program.cs diff --git a/docs/csharp/snippets/discards/discard-class.cs b/docs/csharp/fundamentals/functional/snippets/discards/discard-class.cs similarity index 100% rename from docs/csharp/snippets/discards/discard-class.cs rename to docs/csharp/fundamentals/functional/snippets/discards/discard-class.cs diff --git a/docs/csharp/snippets/discards/discard-out1.cs b/docs/csharp/fundamentals/functional/snippets/discards/discard-out1.cs similarity index 100% rename from docs/csharp/snippets/discards/discard-out1.cs rename to docs/csharp/fundamentals/functional/snippets/discards/discard-out1.cs diff --git a/docs/csharp/snippets/discards/discard-pattern2.cs b/docs/csharp/fundamentals/functional/snippets/discards/discard-pattern2.cs similarity index 100% rename from docs/csharp/snippets/discards/discard-pattern2.cs rename to docs/csharp/fundamentals/functional/snippets/discards/discard-pattern2.cs diff --git a/docs/csharp/snippets/discards/discard-tuple.cs b/docs/csharp/fundamentals/functional/snippets/discards/discard-tuple.cs similarity index 100% rename from docs/csharp/snippets/discards/discard-tuple.cs rename to docs/csharp/fundamentals/functional/snippets/discards/discard-tuple.cs diff --git a/docs/csharp/snippets/discards/discards.csproj b/docs/csharp/fundamentals/functional/snippets/discards/discards.csproj similarity index 100% rename from docs/csharp/snippets/discards/discards.csproj rename to docs/csharp/fundamentals/functional/snippets/discards/discards.csproj diff --git a/docs/csharp/snippets/discards/standalone-discard1.cs b/docs/csharp/fundamentals/functional/snippets/discards/standalone-discard1.cs similarity index 100% rename from docs/csharp/snippets/discards/standalone-discard1.cs rename to docs/csharp/fundamentals/functional/snippets/discards/standalone-discard1.cs diff --git a/docs/csharp/snippets/discards/standalone-discard2.cs b/docs/csharp/fundamentals/functional/snippets/discards/standalone-discard2.cs similarity index 100% rename from docs/csharp/snippets/discards/standalone-discard2.cs rename to docs/csharp/fundamentals/functional/snippets/discards/standalone-discard2.cs diff --git a/docs/csharp/snippets/patterns/OrderProcessor.cs b/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs similarity index 100% rename from docs/csharp/snippets/patterns/OrderProcessor.cs rename to docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs diff --git a/docs/csharp/snippets/patterns/Program.cs b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs similarity index 100% rename from docs/csharp/snippets/patterns/Program.cs rename to docs/csharp/fundamentals/functional/snippets/patterns/Program.cs diff --git a/docs/csharp/snippets/patterns/Simulation.cs b/docs/csharp/fundamentals/functional/snippets/patterns/Simulation.cs similarity index 100% rename from docs/csharp/snippets/patterns/Simulation.cs rename to docs/csharp/fundamentals/functional/snippets/patterns/Simulation.cs diff --git a/docs/csharp/snippets/patterns/patterns.csproj b/docs/csharp/fundamentals/functional/snippets/patterns/patterns.csproj similarity index 100% rename from docs/csharp/snippets/patterns/patterns.csproj rename to docs/csharp/fundamentals/functional/snippets/patterns/patterns.csproj diff --git a/docs/csharp/tutorials/pattern-matching.md b/docs/csharp/fundamentals/functional/tutorials/pattern-matching.md similarity index 85% rename from docs/csharp/tutorials/pattern-matching.md rename to docs/csharp/fundamentals/functional/tutorials/pattern-matching.md index 39d084e75b2d7..bd46d50f135ba 100644 --- a/docs/csharp/tutorials/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/tutorials/pattern-matching.md @@ -35,7 +35,7 @@ Consider a major metropolitan area that is using tolls and peak time pricing to From that brief description, you may have quickly sketched out an object hierarchy to model this system. However, your data is coming from multiple sources like other vehicle registration management systems. These systems provide different classes to model that data and you don't have a single object model you can use. In this tutorial, you'll use these simplified classes to model for the vehicle data from these external systems, as shown in the following code: -[!code-csharp[ExternalSystems](~/samples/snippets/csharp/tutorials/patterns/start/toll-calculator/ExternalSystems.cs)] +[!code-csharp[ExternalSystems](./snippets/patterns/start/toll-calculator/ExternalSystems.cs)] You can download the starter code from the [dotnet/samples](https://github.com/dotnet/samples/tree/main/csharp/tutorials/patterns/start) GitHub repository. You can see that the vehicle classes are from different systems, and are in different namespaces. No common base class, other than `System.Object` can be leveraged. @@ -83,7 +83,7 @@ namespace toll_calculator } ``` -The preceding code uses a [`switch` expression](../language-reference/operators/switch-expression.md) (not the same as a [`switch`](../language-reference/keywords/switch.md) statement) that tests the [declaration pattern](../language-reference/operators/patterns.md#declaration-and-type-patterns). A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The `{ }` case must follow the cases for each vehicle type. If the order were reversed, the `{ }` case would take precedence. Finally, the `null` [constant pattern](../language-reference/operators/patterns.md#constant-pattern) detects when `null` is passed to this method. The `null` pattern can be last because the other patterns match only a non-null object of the correct type. +The preceding code uses a [`switch` expression](../../../language-reference/operators/switch-expression.md) (not the same as a [`switch`](../../../language-reference/keywords/switch.md) statement) that tests the [declaration pattern](../../../language-reference/operators/patterns.md#declaration-and-type-patterns). A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The `{ }` case must follow the cases for each vehicle type. If the order were reversed, the `{ }` case would take precedence. Finally, the `null` [constant pattern](../../../language-reference/operators/patterns.md#constant-pattern) detects when `null` is passed to this method. The `null` pattern can be last because the other patterns match only a non-null object of the correct type. You can test this code using the following code in `Program.cs`: @@ -146,7 +146,7 @@ The toll authority wants to encourage vehicles to travel at maximum capacity. Th - Buses that are less than 50% full pay an extra $2.00. - Buses that are more than 90% full get a $1.00 discount. -These rules can be implemented using a [property pattern](../language-reference/operators/patterns.md#property-pattern) in the same switch expression. A property pattern compares a property value to a constant value. The property pattern examines properties of the object once the type has been determined. The single case for a `Car` expands to four different cases: +These rules can be implemented using a [property pattern](../../../language-reference/operators/patterns.md#property-pattern) in the same switch expression. A property pattern compares a property value to a constant value. The property pattern examines properties of the object once the type has been determined. The single case for a `Car` expands to four different cases: ```csharp vehicle switch @@ -282,7 +282,7 @@ In the preceding sample, using a recursive expression means you don't repeat the For the final feature, the toll authority wants to add time sensitive peak pricing. During the morning and evening rush hours, the tolls are doubled. That rule only affects traffic in one direction: inbound to the city in the morning, and outbound in the evening rush hour. During other times during the workday, tolls increase by 50%. Late night and early morning, tolls are reduced by 25%. During the weekend, it's the normal rate, regardless of the time. You could use a series if `if` and `else` statements to express this using the following code: -[!code-csharp[FullTuplePattern](~/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs#SnippetPremiumWithoutPattern)] +[!code-csharp[FullTuplePattern](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#SnippetPremiumWithoutPattern)] The preceding code does work correctly, but isn't readable. You have to chain through all the input cases and the nested `if` statements to reason about the code. Instead, you'll use pattern matching for this feature, but you'll integrate it with other techniques. You could build a single pattern match expression that would account for all the combinations of direction, day of the week, and time. The result would be a complicated expression. It would be hard to read and difficult to understand. That makes it hard to ensure correctness. Instead, combine those methods to build a tuple of values that concisely describes all those states. Then use pattern matching to calculate a multiplier for the toll. The tuple contains three discrete conditions: @@ -331,17 +331,17 @@ private static bool IsWeekDay(DateTime timeOfToll) => That method is correct, but it's repetitious. You can simplify it, as shown in the following code: -[!code-csharp[IsWeekDay](~/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs#IsWeekDay)] +[!code-csharp[IsWeekDay](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#IsWeekDay)] Next, add a similar function to categorize the time into the blocks: -[!code-csharp[GetTimeBand](~/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs#GetTimeBand)] +[!code-csharp[GetTimeBand](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#GetTimeBand)] -You add a private `enum` to convert each range of time to a discrete value. Then, the `GetTimeBand` method uses [relational patterns](../language-reference/operators/patterns.md#relational-patterns), and [conjunctive `or` patterns](../language-reference/operators/patterns.md#logical-patterns), both added in C# 9.0. A relational pattern lets you test a numeric value using `<`, `>`, `<=`, or `>=`. The `or` pattern tests if an expression matches one or more patterns. You can also use an `and` pattern to ensure that an expression matches two distinct patterns, and a `not` pattern to test that an expression doesn't match a pattern. +You add a private `enum` to convert each range of time to a discrete value. Then, the `GetTimeBand` method uses [relational patterns](../../../language-reference/operators/patterns.md#relational-patterns), and [conjunctive `or` patterns](../../../language-reference/operators/patterns.md#logical-patterns), both added in C# 9.0. A relational pattern lets you test a numeric value using `<`, `>`, `<=`, or `>=`. The `or` pattern tests if an expression matches one or more patterns. You can also use an `and` pattern to ensure that an expression matches two distinct patterns, and a `not` pattern to test that an expression doesn't match a pattern. After you create those methods, you can use another `switch` expression with the **tuple pattern** to calculate the pricing premium. You could build a `switch` expression with all 16 arms: -[!code-csharp[FullTuplePattern](~/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs#TuplePatternOne)] +[!code-csharp[FullTuplePattern](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#TuplePatternOne)] The above code works, but it can be simplified. All eight combinations for the weekend have the same toll. You can replace all eight with the following line: @@ -374,7 +374,7 @@ public decimal PeakTimePremium(DateTime timeOfToll, bool inbound) => Finally, you can remove the two rush hour times that pay the regular price. Once you remove those arms, you can replace the `false` with a discard (`_`) in the final switch arm. You'll have the following finished method: -[!code-csharp[SimplifiedTuplePattern](../../../samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs#FinalTuplePattern)] +[!code-csharp[SimplifiedTuplePattern](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#FinalTuplePattern)] This example highlights one of the advantages of pattern matching: the pattern branches are evaluated in order. If you rearrange them so that an earlier branch handles one of your later cases, the compiler warns you about the unreachable code. Those language rules made it easier to do the preceding simplifications with confidence that the code didn't change. @@ -386,5 +386,5 @@ You can download the finished code from the [dotnet/samples](https://github.com/ ## See also -- [Patterns](../language-reference/operators/patterns.md) -- [`switch` expression](../language-reference/operators/switch-expression.md) +- [Patterns](../../../language-reference/operators/patterns.md) +- [`switch` expression](../../../language-reference/operators/switch-expression.md) diff --git a/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems.cs b/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/ExternalSystems.cs similarity index 100% rename from samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems.cs rename to docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/ExternalSystems.cs diff --git a/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/Program.cs b/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/Program.cs similarity index 100% rename from samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/Program.cs rename to docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/Program.cs diff --git a/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs b/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/TollCalculator.cs similarity index 100% rename from samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs rename to docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/TollCalculator.cs diff --git a/samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/toll-calculator.csproj b/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/toll-calculator.csproj similarity index 100% rename from samples/snippets/csharp/tutorials/patterns/finished/toll-calculator/toll-calculator.csproj rename to docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/toll-calculator.csproj diff --git a/samples/snippets/csharp/tutorials/patterns/start/toll-calculator/ExternalSystems.cs b/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/ExternalSystems.cs similarity index 100% rename from samples/snippets/csharp/tutorials/patterns/start/toll-calculator/ExternalSystems.cs rename to docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/ExternalSystems.cs diff --git a/samples/snippets/csharp/tutorials/patterns/start/toll-calculator/Program.cs b/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/Program.cs similarity index 100% rename from samples/snippets/csharp/tutorials/patterns/start/toll-calculator/Program.cs rename to docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/Program.cs diff --git a/samples/snippets/csharp/tutorials/patterns/start/toll-calculator/toll-calculator.csproj b/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/toll-calculator.csproj similarity index 100% rename from samples/snippets/csharp/tutorials/patterns/start/toll-calculator/toll-calculator.csproj rename to docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/toll-calculator.csproj diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 91c56bff29472..2f16b738e0f8f 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -57,12 +57,13 @@ items: href: fundamentals/types/generics.md - name: Anonymous Types href: fundamentals/types/anonymous-types.md + # TODO: Delegates, lambdas and events - name: Tutorials - items: # TODO: DTOs (tuples, anonymous types and records) # TODO: Values (record structs and structs) # TODO: Classes and objects (interfaces and classes) # TODO: Generics (methods and classes) + # TODO: Fundamentals of using lambdas - name: Program structure items: - name: Overview @@ -113,15 +114,15 @@ items: # - Lambdas # - Positional records as DTOs - name: Pattern Matching - href: pattern-matching.md + href: fundamentals/functional/pattern-matching.md - name: Discards - href: discards.md + href: fundamentals/functional/discards.md - name: Deconstructing tuples and other types - href: deconstruct.md + href: fundamentals/functional/deconstruct.md - name: Tutorials items: - name: Build data-driven algorithms with pattern matching - href: tutorials/pattern-matching.md + href: fundamentals/functional/tutorials/pattern-matching.md # separating data and algorithms # Data transformations - name: Exceptions and errors diff --git a/docs/csharp/whats-new/csharp-7.md b/docs/csharp/whats-new/csharp-7.md index cc0a394b3f806..d07e5e2dbe499 100644 --- a/docs/csharp/whats-new/csharp-7.md +++ b/docs/csharp/whats-new/csharp-7.md @@ -105,7 +105,31 @@ Discards are supported in the following scenarios: The following example defines a `QueryCityDataForYears` method that returns a 6-tuple that contains data for a city for two different years. The method call in the example is concerned only with the two population values returned by the method and so treats the remaining values in the tuple as discards when it deconstructs the tuple. -[!code-csharp[Tuple-discard](~/samples/snippets/csharp/programming-guide/deconstructing-tuples/discard-tuple1.cs)] +```csharp +using System; + +public class Example +{ + public static void Main() + { + var result = QueryCityData("New York City"); + + var city = result.Item1; + var pop = result.Item2; + var size = result.Item3; + + // Do something with the data. + } + + private static (string, int, double) QueryCityData(string name) + { + if (name == "New York City") + return (name, 8175133, 468.48); + + return ("", 0, 0); + } +} +``` For more information, see [Discards](../discards.md). diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/class-discard1.cs b/samples/snippets/csharp/programming-guide/deconstructing-tuples/class-discard1.cs deleted file mode 100644 index 6bcc780f26ee4..0000000000000 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/class-discard1.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; - -public class Person -{ - public string FirstName { get; set; } - public string MiddleName { get; set; } - public string LastName { get; set; } - public string City { get; set; } - public string State { get; set; } - - public Person(string fname, string mname, string lname, - string cityName, string stateName) - { - FirstName = fname; - MiddleName = mname; - LastName = lname; - City = cityName; - State = stateName; - } - - // Return the first and last name. - public void Deconstruct(out string fname, out string lname) - { - fname = FirstName; - lname = LastName; - } - - public void Deconstruct(out string fname, out string mname, out string lname) - { - fname = FirstName; - mname = MiddleName; - lname = LastName; - } - - public void Deconstruct(out string fname, out string lname, - out string city, out string state) - { - fname = FirstName; - lname = LastName; - city = City; - state = State; - } -} - -public class Example -{ - public static void Main() - { - var p = new Person("John", "Quincy", "Adams", "Boston", "MA"); - - // - // Deconstruct the person object. - var (fName, _, city, _) = p; - Console.WriteLine($"Hello {fName} of {city}!"); - // The example displays the following output: - // Hello John of Boston! - // - } -} -// The example displays the following output: -// Hello John Adams of Boston, MA! diff --git a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class1.cs b/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class1.cs deleted file mode 100644 index 93a22983d62aa..0000000000000 --- a/samples/snippets/csharp/programming-guide/deconstructing-tuples/deconstruct-class1.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; - -public class Person -{ - public string FirstName { get; set; } - public string MiddleName { get; set; } - public string LastName { get; set; } - - public Person(string fname, string mname, string lname) - { - FirstName = fname; - MiddleName = mname; - LastName = lname; - } - // - public void Deconstruct(out string fname, out string mname, out string lname) - // - { - fname = FirstName; - mname = MiddleName; - lname = LastName; - } -} - -public class Example -{ - public static void Main() - { - var p = new Person("John", "Quincy", "Adams"); - // - var (fName, mName, lName) = p; - // - } -} From edad1b1d25b525bf113bc304745fd56fd7f81de2 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 May 2021 15:36:48 -0400 Subject: [PATCH 15/60] Fix a bad snippet ID conversion. --- docs/csharp/fundamentals/functional/deconstruct.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index d699c1025657d..8f67586527922 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -61,11 +61,11 @@ The following example illustrates the use of tuples with discards. The `QueryCit C# does not offer built-in support for deconstructing non-tuple types other than the [`record`](#deconstructing-a-record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: -:::code language="csharp" source="./snippets/deconstructing-tuples/discard-tuple2.cs" ID="Snippet1"::: +:::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple2.cs" ID="Snippet1"::: You can then deconstruct an instance of the `Person` class named `p` with an assignment like the following: -:::code language="csharp" source="./snippets/deconstructing-tuples/discard-tuple2.cs" ID="Snippet2"::: +:::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple3.cs" ID="Snippet1"::: The following example overloads the `Deconstruct` method to return various combinations of properties of a `Person` object. Individual overloads return: From 0136fe51bdf55fc1b2f68b037e7c0f7e1258b20d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 May 2021 15:39:49 -0400 Subject: [PATCH 16/60] Wrong snippet. --- docs/csharp/fundamentals/functional/deconstruct.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index 8f67586527922..42adb8efcd307 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -55,7 +55,7 @@ Often when deconstructing a tuple, you're interested in the values of only some The following example illustrates the use of tuples with discards. The `QueryCityDataForYears` method returns a 6-tuple with the name of a city, its area, a year, the city's population for that year, a second year, and the city's population for that second year. The example shows the change in population between those two years. Of the data available from the tuple, we're unconcerned with the city area, and we know the city name and the two dates at design-time. As a result, we're only interested in the two population values stored in the tuple, and can handle its remaining values as discards. -:::code language="csharp" source="./snippets/deconstructing-tuples/discard-tuple1.cs"::: +:::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple1.cs"::: ## Deconstructing user-defined types From 0c41de1b16687e1864aec4acb8f36b11a9fc5560 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 May 2021 16:00:02 -0400 Subject: [PATCH 17/60] Move fundamental exception content --- .openpublishing.redirection.json | 19 +++++++++++++++++++ .../compiler-generated-exceptions.md | 0 .../creating-and-throwing-exceptions.md | 0 .../exceptions/exception-handling.md | 0 .../exceptions/index.md | 0 .../exceptions/using-exceptions.md | 0 docs/csharp/toc.yml | 15 ++++++++------- 7 files changed, 27 insertions(+), 7 deletions(-) rename docs/csharp/{programming-guide => fundamentals}/exceptions/compiler-generated-exceptions.md (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/creating-and-throwing-exceptions.md (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/exception-handling.md (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/index.md (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/using-exceptions.md (100%) diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index f8b8cddad30df..c35b50b664edb 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -2817,9 +2817,28 @@ "source_path": "docs/csharp/programming-guide/events/how-to-use-a-dictionary-to-store-event-instances.md", "redirect_url": "/dotnet/standard/events/how-to-handle-multiple-events-using-event-properties" }, + { + "source_path": "docs/csharp/programming-guide/exceptions/compiler-generated-exceptions.md", + "redirect_url": "/dotnet/csharp/fundamentals/exceptions/compiler-generated-exceptions" + }, + { + "source_path": "docs/csharp/programming-guide/exceptions/creating-and-throwing-exceptions.md", + "redirect_url": "/dotnet/csharp/fundamentals/exceptions/creating-and-throwing-exceptions" + }, + { + "source_path": "docs/csharp/programming-guide/exceptions/exception-handling.md", + "redirect_url": "/dotnet/csharp/fundamentals/exceptions/exception-handling" + }, + { + "source_path": "docs/csharp/programming-guide/exceptions/index.md", + "redirect_url": "/dotnet/csharp/fundamentals/exceptions" + }, { "source_path": "docs/csharp/programming-guide/exceptions/exceptions-and-exception-handling.md", "redirect_url": "/dotnet/csharp/programming-guide/exceptions/index" + }, { + "source_path": "docs/csharp/programming-guide/exceptions/using-exceptions.md", + "redirect_url": "/dotnet/csharp/fundamentals/exceptions/using-exceptions" }, { "source_path": "docs/csharp/programming-guide/inside-a-program/general-structure-of-a-csharp-program.md", diff --git a/docs/csharp/programming-guide/exceptions/compiler-generated-exceptions.md b/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/compiler-generated-exceptions.md rename to docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md diff --git a/docs/csharp/programming-guide/exceptions/creating-and-throwing-exceptions.md b/docs/csharp/fundamentals/exceptions/creating-and-throwing-exceptions.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/creating-and-throwing-exceptions.md rename to docs/csharp/fundamentals/exceptions/creating-and-throwing-exceptions.md diff --git a/docs/csharp/programming-guide/exceptions/exception-handling.md b/docs/csharp/fundamentals/exceptions/exception-handling.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/exception-handling.md rename to docs/csharp/fundamentals/exceptions/exception-handling.md diff --git a/docs/csharp/programming-guide/exceptions/index.md b/docs/csharp/fundamentals/exceptions/index.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/index.md rename to docs/csharp/fundamentals/exceptions/index.md diff --git a/docs/csharp/programming-guide/exceptions/using-exceptions.md b/docs/csharp/fundamentals/exceptions/using-exceptions.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/using-exceptions.md rename to docs/csharp/fundamentals/exceptions/using-exceptions.md diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 2f16b738e0f8f..f061b361d4a37 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -129,23 +129,22 @@ items: items: - name: Overview displayName: exceptions - href: programming-guide/exceptions/index.md + href: fundamentals/exceptions/index.md - name: Using Exceptions - href: programming-guide/exceptions/using-exceptions.md + href: fundamentals/exceptions/using-exceptions.md - name: Exception Handling - href: programming-guide/exceptions/exception-handling.md + href: fundamentals/exceptions/exception-handling.md - name: Creating and Throwing Exceptions - href: programming-guide/exceptions/creating-and-throwing-exceptions.md + href: fundamentals/exceptions/creating-and-throwing-exceptions.md - name: Compiler-Generated Exceptions - href: programming-guide/exceptions/compiler-generated-exceptions.md + href: fundamentals/exceptions/compiler-generated-exceptions.md + ## TODO: Replace with a tutorial (include async exceptions) - name: How to items: - name: "How to handle an exception using try-catch" href: programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md - name: "How to execute cleanup code using finally" href: programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md - - name: "How to catch a non-CLS exception" - href: programming-guide/exceptions/how-to-catch-a-non-cls-exception.md - name: Coding style items: - name: Identifier names @@ -328,6 +327,8 @@ items: href: how-to/compare-strings.md - name: Safely cast using pattern matching and is/as operators href: how-to/safely-cast-using-pattern-matching-is-and-as-operators.md + - name: "How to catch a non-CLS exception" + href: programming-guide/exceptions/how-to-catch-a-non-cls-exception.md - name: The .NET Compiler Platform SDK (Roslyn APIs) items: - name: The .NET Compiler Platform SDK (Roslyn APIs) overview From 871f7e6f145199752973f88745d8ac9f95470c5b Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 May 2021 16:14:15 -0400 Subject: [PATCH 18/60] more moving exceptions --- .openpublishing.redirection.json | 12 ++++++++++++ .../how-to-execute-cleanup-code-using-finally.md | 0 .../how-to-handle-an-exception-using-try-catch.md | 0 .../exceptions/snippets/exceptions/CatchOrder.cs | 0 .../snippets/exceptions/ExampleTryCatch.cs | 0 .../snippets/exceptions/ExceptionFilter.cs | 0 .../exceptions/snippets/exceptions/ExceptionTest.cs | 0 .../exceptions/InvalidDepartmentException.cs | 0 .../exceptions/snippets/exceptions/Program.cs | 0 .../exceptions/snippets/exceptions/ProgramLog.cs | 0 .../exceptions/snippets/exceptions/exceptions.csproj | 0 .../how-to-catch-a-non-cls-exception.md | 0 docs/csharp/toc.yml | 4 ++-- 13 files changed, 14 insertions(+), 2 deletions(-) rename docs/csharp/{programming-guide => fundamentals}/exceptions/how-to-execute-cleanup-code-using-finally.md (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/how-to-handle-an-exception-using-try-catch.md (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/CatchOrder.cs (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/ExampleTryCatch.cs (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/ExceptionFilter.cs (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/ExceptionTest.cs (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/InvalidDepartmentException.cs (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/Program.cs (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/ProgramLog.cs (100%) rename docs/csharp/{programming-guide => fundamentals}/exceptions/snippets/exceptions/exceptions.csproj (100%) rename docs/csharp/{programming-guide/exceptions => how-to}/how-to-catch-a-non-cls-exception.md (100%) diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index c35b50b664edb..82d39429ecd4b 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -2829,6 +2829,18 @@ "source_path": "docs/csharp/programming-guide/exceptions/exception-handling.md", "redirect_url": "/dotnet/csharp/fundamentals/exceptions/exception-handling" }, + { + "source_path": "docs/csharp/programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md", + "redirect_url": "/dotnet/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch" + }, + { + "source_path": "docs/csharp/programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md", + "redirect_url": "/dotnet/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally" + }, + { + "source_path": "docs/csharp/programming-guide/exceptions/how-to-catch-a-non-cls-exception.md", + "redirect_url": "/dotnet/csharp/how-to/how-to-catch-a-non-cls-exception" + }, { "source_path": "docs/csharp/programming-guide/exceptions/index.md", "redirect_url": "/dotnet/csharp/fundamentals/exceptions" diff --git a/docs/csharp/programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md b/docs/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md rename to docs/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md diff --git a/docs/csharp/programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md b/docs/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md rename to docs/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/CatchOrder.cs b/docs/csharp/fundamentals/exceptions/snippets/exceptions/CatchOrder.cs similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/CatchOrder.cs rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/CatchOrder.cs diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/ExampleTryCatch.cs b/docs/csharp/fundamentals/exceptions/snippets/exceptions/ExampleTryCatch.cs similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/ExampleTryCatch.cs rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/ExampleTryCatch.cs diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/ExceptionFilter.cs b/docs/csharp/fundamentals/exceptions/snippets/exceptions/ExceptionFilter.cs similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/ExceptionFilter.cs rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/ExceptionFilter.cs diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/ExceptionTest.cs b/docs/csharp/fundamentals/exceptions/snippets/exceptions/ExceptionTest.cs similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/ExceptionTest.cs rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/ExceptionTest.cs diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/InvalidDepartmentException.cs b/docs/csharp/fundamentals/exceptions/snippets/exceptions/InvalidDepartmentException.cs similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/InvalidDepartmentException.cs rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/InvalidDepartmentException.cs diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/Program.cs b/docs/csharp/fundamentals/exceptions/snippets/exceptions/Program.cs similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/Program.cs rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/Program.cs diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/ProgramLog.cs b/docs/csharp/fundamentals/exceptions/snippets/exceptions/ProgramLog.cs similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/ProgramLog.cs rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/ProgramLog.cs diff --git a/docs/csharp/programming-guide/exceptions/snippets/exceptions/exceptions.csproj b/docs/csharp/fundamentals/exceptions/snippets/exceptions/exceptions.csproj similarity index 100% rename from docs/csharp/programming-guide/exceptions/snippets/exceptions/exceptions.csproj rename to docs/csharp/fundamentals/exceptions/snippets/exceptions/exceptions.csproj diff --git a/docs/csharp/programming-guide/exceptions/how-to-catch-a-non-cls-exception.md b/docs/csharp/how-to/how-to-catch-a-non-cls-exception.md similarity index 100% rename from docs/csharp/programming-guide/exceptions/how-to-catch-a-non-cls-exception.md rename to docs/csharp/how-to/how-to-catch-a-non-cls-exception.md diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index f061b361d4a37..febdbde9cf86b 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -142,9 +142,9 @@ items: - name: How to items: - name: "How to handle an exception using try-catch" - href: programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md + href: fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md - name: "How to execute cleanup code using finally" - href: programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md + href: fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md - name: Coding style items: - name: Identifier names From 84f4a2591b7180ee11d6b7771823f408dec6be5a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 May 2021 16:30:52 -0400 Subject: [PATCH 19/60] move coding style articles --- .openpublishing.redirection.json | 8 ++++++++ .../coding-style}/coding-conventions.md | 0 .../coding-style}/identifier-names.md | 0 docs/csharp/toc.yml | 6 +++--- 4 files changed, 11 insertions(+), 3 deletions(-) rename docs/csharp/{programming-guide/inside-a-program => fundamentals/coding-style}/coding-conventions.md (100%) rename docs/csharp/{programming-guide/inside-a-program => fundamentals/coding-style}/identifier-names.md (100%) diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 82d39429ecd4b..0c2f0b1d9d009 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -2877,10 +2877,18 @@ "source_path": "docs/csharp/programming-guide/generics/introduction-to-generics.md", "redirect_url": "/dotnet/standard/generics/index" }, + { + "source_path": "docs/csharp/programming-guide/inside-a-program/coding-conventions.md", + "redirect_url": "/dotnet/csharp/fundamentals/coding-style/coding-conventions" + }, { "source_path": "docs/csharp/programming-guide/inside-a-program/hello-world-your-first-program.md", "redirect_url": "/dotnet/csharp/tutorials/intro-to-csharp/hello-world" }, + { + "source_path": "docs/csharp/programming-guide/inside-a-program/identifier-names.md", + "redirect_url": "/dotnet/csharp/fundamentals/coding-style/identifier-names" + }, { "source_path": "docs/csharp/programming-guide/inside-a-program/index.md", "redirect_url": "/dotnet/csharp/fundamentals/program-structure" diff --git a/docs/csharp/programming-guide/inside-a-program/coding-conventions.md b/docs/csharp/fundamentals/coding-style/coding-conventions.md similarity index 100% rename from docs/csharp/programming-guide/inside-a-program/coding-conventions.md rename to docs/csharp/fundamentals/coding-style/coding-conventions.md diff --git a/docs/csharp/programming-guide/inside-a-program/identifier-names.md b/docs/csharp/fundamentals/coding-style/identifier-names.md similarity index 100% rename from docs/csharp/programming-guide/inside-a-program/identifier-names.md rename to docs/csharp/fundamentals/coding-style/identifier-names.md diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index febdbde9cf86b..505a3f2272c5b 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -148,9 +148,9 @@ items: - name: Coding style items: - name: Identifier names - href: programming-guide/inside-a-program/identifier-names.md + href: fundamentals/coding-style/identifier-names.md - name: C# Coding Conventions - href: programming-guide/inside-a-program/coding-conventions.md + href: fundamentals/coding-style/coding-conventions.md - name: What's new in C# items: - name: C# 9.0 @@ -328,7 +328,7 @@ items: - name: Safely cast using pattern matching and is/as operators href: how-to/safely-cast-using-pattern-matching-is-and-as-operators.md - name: "How to catch a non-CLS exception" - href: programming-guide/exceptions/how-to-catch-a-non-cls-exception.md + href: how-to/how-to-catch-a-non-cls-exception.md - name: The .NET Compiler Platform SDK (Roslyn APIs) items: - name: The .NET Compiler Platform SDK (Roslyn APIs) overview From 8c77bcf26db74c0276e61dd55ceafec40b6cb9e9 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 May 2021 16:36:43 -0400 Subject: [PATCH 20/60] fix warnings. --- .../fundamentals/coding-style/coding-conventions.md | 4 ++-- .../fundamentals/coding-style/identifier-names.md | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/docs/csharp/fundamentals/coding-style/coding-conventions.md b/docs/csharp/fundamentals/coding-style/coding-conventions.md index 1405d8907c9b1..7d2e18b8db2ce 100644 --- a/docs/csharp/fundamentals/coding-style/coding-conventions.md +++ b/docs/csharp/fundamentals/coding-style/coding-conventions.md @@ -186,7 +186,7 @@ The following sections describe practices that the C# team follows to prepare co ### Implicitly typed local variables -- Use [implicit typing](../classes-and-structs/implicitly-typed-local-variables.md) for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important. +- Use [implicit typing](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md) for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important. :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet8"::: @@ -198,7 +198,7 @@ The following sections describe practices that the C# team follows to prepare co :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet10"::: -- Avoid the use of `var` in place of [dynamic](../../language-reference/builtin-types/reference-types.md). Use `dynamic` when you want run-time type inference. For more information, see [Using type dynamic (C# Programming Guide)](../types/using-type-dynamic.md). +- Avoid the use of `var` in place of [dynamic](../../language-reference/builtin-types/reference-types.md). Use `dynamic` when you want run-time type inference. For more information, see [Using type dynamic (C# Programming Guide)](../../programming-guide/types/using-type-dynamic.md). - Use implicit typing to determine the type of the loop variable in [for](../../language-reference/keywords/for.md) loops. diff --git a/docs/csharp/fundamentals/coding-style/identifier-names.md b/docs/csharp/fundamentals/coding-style/identifier-names.md index 2081c0addbb7b..4bc6277015362 100644 --- a/docs/csharp/fundamentals/coding-style/identifier-names.md +++ b/docs/csharp/fundamentals/coding-style/identifier-names.md @@ -28,11 +28,10 @@ In addition to the rules, there are a number of identifier [naming conventions]( ## See also -- [C# Programming Guide](../index.md) -- [Inside a C# Program](./index.md) +- [C# Programming Guide](../../programming-guide/index.md) - [C# Reference](../../language-reference/index.md) -- [Classes](../classes-and-structs/classes.md) +- [Classes](../types/classes.md) - [Structure types](../../language-reference/builtin-types/struct.md) -- [Namespaces](../namespaces/index.md) -- [Interfaces](../interfaces/index.md) -- [Delegates](../delegates/index.md) +- [Namespaces](../types/namespaces/index.md) +- [Interfaces](../types/interfaces/index.md) +- [Delegates](../../programming-guide/delegates/index.md) From 08140b547cd8d926c4111abc0fee378888031073 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 11 May 2021 16:39:48 -0400 Subject: [PATCH 21/60] build warnings --- docs/csharp/fundamentals/coding-style/identifier-names.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/fundamentals/coding-style/identifier-names.md b/docs/csharp/fundamentals/coding-style/identifier-names.md index 4bc6277015362..5199537f111d3 100644 --- a/docs/csharp/fundamentals/coding-style/identifier-names.md +++ b/docs/csharp/fundamentals/coding-style/identifier-names.md @@ -32,6 +32,6 @@ In addition to the rules, there are a number of identifier [naming conventions]( - [C# Reference](../../language-reference/index.md) - [Classes](../types/classes.md) - [Structure types](../../language-reference/builtin-types/struct.md) -- [Namespaces](../types/namespaces/index.md) -- [Interfaces](../types/interfaces/index.md) +- [Namespaces](../types/namespaces.md) +- [Interfaces](../types/interfaces.md) - [Delegates](../../programming-guide/delegates/index.md) From 0b6fe092238b5e502c65c8bc229860e4bd8ed468 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 May 2021 17:45:16 -0400 Subject: [PATCH 22/60] edit pass --- docs/csharp/fundamentals/types/index.md | 46 +++++++++----------- docs/csharp/fundamentals/types/namespaces.md | 9 ++-- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index d34186a594372..6f24d39a8eddb 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -1,7 +1,7 @@ --- -title: "Types - C# Programming Guide" -description: Learn about types in C# programming, such as built-in types, custom types, value types, and reference types. -ms.date: 11/19/2020 +title: "The C# type system" +description: Learn about creating types in C#, such as tuples, records, value types, and reference types. +ms.date: 05/14/2021 helpviewer_keywords: - "value types [C#]" - "reference types [C#]" @@ -11,11 +11,8 @@ helpviewer_keywords: - "data types [C#]" - "C# language, types" - "strong typing [C#]" -ms.assetid: f782d7cc-035e-4500-b1b1-36a9881130ad --- -# Types (C# Programming Guide) - -## Types, variables, and values +# The C# type system C# is a strongly typed language. Every variable and constant has a type, as does every expression that evaluates to a value. Every method declaration specifies a name, number of parameters, and type and kind (value, reference, or output) for each input parameter and for the return value. The .NET class library defines a set of built-in numeric types and more complex types that represent a wide variety of logical constructs, such as the file system, network connections, collections and arrays of objects, and dates. A typical C# program uses types from the class library and user-defined types that model the concepts that are specific to the program's problem domain. @@ -26,29 +23,28 @@ The information stored in a type can include the following items: - The members (methods, fields, events, and so on) that it contains. - The base type it inherits from. - The interface(s) it implements. -- The location where the memory for variables will be allocated at run time. - The kinds of operations that are permitted. -The compiler uses type information to make sure all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [int](../../language-reference/builtin-types/integral-numeric-types.md), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [bool](../../language-reference/builtin-types/bool.md), the compiler generates an error, as shown in the following example: +The compiler uses type information to make sure all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [`int`](../../language-reference/builtin-types/integral-numeric-types.md), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [`bool`](../../language-reference/builtin-types/bool.md), the compiler generates an error, as shown in the following example: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="TypeSafeExample"::: > [!NOTE] -> C and C++ developers, notice that in C#, [bool](../../language-reference/builtin-types/bool.md) is not convertible to [int](../../language-reference/builtin-types/integral-numeric-types.md). +> C and C++ developers, notice that in C#, `bool` is not convertible to `int`. The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory. ### Specifying types in variable declarations -When you declare a variable or constant in a program, you must either specify its type or use the [var](../../language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: +When you declare a variable or constant in a program, you must either specify its type or use the [`var`](../../language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="Declarations"::: -The types of method parameters and return values are specified in the method declaration. The following signature shows a method that requires an [int](../../language-reference/builtin-types/integral-numeric-types.md) as an input argument and returns a string: +The types of method parameters and return values are specified in the method declaration. The following signature shows a method that requires an `int` as an input argument and returns a string: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="GetName"::: -After you declare a variable, you can't redeclare it with a new type, and you can't assign a value not compatible with its declared type. For example, you can't declare an [int](../../language-reference/builtin-types/integral-numeric-types.md) and then assign it a Boolean value of `true`. However, values can be converted to other types, for example when they're assigned to new variables or passed as method arguments. A *type conversion* that doesn't cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code. +After you declare a variable, you can't redeclare it with a new type, and you can't assign a value not compatible with its declared type. For example, you can't declare an `int` and then assign it a Boolean value of `true`. However, values can be converted to other types, for example when they're assigned to new variables or passed as method arguments. A *type conversion* that doesn't cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code. For more information, see [Casting and Type Conversions](../../programming-guide/types/casting-and-type-conversions.md). @@ -58,14 +54,14 @@ C# provides a standard set of built-in types to represent integers, floating poi ## Custom types -You use the [struct](../../language-reference/builtin-types/struct.md), [class](../../language-reference/keywords/class.md), [interface](../../language-reference/keywords/interface.md), [enum](../../language-reference/builtin-types/enum.md), and [record](../../language-reference/builtin-types/record.md) constructs to create your own custom types. The .NET class library itself is a collection of custom types provided by Microsoft that you can use in your own applications. By default, the most frequently used types in the class library are available in any C# program. Others become available only when you explicitly add a project reference to the assembly in which they're defined. After the compiler has a reference to the assembly, you can declare variables (and constants) of the types declared in that assembly in source code. For more information, see [.NET Class Library](../../../standard/class-library-overview.md). +You use the [`struct`](../../language-reference/builtin-types/struct.md), [`class`](../../language-reference/keywords/class.md), [`interface`](../../language-reference/keywords/interface.md), [`enum`](../../language-reference/builtin-types/enum.md), and [`record`](../../language-reference/builtin-types/record.md) constructs to create your own custom types. The .NET class library itself is a collection of custom types that you can use in your own applications. By default, the most frequently used types in the class library are available in any C# program. Others become available only when you explicitly add a project reference to the assembly in which they're defined. After the compiler has a reference to the assembly, you can declare variables (and constants) of the types declared in that assembly in source code. For more information, see [.NET Class Library](../../../standard/class-library-overview.md). ## The common type system It's important to understand two fundamental points about the type system in .NET: -- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: [int](../../language-reference/builtin-types/integral-numeric-types.md)), derive ultimately from a single base type, which is (C# keyword: [object](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). -- Each type in the CTS is defined as either a *value type* or a *reference type*. These types include all custom types in the .NET class library and also your own user-defined types. Types that you define by using the [struct](../../language-reference/builtin-types/struct.md) keyword are value types; all the built-in numeric types are `structs`. Types that you define by using the [class](../../language-reference/keywords/class.md) or [record](../../language-reference/builtin-types/record.md) keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior. +- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: [`object`](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). +- Each type in the CTS is defined as either a *value type* or a *reference type*. These types include all custom types in the .NET class library and also your own user-defined types. Types that you define by using the `struct` keyword are value types; all the built-in numeric types are `structs`. Types that you define by using the `class` or `record` keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior. The following illustration shows the relationship between value types and reference types in the CTS. @@ -78,7 +74,7 @@ The following illustration shows the relationship between value types and refere Value types derive from , which derives from . Types that derive from have special behavior in the CLR. Value type variables directly contain their values, which means that the memory is allocated inline in whatever context the variable is declared. There's no separate heap allocation or garbage collection overhead for value-type variables. -There are two categories of value types: [struct](../../language-reference/builtin-types/struct.md) and [enum](../../language-reference/builtin-types/enum.md). +There are two categories of value types: `struct` and `enum`. The built-in numeric types are structs, and they have fields and methods that you can access: @@ -96,17 +92,17 @@ You use the [struct](../../language-reference/builtin-types/struct.md) keyword t For more information about structs, see [Structure types](../../language-reference/builtin-types/struct.md). For more information about value types, see [Value types](../../language-reference/builtin-types/value-types.md). -The other category of value types is [enum](../../language-reference/builtin-types/enum.md). An enum defines a set of named integral constants. For example, the enumeration in the .NET class library contains a set of named constant integers that specify how a file should be opened. It's defined as shown in the following example: +The other category of value types is `enum`. An enum defines a set of named integral constants. For example, the enumeration in the .NET class library contains a set of named constant integers that specify how a file should be opened. It's defined as shown in the following example: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="EnumFileMode"::: -The `System.IO.FileMode.Create` constant has a value of 2. However, the name is much more meaningful for humans reading the source code, and for that reason it's better to use enumerations instead of constant literal numbers. For more information, see . +The constant has a value of 2. However, the name is much more meaningful for humans reading the source code, and for that reason it's better to use enumerations instead of constant literal numbers. For more information, see . All enums inherit from , which inherits from . All the rules that apply to structs also apply to enums. For more information about enums, see [Enumeration types](../../language-reference/builtin-types/enum.md). ### Reference types -A type that is defined as a [class](../../language-reference/keywords/class.md), [record](../../language-reference/builtin-types/record.md), [delegate](../../language-reference/builtin-types/reference-types.md), array, or [interface](../../language-reference/keywords/interface.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 object by using the [new](../../language-reference/operators/new-operator.md) operator, or assign it an object that has been created elsewhere by using `new`, as shown in the following example: +A type that is defined as a `class`, `record`, [`delegate`](../../language-reference/builtin-types/reference-types.md), array, or [`interface`](../../language-reference/keywords/interface.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 object by using the [`new`](../../language-reference/operators/new-operator.md) operator, or assign it an object that has been created elsewhere by using `new`, as shown in the following example: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="DeclarationAndAssignment"::: @@ -124,7 +120,7 @@ Reference types fully support inheritance. When you create a class, you can inhe ## Types of literal values -In C#, literal values receive a type from the compiler. You can specify how a numeric literal should be typed by appending a letter to the end of the number. For example, to specify that the value 4.56 should be treated as a float, append an "f" or "F" after the number: `4.56f`. If no letter is appended, the compiler will infer a type for the literal. For more information about which types can be specified with letter suffixes, see [Integral numeric types](../../language-reference/builtin-types/integral-numeric-types.md) and [Floating-point numeric types](../../language-reference/builtin-types/floating-point-numeric-types.md). +In C#, literal values receive a type from the compiler. You can specify how a numeric literal should be typed by appending a letter to the end of the number. For example, to specify that the value `4.56` should be treated as a `float`, append an "f" or "F" after the number: `4.56f`. If no letter is appended, the compiler will infer a type for the literal. For more information about which types can be specified with letter suffixes, see [Integral numeric types](../../language-reference/builtin-types/integral-numeric-types.md) and [Floating-point numeric types](../../language-reference/builtin-types/floating-point-numeric-types.md). Because literals are typed, and all types derive ultimately from , you can write and compile code such as the following code: @@ -132,7 +128,7 @@ Because literals are typed, and all types derive ultimately from has one type parameter that by convention is given the name *T*. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, string: +A type can be declared with one or more *type parameters* that serve as a placeholder for the actual type (the *concrete type*) that client code will provide when it creates an instance of the type. Such types are called *generic types*. For example, the .NET type has one type parameter that by convention is given the name `T`. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, `string`: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="GenericType"::: @@ -140,11 +136,11 @@ The use of the type parameter makes it possible to reuse the same class to hold ## Implicit types, anonymous types, and nullable value types -As stated previously, you can implicitly type a local variable (but not class members) by using the [var](../../language-reference/keywords/var.md) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). +You can implicitly type a local variable (but not class members) by using the [`var`](../../language-reference/keywords/var.md) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). It can be inconvenient to create a named type for simple sets of related values that you don't intend to store or pass outside method boundaries. You can create *anonymous types* for this purpose. For more information, see [Anonymous Types](anonymous-types.md). -Ordinary value types can't have a value of [null](../../language-reference/keywords/null.md). However, you can create nullable value types by appending a `?` after the type. For example, `int?` is an `int` type that can also have the value [null](../../language-reference/keywords/null.md). Nullable value types are instances of the generic struct type . Nullable value types are especially useful when you're passing data to and from databases in which numeric values might be null. For more information, see [Nullable value types](../../language-reference/builtin-types/nullable-value-types.md). +Ordinary value types can't have a value of [`null`](../../language-reference/keywords/null.md). However, you can create *nullable value types* by appending a `?` after the type. For example, `int?` is an `int` type that can also have the value [`null`](../../language-reference/keywords/null.md). Nullable value types are instances of the generic struct type . Nullable value types are especially useful when you're passing data to and from databases in which numeric values might be `null`. For more information, see [Nullable value types](../../language-reference/builtin-types/nullable-value-types.md). ## Compile-time type and runtime type @@ -164,9 +160,9 @@ If the two types are different for a variable, it's important to understand when For more information, see the following articles: +- [Builtin types](../../language-reference/builtin-types/built-in-types.md) - [Value Types](../../language-reference/builtin-types/value-types.md) - [Reference Types](../../language-reference/keywords/reference-types.md) -TODO: More language reference links ## C# language specification diff --git a/docs/csharp/fundamentals/types/namespaces.md b/docs/csharp/fundamentals/types/namespaces.md index a6a65790bf7df..c6c6335974d08 100644 --- a/docs/csharp/fundamentals/types/namespaces.md +++ b/docs/csharp/fundamentals/types/namespaces.md @@ -1,13 +1,12 @@ --- -title: "Namespaces - C# Programming Guide" -description: Learn about namespaces in C# programming. See an overview of namespace properties and view additional resources. -ms.date: 08/21/2018 +title: "Organizing types in namespaces" +description: Learn how namespaces help you organize related types. +ms.date: 05/14/2021 helpviewer_keywords: - "C# language, namespaces" - "namespaces [C#]" -ms.assetid: b1c4ab46-3fad-4ffa-9deb-dd50a2d8c65a --- -# Namespaces (C# Programming Guide) +# Declare namespaces Namespaces are heavily used in C# programming in two ways. First, .NET uses namespaces to organize its many classes, as follows: From 1e637f778b7d92e5af79a97633a2d29adaefc6ce Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 12 May 2021 20:54:21 -0400 Subject: [PATCH 23/60] proofread. --- docs/csharp/fundamentals/types/index.md | 28 ++++++++++---------- docs/csharp/fundamentals/types/namespaces.md | 6 ++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 6f24d39a8eddb..3431611996bef 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -27,7 +27,7 @@ The information stored in a type can include the following items: The compiler uses type information to make sure all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [`int`](../../language-reference/builtin-types/integral-numeric-types.md), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [`bool`](../../language-reference/builtin-types/bool.md), the compiler generates an error, as shown in the following example: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="TypeSafeExample"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="TypeSafeExample"::: > [!NOTE] > C and C++ developers, notice that in C#, `bool` is not convertible to `int`. @@ -38,11 +38,11 @@ The compiler embeds the type information into the executable file as metadata. T When you declare a variable or constant in a program, you must either specify its type or use the [`var`](../../language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="Declarations"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="Declarations"::: The types of method parameters and return values are specified in the method declaration. The following signature shows a method that requires an `int` as an input argument and returns a string: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="GetName"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="GetName"::: After you declare a variable, you can't redeclare it with a new type, and you can't assign a value not compatible with its declared type. For example, you can't declare an `int` and then assign it a Boolean value of `true`. However, values can be converted to other types, for example when they're assigned to new variables or passed as method arguments. A *type conversion* that doesn't cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code. @@ -78,23 +78,23 @@ There are two categories of value types: `struct` and `enum`. The built-in numeric types are structs, and they have fields and methods that you can access: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="ConstantByte"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="ConstantByte"::: But you declare and assign values to them as if they're simple non-aggregate types: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="NonAggregateTypes"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="NonAggregateTypes"::: Value types are *sealed*, which means that you can't derive a type from any value type, for example . You can't define a struct to inherit from any user-defined class or struct because a struct can only inherit from . However, a struct can implement one or more interfaces. You can cast a struct type to any interface type that it implements; this cast causes a *boxing* operation to wrap the struct inside a reference type object on the managed heap. Boxing operations occur when you pass a value type to a method that takes a or any interface type as an input parameter. For more information, see [Boxing and Unboxing](../../programming-guide/types/boxing-and-unboxing.md). You use the [struct](../../language-reference/builtin-types/struct.md) keyword to create your own custom value types. Typically, a struct is used as a container for a small set of related variables, as shown in the following example: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="Coords"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="Coords"::: For more information about structs, see [Structure types](../../language-reference/builtin-types/struct.md). For more information about value types, see [Value types](../../language-reference/builtin-types/value-types.md). The other category of value types is `enum`. An enum defines a set of named integral constants. For example, the enumeration in the .NET class library contains a set of named constant integers that specify how a file should be opened. It's defined as shown in the following example: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="EnumFileMode"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="EnumFileMode"::: The constant has a value of 2. However, the name is much more meaningful for humans reading the source code, and for that reason it's better to use enumerations instead of constant literal numbers. For more information, see . @@ -104,17 +104,17 @@ All enums inherit from , which in A type that is defined as a `class`, `record`, [`delegate`](../../language-reference/builtin-types/reference-types.md), array, or [`interface`](../../language-reference/keywords/interface.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 object by using the [`new`](../../language-reference/operators/new-operator.md) operator, or assign it an object that has been created elsewhere by using `new`, as shown in the following example: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="DeclarationAndAssignment"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="DeclarationAndAssignment"::: An interface must be initialized together with a class object that implements it. If `MyClass` implements `IMyInterface`, you create an instance of `IMyInterface` as shown in the following example: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="InterfaceDeclaration"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="InterfaceDeclaration"::: 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're allocated and when they're 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 doesn't create a performance issue. For more information about garbage collection, see [Automatic Memory Management](../../../standard/automatic-memory-management.md). All arrays are reference types, even if their elements are value types. Arrays implicitly derive from the class, but you declare and use them with the simplified syntax that is provided by C#, as shown in the following example: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="ArrayDeclaration"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="ArrayDeclaration"::: Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md). For more information about inheritance and virtual methods, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). @@ -124,13 +124,13 @@ In C#, literal values receive a type from the compiler. You can specify how a nu Because literals are typed, and all types derive ultimately from , you can write and compile code such as the following code: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="ConvertTypes"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="ConvertTypes"::: ## Generic types A type can be declared with one or more *type parameters* that serve as a placeholder for the actual type (the *concrete type*) that client code will provide when it creates an instance of the type. Such types are called *generic types*. For example, the .NET type has one type parameter that by convention is given the name `T`. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, `string`: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="GenericType"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="GenericType"::: The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to [object](../../language-reference/builtin-types/reference-types.md). Generic collection classes are called *strongly typed collections* because the compiler knows the specific type of the collection's elements and can raise an error at compile time if, for example, you try to add an integer to the `stringList` object in the previous example. For more information, see [Generics](generics.md). @@ -146,11 +146,11 @@ Ordinary value types can't have a value of [`null`](../../language-reference/key A variable can have different compile-time and run-time types. The *compile-time type* is the declared or inferred type of the variable in the source code. The *run-time type* is the type of the instance referred to by that variable. Often those two types are the same, as in the following example: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="CompileTimeType"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="CompileTimeType"::: In other cases, the compile-time type is different, as shown in the following two examples: -:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" id="RuntimeTypes"::: +:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="RuntimeTypes"::: In both of the preceding examples, the run-time type is a `string`. The compile-time type is `object` in the first line, and `IEnumerable` in the second. diff --git a/docs/csharp/fundamentals/types/namespaces.md b/docs/csharp/fundamentals/types/namespaces.md index c6c6335974d08..ccfc7ad36584c 100644 --- a/docs/csharp/fundamentals/types/namespaces.md +++ b/docs/csharp/fundamentals/types/namespaces.md @@ -6,13 +6,13 @@ helpviewer_keywords: - "C# language, namespaces" - "namespaces [C#]" --- -# Declare namespaces +# Declare namespaces to organize types in packages Namespaces are heavily used in C# programming in two ways. First, .NET uses namespaces to organize its many classes, as follows: [!code-csharp[csProgGuide#22](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/progGuide.cs#22)] - is a namespace and is a class in that namespace. The `using` keyword can be used so that the complete name is not required, as in the following example: + is a namespace and is a class in that namespace. The `using` keyword can be used so that the complete name isn't required, as in the following example: [!code-csharp[csProgGuide#1](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/using.cs#1)] @@ -31,7 +31,7 @@ The name of the namespace must be a valid C# [identifier name](../../programming Namespaces have the following properties: - They organize large code projects. -- They are delimited by using the `.` operator. +- They're delimited by using the `.` operator. - The `using` directive obviates the requirement to specify the name of the namespace for every class. - The `global` namespace is the "root" namespace: `global::System` will always refer to the .NET namespace. From 08aac278a43fd6de921a3ee56880b310dce5fb23 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 15:53:00 -0400 Subject: [PATCH 24/60] continue editing types section --- .openpublishing.redirection.json | 2 +- docs/csharp/fundamentals/types/basic-types.md | 82 ------------------- docs/csharp/fundamentals/types/classes.md | 43 +++++----- docs/csharp/fundamentals/types/records.md | 30 +++---- .../types/snippets/classes/Program.cs | 0 .../types/snippets/classes/classes.csproj | 8 ++ docs/csharp/toc.yml | 2 - .../concepts/basic-types/generic-type.cs | 1 - .../concepts/basic-types/method-signature.cs | 8 -- .../concepts/basic-types/type-safety.cs | 7 -- .../basic-types/variable-declaration.cs | 12 --- 11 files changed, 45 insertions(+), 150 deletions(-) delete mode 100644 docs/csharp/fundamentals/types/basic-types.md rename samples/snippets/csharp/programming-guide/classes-and-structs/class-example.cs => docs/csharp/fundamentals/types/snippets/classes/Program.cs (100%) create mode 100644 docs/csharp/fundamentals/types/snippets/classes/classes.csproj delete mode 100644 samples/snippets/csharp/concepts/basic-types/generic-type.cs delete mode 100644 samples/snippets/csharp/concepts/basic-types/method-signature.cs delete mode 100644 samples/snippets/csharp/concepts/basic-types/type-safety.cs delete mode 100644 samples/snippets/csharp/concepts/basic-types/variable-declaration.cs diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 0c2f0b1d9d009..21593d4aab5d8 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -877,7 +877,7 @@ }, { "source_path": "docs/csharp/basic-types.md", - "redirect_url": "/dotnet/csharp/fundamentals/types/basic-types" + "redirect_url": "/dotnet/csharp/fundamentals/types" }, { "source_path": "docs/csharp/classes.md", diff --git a/docs/csharp/fundamentals/types/basic-types.md b/docs/csharp/fundamentals/types/basic-types.md deleted file mode 100644 index 605240bacd0b1..0000000000000 --- a/docs/csharp/fundamentals/types/basic-types.md +++ /dev/null @@ -1,82 +0,0 @@ ---- -title: Basic Types - C# Guide -description: Learn about the core types (numerics, strings, and object) in all C# programs -ms.date: 10/10/2016 -ms.technology: csharp-fundamentals -ms.assetid: 95c686ba-ae4f-440e-8e94-0dbd6e04d11f ---- - -# Types, variables, and values - -C# is a strongly typed language. Every variable and constant has a type, as does every expression that evaluates to a value. Every method signature specifies a type for each input parameter and for the return value. The .NET class library defines a set of built-in numeric types as well as more complex types that represent a wide variety of logical constructs, such as the file system, network connections, collections and arrays of objects, and dates. A typical C# program uses types from the class library as well as user-defined types that model the concepts that are specific to the program's problem domain. - -The information stored in a type can include the following: - -- The storage space that a variable of the type requires. - -- The maximum and minimum values that it can represent. - -- The members (methods, fields, events, and so on) that it contains. - -- The base type it inherits from. - -- The interface(s) it implements. - -- The location where the memory for variables will be allocated at run time. - -- The kinds of operations that are permitted. - -The compiler uses type information to make sure that all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [int](../../language-reference/builtin-types/integral-numeric-types.md), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [bool](../../language-reference/builtin-types/bool.md), the compiler generates an error, as shown in the following example: - -[!code-csharp[Type Safety](../../../../samples/snippets/csharp/concepts/basic-types/type-safety.cs)] - -> [!NOTE] -> C and C++ developers, notice that in C#, [bool](../../language-reference/builtin-types/bool.md) is not convertible to [int](../../language-reference/builtin-types/integral-numeric-types.md). - -The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory. - -## Specifying types in variable declarations - -When you declare a variable or constant in a program, you must either specify its type or use the [var](../../language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: - -[!code-csharp[Variable Declaration](../../../../samples/snippets/csharp/concepts/basic-types/variable-declaration.cs)] - -The types of method parameters and return values are specified in the method signature. The following signature shows a method that requires an [int](../../language-reference/builtin-types/integral-numeric-types.md) as an input argument and returns a string: - -[!code-csharp[Method Signature](../../../../samples/snippets/csharp/concepts/basic-types/method-signature.cs)] - -After a variable is declared, it cannot be re-declared with a new type, and it cannot be assigned a value that is not compatible with its declared type. For example, you cannot declare an [int](../../language-reference/builtin-types/integral-numeric-types.md) and then assign it a Boolean value of `true`. However, values can be converted to other types, for example when they are assigned to new variables or passed as method arguments. A *type conversion* that does not cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code. - -For more information, see [Casting and type conversions](../../programming-guide/types/casting-and-type-conversions.md). - -## Built-in types - -C# provides a standard set of built-in numeric types to represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. There are also built-in **string** and **object** types. These are available for you to use in any C# program. For the complete list of the built-in types, see [Built-in types](../../language-reference/builtin-types/built-in-types.md). - -## Custom types - -You use the [struct](../../language-reference/builtin-types/struct.md), [class](../../language-reference/keywords/class.md), [record](../../language-reference/builtin-types/record.md), [interface](../../language-reference/keywords/interface.md), and [enum](../../language-reference/builtin-types/enum.md) constructs to create your own custom types. The .NET class library itself is a collection of custom types provided by Microsoft that you can use in your own applications. By default, the most frequently used types in the class library are available in any C# program. Others become available only when you explicitly add a project reference to the assembly in which they are defined. After the compiler has a reference to the assembly, you can declare variables (and constants) of the types declared in that assembly in source code. - -## Generic types - -A type can be declared with one or more *type parameters* that serve as a placeholder for the actual type (the *concrete type*) that client code will provide when it creates an instance of the type. Such types are called *generic types*. For example, has one type parameter that by convention is given the name *T*. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, string: - -[!code-csharp[Generic types](../../../../samples/snippets/csharp/concepts/basic-types/generic-type.cs)] - -The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to [object](../../language-reference/builtin-types/reference-types.md#the-object-type). Generic collection classes are called *strongly typed collections* because the compiler knows the specific type of the collection's elements and can raise an error at compile-time if, for example, you try to add an integer to the `strings` object in the previous example. For more information, see [Generics](generics.md). - -## Implicit types, anonymous types, and tuple types - -As stated previously, you can implicitly type a local variable (but not class members) by using the [var](../../language-reference/keywords/var.md) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly typed local variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). - -In some cases, it is inconvenient to create a named type for simple sets of related values that you do not intend to store or pass outside method boundaries. You can create *anonymous types* for this purpose. For more information, see [Anonymous types](anonymous-types.md). - -It's common to want to return more than one value from a method. You can create *tuple types* that return multiple values in a single method call. For more information, see [Tuple types](../../language-reference/builtin-types/value-tuples.md). - -## The Common type system - -It is important to understand two fundamental points about the type system in .NET: - -- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: `object`). This unified type hierarchy is called the [Common type system](../../../standard/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). - -- Each type in the CTS is defined as either a *value type* or a *reference type*. This includes all custom types in the .NET class library and also your own user-defined types. Types that you define by using the `struct` or `enum` keyword are value types. For more information about value types, see [Value types](../../language-reference/builtin-types/value-types.md). Types that you define by using the [class](../../language-reference/keywords/class.md) keyword are reference types. For more information about reference types, see [Classes](classes.md). Reference types and value types have different compile-time rules, and different run-time behavior. diff --git a/docs/csharp/fundamentals/types/classes.md b/docs/csharp/fundamentals/types/classes.md index edd0e894ce972..fdd2ebdf7d6c8 100644 --- a/docs/csharp/fundamentals/types/classes.md +++ b/docs/csharp/fundamentals/types/classes.md @@ -1,17 +1,16 @@ --- -title: "Classes - C# Programming Guide" +title: "Classes" description: Learn about the class types and how to create them -ms.date: 08/21/2018 +ms.date: 05/14/2021 helpviewer_keywords: - "classes [C#]" - "C# language, classes" -ms.assetid: e8848524-7273-429f-8aba-c658d5eff5ad --- -# Classes (C# Programming Guide) +# Classes ## 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 class by using the [new](../../language-reference/operators/new-operator.md) operator, or assign it an object of a compatible type that may have been created elsewhere, as shown in the following example: +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 class by using the [`new`](../../language-reference/operators/new-operator.md) operator, or assign it an object of a compatible type that may have been created elsewhere, as shown in the following example: ```csharp //Declaring an object of type MyClass. @@ -25,7 +24,7 @@ When the object is created, enough memory is allocated on the managed heap for t ## Declaring Classes - Classes are declared by using the [class](../../language-reference/keywords/class.md) keyword followed by a unique identifier, as shown in the following example: + Classes are declared by using the `class` keyword followed by a unique identifier, as shown in the following example: ```csharp //[access modifier] - [class] - [identifier] @@ -35,13 +34,13 @@ When the object is created, enough memory is allocated on the managed heap for t } ``` - The `class` keyword is preceded by the access level. Because [public](../../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 name of the class must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). 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*. + The `class` keyword is preceded by the access level. Because [`public`](../../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 name of the class must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). 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](../../language-reference/operators/new-operator.md) keyword followed by the name of the class that the object will be based on, like this: + 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: ```csharp Customer object1 = new Customer(); @@ -64,9 +63,9 @@ Although they are sometimes used interchangeably, a class and an object are diff ## Class inheritance -Classes fully support *inheritance*, a fundamental characteristic of object-oriented programming. When you create a class, you can inherit from any other class that is not defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override class virtual methods. Furthermore, you can implement one or more interfaces. +Classes fully support *inheritance*, a fundamental characteristic of object-oriented programming. When you create a class, you can inherit from any other class that is not defined as [`sealed`](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override class virtual methods. Furthermore, you can implement one or more interfaces. -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: +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 @@ -77,19 +76,19 @@ Inheritance is accomplished by using a *derivation*, which means a class is decl ``` When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](../../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 one or more interfaces. For more information, see [Interfaces](interfaces.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 Classes and Methods](../../programming-guide/classes-and-structs/partial-classes-and-methods.md). - + +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 one or more interfaces. For more information, see [Interfaces](interfaces.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 Classes and Methods](../../programming-guide/classes-and-structs/partial-classes-and-methods.md). + ## Example -The following example defines a public class that contains an [auto-implemented property](../../programming-guide/classes-and-structs/auto-implemented-properties.md), a method, and a special method called a constructor. For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md), [Methods](../../programming-guide/classes-and-structs/methods.md), and [Constructors](../../programming-guide/classes-and-structs/constructors.md) topics. The instances of the class are then instantiated with the `new` keyword. - -[!code-csharp[Class Example](~/samples/snippets/csharp/programming-guide/classes-and-structs/class-example.cs)] - +The following example defines a public class that contains an [auto-implemented property](../../programming-guide/classes-and-structs/auto-implemented-properties.md), a method, and a special method called a constructor. For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md), [Methods](../../programming-guide/classes-and-structs/methods.md), and [Constructors](../../programming-guide/classes-and-structs/constructors.md) topics. The instances of the class are then instantiated with the `new` keyword. + +:::code language="csharp" interactive="try-dotnet" source="snippets/classes/Program.cs"::: + ## C# Language Specification -[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] diff --git a/docs/csharp/fundamentals/types/records.md b/docs/csharp/fundamentals/types/records.md index e8ac4012d0463..4c05e5a283aee 100644 --- a/docs/csharp/fundamentals/types/records.md +++ b/docs/csharp/fundamentals/types/records.md @@ -1,27 +1,21 @@ --- -title: "Records - C# Programming Guide" -description: Learn about the record types and how to create them -ms.date: 02/26/2021 +title: "Records" +description: Learn about the record types and how to create them. A record is a class that provides value semantics. +ms.date: 05/14/2021 helpviewer_keywords: - "records [C#]" - "C# language, records" --- -# Records (C# Programming Guide) +# Records -A [record](../../language-reference/builtin-types/record.md) is a [class](../../language-reference/keywords/class.md) that provides special syntax and behavior for working with data models. For information about classes, see [Classes (C# Programming Guide)](classes.md). +A [record](../../language-reference/builtin-types/record.md) is a [class](../../language-reference/keywords/class.md) that provides special syntax and behavior for working with data models. For information about classes, see [Classes](classes.md). ## When to use records Consider using a record in place of a class in the following scenarios: -* You want to define a reference type for which objects are immutable. * You want to define a data model that depends on [value equality](../../programming-guide/statements-expressions-operators/equality-comparisons.md#value-equality). - -### Immutability - -An immutable type is one that prevents you from changing any property or field values of an object after it's instantiated. Immutability can be useful when you need a type to be thread-safe or you're depending on a hash code remaining the same in a hash table. Records provide concise syntax for creating and working with immutable types. - -Immutability isn't appropriate for all data scenarios. [Entity Framework Core](/ef/core/), for example, doesn't support updating with immutable entity types. +* You want to define a reference type for which objects are immutable. ### Value equality @@ -29,6 +23,12 @@ For records, value equality means that two variables of a record type are equal Not all data models work well with value equality. For example, [Entity Framework Core](/ef/core/) depends on reference equality to ensure that it uses only one instance of an entity type for what is conceptually one entity. For this reason, record types aren't appropriate for use as entity types in Entity Framework Core. +### Immutability + +An immutable type is one that prevents you from changing any property or field values of an object after it's instantiated. Immutability can be useful when you need a type to be thread-safe or you're depending on a hash code remaining the same in a hash table. Records provide concise syntax for creating and working with immutable types. + +Immutability isn't appropriate for all data scenarios. [Entity Framework Core](/ef/core/), for example, doesn't support updating with immutable entity types. + ## How records differ from classes The same syntax that [declares](classes.md#declaring-classes) and [instantiates](classes.md#creating-objects) classes can be used with records. Just substitute the `record` keyword for the `class` keyword. Likewise, the same syntax for expressing inheritance relationships is supported by records. Records differ from classes in the following ways: @@ -43,15 +43,15 @@ The same syntax that [declares](classes.md#declaring-classes) and [instantiates] The following example defines a public record that uses positional parameters to declare and instantiate a record. It then prints out the type name and property values: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" id="InstantiatePositional"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" id="InstantiatePositional"::: The following example demonstrates value equality in records: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" id="Equality"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" id="Equality"::: The following example demonstrates use of a `with` expression to copy an immutable object and change one of the properties: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" id="WithExpressions"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" id="WithExpressions"::: For more information, see [Records (C# reference)](../../language-reference/builtin-types/record.md). diff --git a/samples/snippets/csharp/programming-guide/classes-and-structs/class-example.cs b/docs/csharp/fundamentals/types/snippets/classes/Program.cs similarity index 100% rename from samples/snippets/csharp/programming-guide/classes-and-structs/class-example.cs rename to docs/csharp/fundamentals/types/snippets/classes/Program.cs diff --git a/docs/csharp/fundamentals/types/snippets/classes/classes.csproj b/docs/csharp/fundamentals/types/snippets/classes/classes.csproj new file mode 100644 index 0000000000000..2082704286ba4 --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/classes/classes.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 505a3f2272c5b..08bc01accf2b6 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -43,8 +43,6 @@ items: href: fundamentals/types/index.md - name: Namespaces href: fundamentals/types/namespaces.md - - name: Basic Types - href: fundamentals/types/basic-types.md # TODO: tuples - name: Records href: fundamentals/types/records.md diff --git a/samples/snippets/csharp/concepts/basic-types/generic-type.cs b/samples/snippets/csharp/concepts/basic-types/generic-type.cs deleted file mode 100644 index d78df11ed0aa2..0000000000000 --- a/samples/snippets/csharp/concepts/basic-types/generic-type.cs +++ /dev/null @@ -1 +0,0 @@ -List strings = new List(); \ No newline at end of file diff --git a/samples/snippets/csharp/concepts/basic-types/method-signature.cs b/samples/snippets/csharp/concepts/basic-types/method-signature.cs deleted file mode 100644 index aac487ee641df..0000000000000 --- a/samples/snippets/csharp/concepts/basic-types/method-signature.cs +++ /dev/null @@ -1,8 +0,0 @@ -public string GetName(int ID) -{ - if (ID < names.Length) - return names[ID]; - else - return String.Empty; -} -private string[] names = { "Spencer", "Sally", "Doug" }; diff --git a/samples/snippets/csharp/concepts/basic-types/type-safety.cs b/samples/snippets/csharp/concepts/basic-types/type-safety.cs deleted file mode 100644 index 99007726809bb..0000000000000 --- a/samples/snippets/csharp/concepts/basic-types/type-safety.cs +++ /dev/null @@ -1,7 +0,0 @@ -int a = 5; -int b = a + 2; //OK - -bool test = true; - -// Error. Operator '+' cannot be applied to operands of type 'int' and 'bool'. -int c = a + test; diff --git a/samples/snippets/csharp/concepts/basic-types/variable-declaration.cs b/samples/snippets/csharp/concepts/basic-types/variable-declaration.cs deleted file mode 100644 index 28197cda31118..0000000000000 --- a/samples/snippets/csharp/concepts/basic-types/variable-declaration.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Declaration only: -float temperature; -string name; -MyClass myClass; - -// Declaration with initializers (four examples): -char firstLetter = 'C'; -var limit = 3; -int[] source = { 0, 1, 2, 3, 4, 5 }; -var query = from item in source - where item <= limit - select item; From 20beb67f711def616bfe8fea3411ee918e24411c Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 16:21:23 -0400 Subject: [PATCH 25/60] edit generics And move the sample code. --- docs/csharp/fundamentals/types/classes.md | 6 +- docs/csharp/fundamentals/types/generics.md | 23 ++-- docs/csharp/fundamentals/types/interfaces.md | 17 ++- docs/csharp/fundamentals/types/records.md | 10 +- .../types/snippets/generics/Program.cs | 113 ++++++++++++++++++ .../types/snippets/generics/generics.csproj | 8 ++ .../csProgGuideGenerics/CS/Generics.cs | 113 ------------------ 7 files changed, 148 insertions(+), 142 deletions(-) create mode 100644 docs/csharp/fundamentals/types/snippets/generics/Program.cs create mode 100644 docs/csharp/fundamentals/types/snippets/generics/generics.csproj diff --git a/docs/csharp/fundamentals/types/classes.md b/docs/csharp/fundamentals/types/classes.md index fdd2ebdf7d6c8..baad55e18adfe 100644 --- a/docs/csharp/fundamentals/types/classes.md +++ b/docs/csharp/fundamentals/types/classes.md @@ -6,7 +6,7 @@ helpviewer_keywords: - "classes [C#]" - "C# language, classes" --- -# Classes +# Introduction to classes ## Reference types @@ -52,7 +52,7 @@ Although they are sometimes used interchangeably, a class and an object are diff 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 an existing object, such as this: + We don't recommend creating object references such as the preceding 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 an existing object, such as this: ```csharp Customer object3 = new Customer(); @@ -85,7 +85,7 @@ Class definitions can be split between different source files. For more informat ## Example -The following example defines a public class that contains an [auto-implemented property](../../programming-guide/classes-and-structs/auto-implemented-properties.md), a method, and a special method called a constructor. For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md), [Methods](../../programming-guide/classes-and-structs/methods.md), and [Constructors](../../programming-guide/classes-and-structs/constructors.md) topics. The instances of the class are then instantiated with the `new` keyword. +The following example defines a public class that contains an [auto-implemented property](../../programming-guide/classes-and-structs/auto-implemented-properties.md), a method, and a special method called a constructor. For more information, see [Properties](../../programming-guide/classes-and-structs/properties.md), [Methods](../../programming-guide/classes-and-structs/methods.md), and [Constructors](../../programming-guide/classes-and-structs/constructors.md) articles. The instances of the class are then instantiated with the `new` keyword. :::code language="csharp" interactive="try-dotnet" source="snippets/classes/Program.cs"::: diff --git a/docs/csharp/fundamentals/types/generics.md b/docs/csharp/fundamentals/types/generics.md index 1e0199663efd6..3c9d33e14f1fb 100644 --- a/docs/csharp/fundamentals/types/generics.md +++ b/docs/csharp/fundamentals/types/generics.md @@ -1,39 +1,38 @@ --- -title: "Generics - C# Programming Guide" +title: "Generic classes and methods" description: Learn about generics. Generic types maximize code reuse, type safety, and performance, and are commonly used to create collection classes. -ms.date: 07/20/2015 -helpviewer_keywords: +ms.date: 05/14/2021 +helpviewer_keywords: - "C# language, generics" - "generics [C#]" -ms.assetid: 75ea8509-a4ea-4e7a-a2b3-cf72482e9282 --- -# Generics (C# Programming Guide) +# Generic classes and methods -Generics introduce the concept of type parameters to .NET, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter `T`, you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here: +Generics introduces the concept of type parameters to .NET, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter `T`, you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations, as shown here: -[!code-csharp[csProgGuideGenerics#1](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs#1)] +:::code language="csharp" source="snippets/generics/Program.cs" ID="Snippet1"::: Generic classes and methods combine reusability, type safety, and efficiency in a way that their non-generic counterparts cannot. Generics are most frequently used with collections and the methods that operate on them. The namespace contains several generic-based collection classes. The non-generic collections, such as are not recommended and are maintained for compatibility purposes. For more information, see [Generics in .NET](../../../standard/generics/index.md). -Of course, you can also create custom generic types and methods to provide your own generalized solutions and design patterns that are type-safe and efficient. The following code example shows a simple generic linked-list class for demonstration purposes. (In most cases, you should use the class provided by .NET instead of creating your own.) The type parameter `T` is used in several locations where a concrete type would ordinarily be used to indicate the type of the item stored in the list. It is used in the following ways: +You can also create custom generic types and methods to provide your own generalized solutions and design patterns that are type-safe and efficient. The following code example shows a simple generic linked-list class for demonstration purposes. (In most cases, you should use the class provided by .NET instead of creating your own.) The type parameter `T` is used in several locations where a concrete type would ordinarily be used to indicate the type of the item stored in the list. It is used in the following ways: - As the type of a method parameter in the `AddHead` method. - As the return type of the `Data` property in the nested `Node` class. - As the type of the private member `data` in the nested class. - Note that `T` is available to the nested `Node` class. When `GenericList` is instantiated with a concrete type, for example as a `GenericList`, each occurrence of `T` will be replaced with `int`. +`T` is available to the nested `Node` class. When `GenericList` is instantiated with a concrete type, for example as a `GenericList`, each occurrence of `T` will be replaced with `int`. -[!code-csharp[csProgGuideGenerics#2](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs#2)] +:::code language="csharp" source="snippets/generics/Program.cs" ID="Snippet2"::: The following code example shows how client code uses the generic `GenericList` class to create a list of integers. Simply by changing the type argument, the following code could easily be modified to create lists of strings or any other custom type: -[!code-csharp[csProgGuideGenerics#3](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs#3)] +:::code language="csharp" source="snippets/generics/Program.cs" ID="Snippet3"::: ## Generics overview - Use generic types to maximize code reuse, type safety, and performance. - The most common use of generics is to create collection classes. -- The .NET class library contains several generic collection classes in the namespace. These should be used whenever possible instead of classes such as in the namespace. +- The .NET class library contains several generic collection classes in the namespace. The generic collections should be used whenever possible instead of classes such as in the namespace. - You can create your own generic interfaces, classes, methods, events, and delegates. - Generic classes may be constrained to enable access to methods on particular data types. - Information on the types that are used in a generic data type may be obtained at run-time by using reflection. diff --git a/docs/csharp/fundamentals/types/interfaces.md b/docs/csharp/fundamentals/types/interfaces.md index c41019bb79188..48f95a8ccfd70 100644 --- a/docs/csharp/fundamentals/types/interfaces.md +++ b/docs/csharp/fundamentals/types/interfaces.md @@ -1,21 +1,20 @@ --- -title: "Interfaces - C# Programming Guide" -description: An interface in C# contains definitions for a group of related functionalities that a non-abstract class or a struct must implement. -ms.date: 02/20/2020 +title: "Interfaces" +description: An interface in C# contains definitions for a group of related functionalities that a non-abstract class or a struct must implement. It describes the behavior of a type that implements the interface. +ms.date: 05/14/2021 helpviewer_keywords: - "interfaces [C#]" - "C# language, interfaces" -ms.assetid: 2feda177-ce11-432d-81b4-d50f5f35fd37 --- -# Interfaces (C# Programming Guide) +# Interfaces -An interface contains definitions for a group of related functionalities that a non-abstract [class](../../language-reference/keywords/class.md) or a [struct](../../language-reference/builtin-types/struct.md) must implement. An interface may define `static` methods, which must have an implementation. Beginning with C# 8.0, an interface may define a default implementation for members. An interface may not declare instance data such as fields, auto-implemented properties, or property-like events. +An interface contains definitions for a group of related functionalities that a non-abstract [`class`](../../language-reference/keywords/class.md) or a [`struct`](../../language-reference/builtin-types/struct.md) must implement. An interface may define `static` methods, which must have an implementation. Beginning with C# 8.0, an interface may define a default implementation for members. An interface may not declare instance data such as fields, auto-implemented properties, or property-like events. By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes. In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class. -You define an interface by using the [interface](../../language-reference/keywords/interface.md) keyword as the following example shows. +You define an interface by using the [`interface`](../../language-reference/keywords/interface.md) keyword as the following example shows. -:::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" id="Equatable"::: +:::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" ID="Equatable"::: The name of an interface must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). By convention, interface names begin with a capital `I`. @@ -33,7 +32,7 @@ When a class or struct implements an interface, the class or struct must provide The following example shows an implementation of the interface. The implementing class, `Car`, must provide an implementation of the method. -:::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" id="ImplementEquatable"::: +:::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" ID="ImplementEquatable"::: Properties and indexers of a class can define extra accessors for a property or indexer that's defined in an interface. For example, an interface might declare a property that has a [get](../../language-reference/keywords/get.md) accessor. The class that implements the interface can declare the same property with both a `get` and [set](../../language-reference/keywords/set.md) accessor. However, if the property or indexer uses explicit implementation, the accessors must match. For more information about explicit implementation, see [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md) and [Interface Properties](../../programming-guide/classes-and-structs/interface-properties.md). diff --git a/docs/csharp/fundamentals/types/records.md b/docs/csharp/fundamentals/types/records.md index 4c05e5a283aee..ca8dbf27bf323 100644 --- a/docs/csharp/fundamentals/types/records.md +++ b/docs/csharp/fundamentals/types/records.md @@ -6,7 +6,7 @@ helpviewer_keywords: - "records [C#]" - "C# language, records" --- -# Records +# Introduction to records A [record](../../language-reference/builtin-types/record.md) is a [class](../../language-reference/keywords/class.md) that provides special syntax and behavior for working with data models. For information about classes, see [Classes](classes.md). @@ -41,17 +41,17 @@ The same syntax that [declares](classes.md#declaring-classes) and [instantiates] ## Examples -The following example defines a public record that uses positional parameters to declare and instantiate a record. It then prints out the type name and property values: +The following example defines a public record that uses positional parameters to declare and instantiate a record. It then prints the type name and property values: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" id="InstantiatePositional"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" ID="InstantiatePositional"::: The following example demonstrates value equality in records: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" id="Equality"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" ID="Equality"::: The following example demonstrates use of a `with` expression to copy an immutable object and change one of the properties: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" id="WithExpressions"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" ID="WithExpressions"::: For more information, see [Records (C# reference)](../../language-reference/builtin-types/record.md). diff --git a/docs/csharp/fundamentals/types/snippets/generics/Program.cs b/docs/csharp/fundamentals/types/snippets/generics/Program.cs new file mode 100644 index 0000000000000..a1a3981d01c5e --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/generics/Program.cs @@ -0,0 +1,113 @@ +using System; + +namespace generics +{ + //--------------------------------------------------------------------------- + // + // Declare the generic class. + public class GenericList + { + public void Add(T input) { } + } + class TestGenericList + { + private class ExampleClass { } + static void Main() + { + // Declare a list of type int. + GenericList list1 = new GenericList(); + list1.Add(1); + + // Declare a list of type string. + GenericList list2 = new GenericList(); + list2.Add(""); + + // Declare a list of type ExampleClass. + GenericList list3 = new GenericList(); + list3.Add(new ExampleClass()); + } + } + // + // + // type parameter T in angle brackets + public class GenericList + { + // The nested class is also generic on T. + private class Node + { + // T used in non-generic constructor. + public Node(T t) + { + next = null; + data = t; + } + + private Node next; + public Node Next + { + get { return next; } + set { next = value; } + } + + // T as private member data type. + private T data; + + // T as return type of property. + public T Data + { + get { return data; } + set { data = value; } + } + } + + private Node head; + + // constructor + public GenericList() + { + head = null; + } + + // T as method parameter type: + public void AddHead(T t) + { + Node n = new Node(t); + n.Next = head; + head = n; + } + + public IEnumerator GetEnumerator() + { + Node current = head; + + while (current != null) + { + yield return current.Data; + current = current.Next; + } + } + } + // + // + class TestGenericList + { + static void Main() + { + // int is the type argument + GenericList list = new GenericList(); + + for (int x = 0; x < 10; x++) + { + list.AddHead(x); + } + + foreach (int i in list) + { + System.Console.Write(i + " "); + } + System.Console.WriteLine("\nDone"); + } + } + // + +} diff --git a/docs/csharp/fundamentals/types/snippets/generics/generics.csproj b/docs/csharp/fundamentals/types/snippets/generics/generics.csproj new file mode 100644 index 0000000000000..2082704286ba4 --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/generics/generics.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs index 73bc88295f28e..a4c760bf5e0b2 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs @@ -2,32 +2,6 @@ namespace CsCsrefProgrammingGenerics { - //--------------------------------------------------------------------------- - // - // Declare the generic class. - public class GenericList - { - public void Add(T input) { } - } - class TestGenericList - { - private class ExampleClass { } - static void Main() - { - // Declare a list of type int. - GenericList list1 = new GenericList(); - list1.Add(1); - - // Declare a list of type string. - GenericList list2 = new GenericList(); - list2.Add(""); - - // Declare a list of type ExampleClass. - GenericList list3 = new GenericList(); - list3.Add(new ExampleClass()); - } - } - // class TestGenericList2 { @@ -43,93 +17,6 @@ static void Main() } } - //--------------------------------------------------------------------------- - namespace WrapOverview - { - // - // type parameter T in angle brackets - public class GenericList - { - // The nested class is also generic on T. - private class Node - { - // T used in non-generic constructor. - public Node(T t) - { - next = null; - data = t; - } - - private Node next; - public Node Next - { - get { return next; } - set { next = value; } - } - - // T as private member data type. - private T data; - - // T as return type of property. - public T Data - { - get { return data; } - set { data = value; } - } - } - - private Node head; - - // constructor - public GenericList() - { - head = null; - } - - // T as method parameter type: - public void AddHead(T t) - { - Node n = new Node(t); - n.Next = head; - head = n; - } - - public IEnumerator GetEnumerator() - { - Node current = head; - - while (current != null) - { - yield return current.Data; - current = current.Next; - } - } - } - // - - // - class TestGenericList - { - static void Main() - { - // int is the type argument - GenericList list = new GenericList(); - - for (int x = 0; x < 10; x++) - { - list.AddHead(x); - } - - foreach (int i in list) - { - System.Console.Write(i + " "); - } - System.Console.WriteLine("\nDone"); - } - } - // - } - //--------------------------------------------------------------------------- public class WrapBenefits { From 4a14c60e90c64f04b91dccec14f7a2a22f50c179 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 16:39:26 -0400 Subject: [PATCH 26/60] edit for anonymous types --- .../fundamentals/types/anonymous-types.md | 89 +++++++++---------- docs/csharp/fundamentals/types/interfaces.md | 4 +- .../types/snippets/anonymous-types/Program.cs | 34 +++++++ .../anonymous-types/anonymous-types.csproj | 9 ++ .../csProgGuideGenerics/CS/Generics.cs | 58 ++++++++++++ .../csRef30Features/CS/csref30.cs | 34 ------- 6 files changed, 146 insertions(+), 82 deletions(-) create mode 100644 docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs create mode 100644 docs/csharp/fundamentals/types/snippets/anonymous-types/anonymous-types.csproj diff --git a/docs/csharp/fundamentals/types/anonymous-types.md b/docs/csharp/fundamentals/types/anonymous-types.md index 7a10873ff613b..078c5cee5d89b 100644 --- a/docs/csharp/fundamentals/types/anonymous-types.md +++ b/docs/csharp/fundamentals/types/anonymous-types.md @@ -1,52 +1,49 @@ --- -title: "Anonymous Types - C# Programming Guide" +title: "Anonymous Types" description: Anonymous types in C# encapsulate a set of read-only properties in an object without having to explicitly define a type. The compiler generates a name. -ms.date: 07/20/2015 +ms.date: 05/14/2021 helpviewer_keywords: - "anonymous types [C#]" - "C# Language, anonymous types" -ms.assetid: 59c9d7a4-3b0e-475e-b620-0ab86c088e9b --- -# Anonymous Types (C# Programming Guide) - -Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler. - - You create anonymous types by using the [new](../../language-reference/operators/new-operator.md) operator together with an object initializer. For more information about object initializers, see [Object and Collection Initializers](../../programming-guide/classes-and-structs/object-and-collection-initializers.md). - - The following example shows an anonymous type that is initialized with two properties named `Amount` and `Message`. - -```csharp -var v = new { Amount = 108, Message = "Hello" }; - -// Rest the mouse pointer over v.Amount and v.Message in the following -// statement to verify that their inferred types are int and string. -Console.WriteLine(v.Amount + v.Message); -``` - - Anonymous types typically are used in the [select](../../language-reference/keywords/select-clause.md) clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see [LINQ in C#](../../linq/index.md). - - Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid. The expression that is used to initialize a property cannot be `null`, an anonymous function, or a pointer type. - - The most common scenario is to initialize an anonymous type with properties from another type. In the following example, assume that a class exists that is named `Product`. Class `Product` includes `Color` and `Price` properties, together with other properties that you are not interested in. Variable `products` is a collection of `Product` objects. The anonymous type declaration starts with the `new` keyword. The declaration initializes a new type that uses only two properties from `Product`. This causes a smaller amount of data to be returned in the query. - - If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You must provide a name for a property that is being initialized with an expression, as shown in the previous example. In the following example, the names of the properties of the anonymous type are `Color` and `Price`. - - [!code-csharp[csRef30Features#81](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csRef30Features/CS/csref30.cs#81)] - - Typically, when you use an anonymous type to initialize a variable, you declare the variable as an implicitly typed local variable by using [var](../../language-reference/keywords/var.md). The type name cannot be specified in the variable declaration because only the compiler has access to the underlying name of the anonymous type. For more information about `var`, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). - - You can create an array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example. - -```csharp -var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }}; -``` - -## Remarks - - Anonymous types are [class](../../language-reference/keywords/class.md) types that derive directly from [object](../../language-reference/builtin-types/reference-types.md), and that cannot be cast to any type except [object](../../language-reference/builtin-types/reference-types.md). The compiler provides a name for each anonymous type, although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type. - - If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same names and types, the compiler treats the objects as instances of the same type. They share the same compiler-generated type information. - - You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as type object. However, doing this defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type. - - Because the and methods on anonymous types are defined in terms of the `Equals` and `GetHashCode` methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal. +# Anonymous Types + +Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler. + +You create anonymous types by using the [`new`](../../language-reference/operators/new-operator.md) operator together with an object initializer. For more information about object initializers, see [Object and Collection Initializers](../../programming-guide/classes-and-structs/object-and-collection-initializers.md). + +The following example shows an anonymous type that is initialized with two properties named `Amount` and `Message`. + +```csharp +var v = new { Amount = 108, Message = "Hello" }; + +// Rest the mouse pointer over v.Amount and v.Message in the following +// statement to verify that their inferred types are int and string. +Console.WriteLine(v.Amount + v.Message); +``` + +Anonymous types typically are used in the [`select`](../../language-reference/keywords/select-clause.md) clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, see [LINQ in C#](../../linq/index.md). + +Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid. The expression that is used to initialize a property cannot be `null`, an anonymous function, or a pointer type. + +The most common scenario is to initialize an anonymous type with properties from another type. In the following example, assume that a class exists that is named `Product`. Class `Product` includes `Color` and `Price` properties, together with other properties that you are not interested in. Variable `products` is a collection of `Product` objects. The anonymous type declaration starts with the `new` keyword. The declaration initializes a new type that uses only two properties from `Product`. Using anonymous types causes a smaller amount of data to be returned in the query. + +If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You provide a name for a property that is being initialized with an expression, as shown in the previous example. In the following example, the names of the properties of the anonymous type are `Color` and `Price`. + +:::code language="csharp" source="snippets/anonymous-types/Program.cs" ID="snippet81"::: + +Typically, when you use an anonymous type to initialize a variable, you declare the variable as an implicitly typed local variable by using [var](../../language-reference/keywords/var.md). The type name cannot be specified in the variable declaration because only the compiler has access to the underlying name of the anonymous type. For more information about `var`, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md). + +You can create an array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example. + +```csharp +var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }}; +``` + +Anonymous types are [`class`](../../language-reference/keywords/class.md) types that derive directly from [`object`](../../language-reference/builtin-types/reference-types.md), and that cannot be cast to any type except [`object`](../../language-reference/builtin-types/reference-types.md). The compiler provides a name for each anonymous type, although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type. + +If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same names and types, the compiler treats the objects as instances of the same type. They share the same compiler-generated type information. + +You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as type `object`. However, using `object` for anonymous types defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type. + +Because the and methods on anonymous types are defined in terms of the `Equals` and `GetHashCode` methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal. diff --git a/docs/csharp/fundamentals/types/interfaces.md b/docs/csharp/fundamentals/types/interfaces.md index 48f95a8ccfd70..5685a030c426b 100644 --- a/docs/csharp/fundamentals/types/interfaces.md +++ b/docs/csharp/fundamentals/types/interfaces.md @@ -1,12 +1,12 @@ --- -title: "Interfaces" +title: "Interfaces - define behavior for multiple types" description: An interface in C# contains definitions for a group of related functionalities that a non-abstract class or a struct must implement. It describes the behavior of a type that implements the interface. ms.date: 05/14/2021 helpviewer_keywords: - "interfaces [C#]" - "C# language, interfaces" --- -# Interfaces +# Interfaces - define behavior for multiple types An interface contains definitions for a group of related functionalities that a non-abstract [`class`](../../language-reference/keywords/class.md) or a [`struct`](../../language-reference/builtin-types/struct.md) must implement. An interface may define `static` methods, which must have an implementation. Beginning with C# 8.0, an interface may define a default implementation for members. An interface may not declare instance data such as fields, auto-implemented properties, or property-like events. diff --git a/docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs b/docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs new file mode 100644 index 0000000000000..4a6fcc57a8227 --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs @@ -0,0 +1,34 @@ +using System; + +namespace anonymous_types +{ + class Product + { + public string Color {get;set;} + public decimal Price {get;set;} + } + class Anonymous + { + static void Main() + { + // don't show this unless you add a bunch more + // properties to the type. otherwise it obviates the + // need for the anonymous type + List products = new List() + { + new Product() { Color="Orange", Price=2.00M}, + }; + + // + var productQuery = + from prod in products + select new { prod.Color, prod.Price }; + + foreach (var v in productQuery) + { + Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price); + } + // + } + } +} diff --git a/docs/csharp/fundamentals/types/snippets/anonymous-types/anonymous-types.csproj b/docs/csharp/fundamentals/types/snippets/anonymous-types/anonymous-types.csproj new file mode 100644 index 0000000000000..156fd1747df13 --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/anonymous-types/anonymous-types.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + anonymous_types + + + diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs index a4c760bf5e0b2..1a92698d5be32 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs @@ -2,6 +2,64 @@ namespace CsCsrefProgrammingGenerics { + public class GenericList + { + // The nested class is also generic on T. + private class Node + { + // T used in non-generic constructor. + public Node(T t) + { + next = null; + data = t; + } + + private Node next; + public Node Next + { + get { return next; } + set { next = value; } + } + + // T as private member data type. + private T data; + + // T as return type of property. + public T Data + { + get { return data; } + set { data = value; } + } + } + + private Node head; + + // constructor + public GenericList() + { + head = null; + } + + // T as method parameter type: + public void AddHead(T t) + { + Node n = new Node(t); + n.Next = head; + head = n; + } + + public IEnumerator GetEnumerator() + { + Node current = head; + + while (current != null) + { + yield return current.Data; + current = current.Next; + } + } + } + // class TestGenericList2 { diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csRef30Features/CS/csref30.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csRef30Features/CS/csref30.cs index 8384158378886..af92cd2207edf 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csRef30Features/CS/csref30.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csRef30Features/CS/csref30.cs @@ -595,40 +595,6 @@ orderby str descending } // - class Container - { - - class Product - { - public string Color {get;set;} - public decimal Price {get;set;} - } - class Anonymous - { - static void Main() - { - // don't show this unless you add a bunch more - // properties to the type. otherwise it obviates the - // need for the anonymous type - List products = new List() - { - new Product() { Color="Orange", Price=2.00M}, - }; - - // - var productQuery = - from prod in products - select new { prod.Color, prod.Price }; - - foreach (var v in productQuery) - { - Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price); - } - // - } - } -} - class CSHarp30 { From 42497aad9e208ee6e7cc02a52e2817fc5c1392ed Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 16:50:27 -0400 Subject: [PATCH 27/60] fix build issues. --- .../fundamentals/program-structure/index.md | 14 +- .../types/snippets/anonymous-types/Program.cs | 2 + .../types/snippets/generics/Program.cs | 126 ++++++++++-------- .../types/snippets/generics/generics.csproj | 1 + docs/csharp/toc.yml | 26 ++-- 5 files changed, 90 insertions(+), 79 deletions(-) diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index 0fa31546ff5cf..b78b15e97eb0f 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -1,20 +1,20 @@ --- -title: "General Structure of a C# Program - C# Programming Guide" +title: "General Structure of a C# Program" description: Learn about the structure of a C# program by using a skeleton program that contains all the required elements for a program. -ms.date: 07/20/2015 +ms.date: 05/14/2021 helpviewer_keywords: - "C# language, program structure" ms.assetid: 5ae964a5-0ef0-40fe-88fb-6d1793371d0d --- -# General Structure of a C# Program (C# Programming Guide) +# General Structure of a C# Program -C# programs can consist of one or more files. Each file can contain zero or more namespaces. A namespace can contain types such as classes, structs, interfaces, enumerations, and delegates, in addition to other namespaces. The following is the skeleton of a C# program that contains all of these elements. +C# programs consists of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements. -:::code language="csharp" source="snippets/toplevel-structure/Program.cs"::: +:::code language="csharp" source="snippets/toplevel-structure/Program.cs" interactive="try-dotnet"::: -The preceding example uses *top level statements* for the program's entry point. This feature was added in C# 9. Prior to C# 9, the entry point was a static method named `Main`, as shown in the following example: +The preceding example uses *top-level statements* for the program's entry point. This feature was added in C# 9. Prior to C# 9, the entry point was a static method named `Main`, as shown in the following example: -:::code language="csharp" source="snippets/structure/Program.cs"::: +:::code language="csharp" source="snippets/structure/Program.cs" interactive= "try-dotnet"::: ## Related Sections diff --git a/docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs b/docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs index 4a6fcc57a8227..8dd1e8923532f 100644 --- a/docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs +++ b/docs/csharp/fundamentals/types/snippets/anonymous-types/Program.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; namespace anonymous_types { diff --git a/docs/csharp/fundamentals/types/snippets/generics/Program.cs b/docs/csharp/fundamentals/types/snippets/generics/Program.cs index a1a3981d01c5e..9c5d688691a72 100644 --- a/docs/csharp/fundamentals/types/snippets/generics/Program.cs +++ b/docs/csharp/fundamentals/types/snippets/generics/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace generics { @@ -28,86 +29,93 @@ static void Main() } } // - // - // type parameter T in angle brackets - public class GenericList + namespace SecondExample { - // The nested class is also generic on T. - private class Node + // + // type parameter T in angle brackets + public class GenericList { - // T used in non-generic constructor. - public Node(T t) + // The nested class is also generic on T. + private class Node { - next = null; - data = t; - } + // T used in non-generic constructor. + public Node(T t) + { + next = null; + data = t; + } - private Node next; - public Node Next - { - get { return next; } - set { next = value; } - } + private Node next; + public Node Next + { + get { return next; } + set { next = value; } + } - // T as private member data type. - private T data; + // T as private member data type. + private T data; - // T as return type of property. - public T Data - { - get { return data; } - set { data = value; } + // T as return type of property. + public T Data + { + get { return data; } + set { data = value; } + } } - } - private Node head; + private Node head; - // constructor - public GenericList() - { - head = null; - } - - // T as method parameter type: - public void AddHead(T t) - { - Node n = new Node(t); - n.Next = head; - head = n; - } + // constructor + public GenericList() + { + head = null; + } - public IEnumerator GetEnumerator() - { - Node current = head; + // T as method parameter type: + public void AddHead(T t) + { + Node n = new Node(t); + n.Next = head; + head = n; + } - while (current != null) + public IEnumerator GetEnumerator() { - yield return current.Data; - current = current.Next; + Node current = head; + + while (current != null) + { + yield return current.Data; + current = current.Next; + } } } + // } - // - // - class TestGenericList + namespace ThirdExample { - static void Main() + using SecondExample; + // + class TestGenericList { - // int is the type argument - GenericList list = new GenericList(); - - for (int x = 0; x < 10; x++) + static void Main() { - list.AddHead(x); - } + // int is the type argument + GenericList list = new GenericList(); - foreach (int i in list) - { - System.Console.Write(i + " "); + for (int x = 0; x < 10; x++) + { + list.AddHead(x); + } + + foreach (int i in list) + { + System.Console.Write(i + " "); + } + System.Console.WriteLine("\nDone"); } - System.Console.WriteLine("\nDone"); } + // } - // } diff --git a/docs/csharp/fundamentals/types/snippets/generics/generics.csproj b/docs/csharp/fundamentals/types/snippets/generics/generics.csproj index 2082704286ba4..b2a06aa4e57ee 100644 --- a/docs/csharp/fundamentals/types/snippets/generics/generics.csproj +++ b/docs/csharp/fundamentals/types/snippets/generics/generics.csproj @@ -3,6 +3,7 @@ Exe net5.0 + generics.TestGenericList diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 08bc01accf2b6..a7a688929eede 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -37,6 +37,19 @@ items: href: tour-of-csharp/tutorials/arrays-and-collections.md - name: Fundamentals items: + - name: Program structure + items: + - name: Overview + href: fundamentals/program-structure/index.md + - name: Top-level statements + href: fundamentals/program-structure/top-level-statements.md + - name: Main method + href: fundamentals/program-structure/main-command-line.md + - name: Tutorials + items: + - name: "How to display command-line arguments" + href: fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md + # Command line processing tutorials - name: Type system items: - name: Overview @@ -62,19 +75,6 @@ items: # TODO: Classes and objects (interfaces and classes) # TODO: Generics (methods and classes) # TODO: Fundamentals of using lambdas - - name: Program structure - items: - - name: Overview - href: fundamentals/program-structure/index.md - - name: Top-level statements - href: fundamentals/program-structure/top-level-statements.md - - name: Main method - href: fundamentals/program-structure/main-command-line.md - - name: Tutorials - items: - - name: "How to display command-line arguments" - href: fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md - # Command line processing tutorials - name: Object Oriented programming items: - name: Overview From 343e075e5ece7cc9437c1445b2a649586f01d00d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 17:05:22 -0400 Subject: [PATCH 28/60] edit program structure --- .../program-structure/main-command-line.md | 25 ++++++++------ .../snippets}/main-arguments/Program.cs | 0 .../main-arguments/main-arguments.csproj | 2 +- .../main-command-line/MainReturnValTest.cs | 9 +++++ .../snippets/main-command-line/Program.cs | 21 ++++++++++++ .../snippets/main-command-line/TestClass.cs | 8 +++++ .../main-command-line.csproj | 10 ++++++ .../program-structure/top-level-statements.md | 6 ++-- .../csProgGuideMain/CS/Class1.cs | 11 ------ .../csProgGuideMain/CS/Class3.cs | 34 +------------------ 10 files changed, 67 insertions(+), 59 deletions(-) rename {samples/snippets/csharp => docs/csharp/fundamentals/program-structure/snippets}/main-arguments/Program.cs (100%) rename {samples/snippets/csharp => docs/csharp/fundamentals/program-structure/snippets}/main-arguments/main-arguments.csproj (68%) create mode 100644 docs/csharp/fundamentals/program-structure/snippets/main-command-line/MainReturnValTest.cs create mode 100644 docs/csharp/fundamentals/program-structure/snippets/main-command-line/Program.cs create mode 100644 docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs create mode 100644 docs/csharp/fundamentals/program-structure/snippets/main-command-line/main-command-line.csproj diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index e55d94bd5675f..185096d37189e 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -1,7 +1,7 @@ --- -title: "Main() and command-line arguments - C# Programming Guide" +title: "Main() and command-line arguments" description: Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program. -ms.date: 03/08/2021 +ms.date: 05/14/2021 f1_keywords: - "main_CSharpKeyword" - "Main" @@ -11,15 +11,14 @@ helpviewer_keywords: - "arguments [C#], command-line" - "command line [C#], arguments" - "command-line arguments [C#], Main method" -ms.assetid: 73a17231-cf96-44ea-aa8a-54807c6fb1f4 --- -# Main() and command-line arguments (C# Programming Guide) +# Main() and command-line arguments The `Main` method is the entry point of a C# application. (Libraries and services do not require a `Main` method as an entry point.) When the application is started, the `Main` method is the first method that is invoked. There can only be one entry point in a C# program. If you have more than one class that has a `Main` method, you must compile your program with the **StartupObject** compiler option to specify which `Main` method to use as the entry point. For more information, see [**StartupObject** (C# Compiler Options)](../../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject). -[!code-csharp[csProgGuideMain#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#17)] +:::code language="csharp" source="snippets/main-command-line/TestClass.cs"::: Starting in C# 9, you can omit the `Main` method, and write C# statements as if they were in the `Main` method, as in the following example: @@ -30,12 +29,16 @@ For information about how to write application code with an implicit entry point ## Overview - The `Main` method is the entry point of an executable program; it is where the program control starts and ends. +<<<<<<< HEAD - `Main` is declared inside a class or struct. `Main` must be [static](../../language-reference/keywords/static.md) and it doesn't need to be [public](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [private](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. +======= +- `Main` is declared inside a class or struct. `Main` must be [`static`](../../language-reference/keywords/static.md) and it need not be [`public`](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [`private`](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. +>>>>>>> cd631f0b85 (edit program structure) - `Main` can either have a `void`, `int`, or, starting with C# 7.1, `Task`, or `Task` return type. - If and only if `Main` returns a `Task` or `Task`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. Note that this specifically excludes an `async void Main` method. - The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the method to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the method. -The following is a list of valid `Main` signatures: +The following list shows valid `Main` signatures: ```csharp public static void Main() { } @@ -48,7 +51,7 @@ public static async Task Main(string[] args) { } public static async Task Main(string[] args) { } ``` -The preceding examples all use the public accessor modifier. That is typical, but not required. +The preceding examples all use the `public` accessor modifier. That is typical, but not required. The addition of `async` and `Task`, `Task` return types simplifies program code when console applications need to start and `await` asynchronous operations in `Main`. @@ -80,7 +83,7 @@ This example uses [.NET Core](../../../core/introduction.md) command-line tools. Modify the `Main` method in *program.cs* as follows: - [!code-csharp[csProgGuideMain#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs#14)] +:::code language="csharp" source="snippets/main-command-line/MainReturnValTest.cs"::: When a program is executed in Windows, any value returned from the `Main` function is stored in an environment variable. This environment variable can be retrieved using `ERRORLEVEL` from a batch file, or `$LastExitCode` from PowerShell. @@ -128,7 +131,7 @@ private static async Task AsyncConsoleWork() Now, this can be replaced by: -[!code-csharp[AsyncMain](../../../../samples/snippets/csharp/main-arguments/program.cs#AsyncMain)] +:::code language="csharp" source="snippets/main-arguments/Program.cs"::: The advantage of the new syntax is that the compiler always generates the correct code. @@ -167,7 +170,7 @@ If the arguments are not used, you can omit `args` from the method signature for The parameter of the `Main` method is a array that represents the command-line arguments. Usually you determine whether arguments exist by testing the `Length` property, for example: -[!code-csharp[csProgGuideMain#4](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs#4)] +:::code language="csharp" source="snippets/main-command-line/Program.cs" ID="Snippet4"::: > [!TIP] > The `args` array cannot be null. So, it's safe to access the `Length` property without null checking. @@ -200,7 +203,7 @@ To compile and run the application from a command prompt, follow these steps: [!code-csharp[csProgGuideMain#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#16)] -2. From the **Start** screen or **Start** menu, open a Visual Studio **Developer Command Prompt** window, and then navigate to the folder that contains the file that you just created. +2. From the **Start** screen or **Start** menu, open a Visual Studio **Developer Command Prompt** window, and then navigate to the folder that contains the file that you created. 3. Enter the following command to compile the application. diff --git a/samples/snippets/csharp/main-arguments/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/main-arguments/Program.cs similarity index 100% rename from samples/snippets/csharp/main-arguments/Program.cs rename to docs/csharp/fundamentals/program-structure/snippets/main-arguments/Program.cs diff --git a/samples/snippets/csharp/main-arguments/main-arguments.csproj b/docs/csharp/fundamentals/program-structure/snippets/main-arguments/main-arguments.csproj similarity index 68% rename from samples/snippets/csharp/main-arguments/main-arguments.csproj rename to docs/csharp/fundamentals/program-structure/snippets/main-arguments/main-arguments.csproj index c73e0d1692ab3..2082704286ba4 100644 --- a/samples/snippets/csharp/main-arguments/main-arguments.csproj +++ b/docs/csharp/fundamentals/program-structure/snippets/main-arguments/main-arguments.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp3.1 + net5.0 diff --git a/docs/csharp/fundamentals/program-structure/snippets/main-command-line/MainReturnValTest.cs b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/MainReturnValTest.cs new file mode 100644 index 0000000000000..6045578ee314d --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/MainReturnValTest.cs @@ -0,0 +1,9 @@ +// Save this program as MainReturnValTest.cs. +class MainReturnValTest +{ + static int Main() + { + //... + return 0; + } +} diff --git a/docs/csharp/fundamentals/program-structure/snippets/main-command-line/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/Program.cs new file mode 100644 index 0000000000000..04cb08963d026 --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/Program.cs @@ -0,0 +1,21 @@ +using System; + +namespace main_command_line +{ + class Program + { + static int Main(string[] args) + { + // + if (args.Length == 0) + { + System.Console.WriteLine("Please enter a numeric argument."); + return 1; + } + // + + return 0; + } + } +} + diff --git a/docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs new file mode 100644 index 0000000000000..419f9a9fd8619 --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs @@ -0,0 +1,8 @@ +class TestClass +{ + static void Main(string[] args) + { + // Display the number of command line arguments. + Console.WriteLine(args.Length); + } +} diff --git a/docs/csharp/fundamentals/program-structure/snippets/main-command-line/main-command-line.csproj b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/main-command-line.csproj new file mode 100644 index 0000000000000..889abddd009c9 --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/main-command-line.csproj @@ -0,0 +1,10 @@ + + + + Exe + net5.0 + main_command_line + main_command_line.Program + + + diff --git a/docs/csharp/fundamentals/program-structure/top-level-statements.md b/docs/csharp/fundamentals/program-structure/top-level-statements.md index 29a4606ebcada..47832061a9f46 100644 --- a/docs/csharp/fundamentals/program-structure/top-level-statements.md +++ b/docs/csharp/fundamentals/program-structure/top-level-statements.md @@ -1,12 +1,12 @@ --- -title: "Top-level statements - C# Programming Guide" +title: "Top-level statements" description: Learn about top-level statements in C# 9 and later. -ms.date: 03/08/2021 +ms.date: 05/14/2021 helpviewer_keywords: - "C# language, top-level statements" - "C# language, Main method" --- -# Top-level statements (C# Programming Guide) +# Top-level statements Starting in C# 9, you don't have to explicitly include a `Main` method in a console application project. Instead, you can use the *top-level statements* feature to minimize the code you have to write. In this case, the compiler generates a class and `Main` method entry point for the application. diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs index bb12862652458..90265dba85643 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs @@ -85,15 +85,4 @@ static void Main(string[] args) Arg[2] = [c] */ // - - // - class TestClass - { - static void Main(string[] args) - { - // Display the number of command line arguments. - Console.WriteLine(args.Length); - } - } - // } diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs index cf509f8830e18..1d38b136f0081 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class3.cs @@ -1,22 +1,4 @@ -class Test1 -{ - // - static int Main(string[] args) - // - { - // - if (args.Length == 0) - { - System.Console.WriteLine("Please enter a numeric argument."); - return 1; - } - // - - return 0; - } -} - -class Test2 +class Test2 { // static void Main(string[] args) @@ -36,20 +18,6 @@ static void Main() // } -//----------------------------------------------------------------------------- -// -// Save this program as MainReturnValTest.cs. -class MainReturnValTest -{ - // - static int Main() - { - //... - return 0; - } - // -} -// //----------------------------------------------------------------------------- class Test4 From c7437806567138dfb03d0f0d3499ad5283d701f4 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 17:10:10 -0400 Subject: [PATCH 29/60] build error --- .../program-structure/snippets/main-command-line/TestClass.cs | 2 ++ .../fundamentals/program-structure/top-level-statements.md | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs index 419f9a9fd8619..cca8508a4ae1e 100644 --- a/docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs +++ b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/TestClass.cs @@ -1,3 +1,5 @@ +using System; + class TestClass { static void Main(string[] args) diff --git a/docs/csharp/fundamentals/program-structure/top-level-statements.md b/docs/csharp/fundamentals/program-structure/top-level-statements.md index 47832061a9f46..1c0d42ae6e053 100644 --- a/docs/csharp/fundamentals/program-structure/top-level-statements.md +++ b/docs/csharp/fundamentals/program-structure/top-level-statements.md @@ -1,6 +1,6 @@ --- -title: "Top-level statements" -description: Learn about top-level statements in C# 9 and later. +title: "Top-level statements - programs without Main methods" +description: Learn about top-level statements in C# 9 and later. You can create programs without the ceremony of a Program class and a Main method. ms.date: 05/14/2021 helpviewer_keywords: - "C# language, top-level statements" From 3b6158ee27972b3fe1453557d57369776632890a Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 17:11:44 -0400 Subject: [PATCH 30/60] no interactive top level statements --- docs/csharp/fundamentals/program-structure/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index b78b15e97eb0f..a2ca1ad40fd77 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -10,11 +10,11 @@ ms.assetid: 5ae964a5-0ef0-40fe-88fb-6d1793371d0d C# programs consists of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements. -:::code language="csharp" source="snippets/toplevel-structure/Program.cs" interactive="try-dotnet"::: +:::code language="csharp" source="snippets/toplevel-structure/Program.cs"::: The preceding example uses *top-level statements* for the program's entry point. This feature was added in C# 9. Prior to C# 9, the entry point was a static method named `Main`, as shown in the following example: -:::code language="csharp" source="snippets/structure/Program.cs" interactive= "try-dotnet"::: +:::code language="csharp" source="snippets/structure/Program.cs"::: ## Related Sections From 93d171510ec2a2518529016f655b92ca76f1924d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 13 May 2021 17:15:13 -0400 Subject: [PATCH 31/60] update H1 --- .../fundamentals/program-structure/top-level-statements.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/fundamentals/program-structure/top-level-statements.md b/docs/csharp/fundamentals/program-structure/top-level-statements.md index 1c0d42ae6e053..0bfd95fbc1d1d 100644 --- a/docs/csharp/fundamentals/program-structure/top-level-statements.md +++ b/docs/csharp/fundamentals/program-structure/top-level-statements.md @@ -6,7 +6,7 @@ helpviewer_keywords: - "C# language, top-level statements" - "C# language, Main method" --- -# Top-level statements +# Top-level statements - programs without Main methods Starting in C# 9, you don't have to explicitly include a `Main` method in a console application project. Instead, you can use the *top-level statements* feature to minimize the code you have to write. In this case, the compiler generates a class and `Main` method entry point for the application. From 25577060fcc0c5805278c4408a970119ecfebdca Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 11:39:39 -0400 Subject: [PATCH 32/60] review samples in OO section --- .../fundamentals/object-oriented/index.md | 22 ++--- .../object-oriented/inheritance.md | 10 +- .../fundamentals/object-oriented/objects.md | 75 +++++++------- .../object-oriented/polymorphism.md | 31 +++--- .../object-oriented/snippets/index}/class1.cs | 6 +- .../snippets/index/index.csproj | 8 ++ .../snippets/inheritance}/Inheritance.cs | 38 +------ .../snippets/inheritance/Program.cs | 45 +++++++++ .../snippets/inheritance}/hierarchy.cs | 0 .../snippets/inheritance/inheritance.csproj | 8 ++ .../snippets/objects/Application.cs | 48 +++++++++ .../snippets/objects/Program.cs | 42 ++++++++ .../snippets/objects/objects.csproj | 9 ++ .../csProgGuideStatements/CS/Statements.cs | 98 ------------------- .../snippets/csharp/objectoriented/Program.cs | 4 - 15 files changed, 229 insertions(+), 215 deletions(-) rename {samples/snippets/csharp/programming-guide/classes-and-structs => docs/csharp/fundamentals/object-oriented/snippets/index}/class1.cs (92%) create mode 100644 docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj rename {samples/snippets/csharp/objectoriented => docs/csharp/fundamentals/object-oriented/snippets/inheritance}/Inheritance.cs (82%) create mode 100644 docs/csharp/fundamentals/object-oriented/snippets/inheritance/Program.cs rename {samples/snippets/csharp/objectoriented => docs/csharp/fundamentals/object-oriented/snippets/inheritance}/hierarchy.cs (100%) create mode 100644 docs/csharp/fundamentals/object-oriented/snippets/inheritance/inheritance.csproj create mode 100644 docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs create mode 100644 docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs create mode 100644 docs/csharp/fundamentals/object-oriented/snippets/objects/objects.csproj diff --git a/docs/csharp/fundamentals/object-oriented/index.md b/docs/csharp/fundamentals/object-oriented/index.md index 519667a67a742..5b923959bac5e 100644 --- a/docs/csharp/fundamentals/object-oriented/index.md +++ b/docs/csharp/fundamentals/object-oriented/index.md @@ -1,7 +1,7 @@ --- -title: "Classes, structs, and records - C# Programming Guide" +title: "Classes, structs, and records" description: Describes the use of classes, structures (structs), and records in C#. -ms.date: 04/06/2021 +ms.date: 05/14/2021 helpviewer_keywords: - "structs [C#], about structs" - "records [C#], about records" @@ -12,7 +12,7 @@ helpviewer_keywords: - "objects [C#]" - "C# language, classes" --- -# Classes, structs, and records (C# programming guide) +# Classes, structs, and records Classes and structs are two of the basic constructs of the common type system in .NET. C# 9 adds records, which are a kind of class. Each is essentially a data structure that encapsulates a set of data and behaviors that belong together as a logical unit. The data and behaviors are the *members* of the class, struct, or record, and they include its methods, properties, events, and so on, as listed later in this article. @@ -20,7 +20,7 @@ A class, struct, or record declaration is like a blueprint that is used to creat A class or a record is a reference type. When an object of the type is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data. -A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy. +A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it's copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy don't affect the other copy. In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that isn't intended to be modified after the struct is created. Record types are for larger data structures that contain primarily data that isn't intended to be modified after the object is created. @@ -28,11 +28,11 @@ In general, classes are used to model more complex behavior, or data that is int In the following example, `CustomClass` in the `ProgrammingGuide` namespace has three members: an instance constructor, a property named `Number`, and a method named `Multiply`. The `Main` method in the `Program` class creates an instance (object) of `CustomClass`, and the object's method and property are accessed by using dot notation. - :::code language="csharp" source="../../../../samples/snippets/csharp/programming-guide/classes-and-structs/class1.cs" id="Snippet1"::: + :::code language="csharp" source="./snippets/index/class1.cs" interactive="try-dotnet"::: ## Encapsulation - *Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that are not intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see [Object-oriented programming](./tutorials/oop.md). + *Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that aren't intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see [Object-oriented programming](./tutorials/oop.md). ## Members @@ -53,7 +53,7 @@ The following list includes all the various kinds of members that may be declare ## Accessibility - Some methods and properties are meant to be called or accessed from code outside a class or struct, known as *client code*. Other methods and properties might be only for use in the class or struct itself. It is important to limit the accessibility of your code so that only the intended client code can reach it. You specify how accessible your types and their members are to client code by using the following access modifiers: + Some methods and properties are meant to be called or accessed from code outside a class or struct, known as *client code*. Other methods and properties might be only for use in the class or struct itself. It's important to limit the accessibility of your code so that only the intended client code can reach it. You specify how accessible your types and their members are to client code by using the following access modifiers: - [public](../../language-reference/keywords/public.md) - [protected](../../language-reference/keywords/protected.md) @@ -76,7 +76,7 @@ Classes, structs, and records can inherit multiple interfaces. To inherit from a ## Generic Types - Classes, structs, and records can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example The class in the namespace is defined with one type parameter. Client code creates an instance of a `List` or `List` to specify the type that the list will hold. For more information, see [Generics](../types/generics.md). +Classes, structs, and records can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example,, The class in the namespace is defined with one type parameter. Client code creates an instance of a `List` or `List` to specify the type that the list will hold. For more information, see [Generics](../types/generics.md). ## Static Types @@ -88,7 +88,7 @@ A class, struct, or record can be nested within another class, struct, or record ## Partial Types -You can define part of a class, struct or method in one code file and another part in a separate code file. +You can define part of a class, struct, or method in one code file and another part in a separate code file. ## Object Initializers @@ -96,7 +96,7 @@ You can instantiate and initialize class or struct objects, and collections of o ## Anonymous Types -In situations where it is not convenient or necessary to create a named class, for example when you are populating a list with data structures that you do not have to persist or pass to another method, you use anonymous types. +In situations where it isn't convenient or necessary to create a named class, for example when you're populating a list with data structures that you don't have to persist or pass to another method, you use anonymous types. ## Extension Methods @@ -122,7 +122,7 @@ C# 9 introduces the `record` type, a reference type that you can create instead * Built-in formatting for display. - The `ToString` method prints out the record type name and the names and values of public properties. + The `ToString` method prints the record type name and the names and values of public properties. * Support for inheritance hierarchies. diff --git a/docs/csharp/fundamentals/object-oriented/inheritance.md b/docs/csharp/fundamentals/object-oriented/inheritance.md index d0f9b391390ff..f5d8869bb573f 100644 --- a/docs/csharp/fundamentals/object-oriented/inheritance.md +++ b/docs/csharp/fundamentals/object-oriented/inheritance.md @@ -1,7 +1,7 @@ --- -title: "Inheritance - C# Programming Guide" +title: "Inheritance" description: Inheritance in C# enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. -ms.date: 02/07/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "abstract methods [C#]" - "abstract classes [C#]" @@ -11,7 +11,7 @@ helpviewer_keywords: - "C# language, inheritance" ms.assetid: 81d64ee4-50f9-4d6c-a8dc-257c348d2eea --- -# Inheritance (C# Programming Guide) +# Inheritance - derive types to create more specialized behavior Inheritance, together with encapsulation and polymorphism, is one of the three primary characteristics of object-oriented programming. Inheritance enables you to create new classes that reuse, extend, and modify the behavior defined in other classes. The class whose members are inherited is called the *base class*, and the class that inherits those members is called the *derived class*. A derived class can have only one direct base class. However, inheritance is transitive. If `ClassC` is derived from `ClassB`, and `ClassB` is derived from `ClassA`, `ClassC` inherits the members declared in `ClassB` and `ClassA`. @@ -30,11 +30,11 @@ The following illustration shows a class `WorkItem` that represents an item of w The following example shows how the class relationships demonstrated in the previous illustration are expressed in C#. The example also shows how `WorkItem` overrides the virtual method , and how the `ChangeRequest` class inherits the `WorkItem` implementation of the method. The first block defines the classes: -[!code-csharp[DefineClasses](~/samples/snippets/csharp/objectoriented/inheritance.cs#Classes)] +:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetClasses"::: This next block shows how to use the base and derived classes: -[!code-csharp[UseClasses](~/samples/snippets/csharp/objectoriented/inheritance.cs#UseClasses)] +:::code language="csharp" source="./snippets/inheritance/Program.cs" ID="SnippetUseClasses"::: ## Abstract and virtual methods diff --git a/docs/csharp/fundamentals/object-oriented/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md index bd7f141ada088..839b7930abed7 100644 --- a/docs/csharp/fundamentals/object-oriented/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -1,61 +1,56 @@ --- -title: "Objects - C# Programming Guide" +title: "Objects - create instances of types" description: C# uses a class or struct definition to define types of objects. In an object-oriented language such as C#, a program consists of objects interacting dynamically. -ms.date: 02/03/2021 +ms.date: 05/14/2021 helpviewer_keywords: - "objects [C#], about objects" - "variables [C#]" -ms.assetid: af4a5230-fbf3-4eea-95e1-8b883c2f845c --- -# Objects (C# Programming Guide) +# Objects - create instances of types + +A class or struct definition is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection. Client code is the code that uses these variables to call the methods and access the public properties of the object. In an object-oriented language such as C#, a typical program consists of multiple objects interacting dynamically. -A class or struct definition is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection. Client code is the code that uses these variables to call the methods and access the public properties of the object. In an object-oriented language such as C#, a typical program consists of multiple objects interacting dynamically. - > [!NOTE] > Static types behave differently than what is described here. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md). - -## Struct Instances vs. Class Instances - - Because classes are reference types, a variable of a class object holds a reference to the address of the object on the managed heap. If a second object of the same type is assigned to the first object, then both variables refer to the object at that address. This point is discussed in more detail later in this topic. - - Instances of classes are created by using the [new operator](../../language-reference/operators/new-operator.md). In the following example, `Person` is the type and `person1` and `person2` are instances, or objects, of that type. - - [!code-csharp[csProgGuideStatements#30](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#30)] - - Because structs are value types, a variable of a struct object holds a copy of the entire object. Instances of structs can also be created by using the `new` operator, but this is not required, as shown in the following example: - - [!code-csharp[csProgGuideStatements#31](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#31)] - - The memory for both `p1` and `p2` is allocated on the thread stack. That memory is reclaimed along with the type or method in which it is declared. This is one reason why structs are copied on assignment. By contrast, the memory that is allocated for a class instance is automatically reclaimed (garbage collected) by the common language runtime when all references to the object have gone out of scope. It is not possible to deterministically destroy a class object like you can in C++. For more information about garbage collection in .NET, see [Garbage Collection](../../../standard/garbage-collection/index.md). - + +## Struct Instances vs. Class Instances + +Because classes are reference types, a variable of a class object holds a reference to the address of the object on the managed heap. If a second object of the same type is assigned to the first object, then both variables refer to the object at that address. This point is discussed in more detail later in this article. + +Instances of classes are created by using the [`new` operator](../../language-reference/operators/new-operator.md). In the following example, `Person` is the type and `person1` and `person2` are instances, or objects, of that type. + +:::code language="csharp" source="./snippets/objects/Program.cs" interactive="try-dotnet"::: + +Because structs are value types, a variable of a struct object holds a copy of the entire object. Instances of structs can also be created by using the `new` operator, but this isn't required, as shown in the following example: + +:::code language="csharp" source="./snippets/objects/Application.cs" interactive="try-dotnet"::: + +The memory for both `p1` and `p2` is allocated on the thread stack. That memory is reclaimed along with the type or method in which it's declared. This is one reason why structs are copied on assignment. By contrast, the memory that is allocated for a class instance is automatically reclaimed (garbage collected) by the common language runtime when all references to the object have gone out of scope. It isn't possible to deterministically destroy a class object like you can in C++. For more information about garbage collection in .NET, see [Garbage Collection](../../../standard/garbage-collection/index.md). + > [!NOTE] > The allocation and deallocation of memory on the managed heap is highly optimized in the common language runtime. In most cases there is no significant difference in the performance cost of allocating a class instance on the heap versus allocating a struct instance on the stack. - -## Object Identity vs. Value Equality - - When you compare two objects for equality, you must first distinguish whether you want to know whether the two variables represent the same object in memory, or whether the values of one or more of their fields are equivalent. If you are intending to compare values, you must consider whether the objects are instances of value types (structs) or reference types (classes, delegates, arrays). - -- To determine whether two class instances refer to the same location in memory (which means that they have the same *identity*), use the static method. ( is the implicit base class for all value types and reference types, including user-defined structs and classes.) - -- To determine whether the instance fields in two struct instances have the same values, use the method. Because all structs implicitly inherit from , you call the method directly on your object as shown in the following example: - - [!code-csharp[csProgGuideStatements#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#32)] - - The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) + +## Object Identity vs. Value Equality + +When you compare two objects for equality, you must first distinguish whether you want to know whether the two variables represent the same object in memory, or whether the values of one or more of their fields are equivalent. If you're intending to compare values, you must consider whether the objects are instances of value types (structs) or reference types (classes, delegates, arrays). + +- To determine whether two class instances refer to the same location in memory (which means that they have the same *identity*), use the static method. ( is the implicit base class for all value types and reference types, including user-defined structs and classes.) +- To determine whether the instance fields in two struct instances have the same values, use the method. Because all structs implicitly inherit from , you call the method directly on your object as shown in the following example: + + [!code-csharp[csProgGuideStatements#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#32)] + + The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) - To determine whether the values of the fields in two class instances are equal, you might be able to use the method or the [== operator](../../language-reference/operators/equality-operators.md#equality-operator-). However, only use them if the class has overridden or overloaded them to provide a custom definition of what "equality" means for objects of that type. The class might also implement the interface or the interface. Both interfaces provide methods that can be used to test value equality. When designing your own classes that override `Equals`, make sure to follow the guidelines stated in [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) and . - -## Related Sections - For more information: - +## Related Sections + +For more information: + - [Classes](../../programming-guide/classes-and-structs/classes.md) - [Constructors](../../programming-guide/classes-and-structs/constructors.md) - [Finalizers](../../programming-guide/classes-and-structs/destructors.md) - [Events](../../programming-guide/events/index.md) - -## See also - - [object](../../language-reference/builtin-types/reference-types.md) - [Inheritance](./inheritance.md) - [class](../../language-reference/keywords/class.md) diff --git a/docs/csharp/fundamentals/object-oriented/polymorphism.md b/docs/csharp/fundamentals/object-oriented/polymorphism.md index 809a65b09808b..a4b021646f712 100644 --- a/docs/csharp/fundamentals/object-oriented/polymorphism.md +++ b/docs/csharp/fundamentals/object-oriented/polymorphism.md @@ -1,31 +1,30 @@ --- -title: "Polymorphism - C# Programming Guide" +title: "Polymorphism" description: Learn about polymorphism, a key concept in object-oriented programming languages like C#, which describes the relationship between base and derived classes. -ms.date: 02/08/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "C# language, polymorphism" - "polymorphism [C#]" -ms.assetid: 086af969-29a5-4ce8-a993-0b7d53839dab --- -# Polymorphism (C# Programming Guide) +# Polymorphism Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word that means "many-shaped" and it has two distinct aspects: - + - At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this polymorphism occurs, the object's declared type is no longer identical to its run-time type. - Base classes may define and implement [virtual](../../language-reference/keywords/virtual.md) *methods*, and derived classes can [override](../../language-reference/keywords/override.md) them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. In your source code you can call a method on a base class, and cause a derived class's version of the method to be executed. -Virtual methods enable you to work with groups of related objects in a uniform way. For example, suppose you have a drawing application that enables a user to create various kinds of shapes on a drawing surface. You do not know at compile time which specific types of shapes the user will create. However, the application has to keep track of all the various types of shapes that are created, and it has to update them in response to user mouse actions. You can use polymorphism to solve this problem in two basic steps: +Virtual methods enable you to work with groups of related objects in a uniform way. For example, suppose you have a drawing application that enables a user to create various kinds of shapes on a drawing surface. You don't know at compile time which specific types of shapes the user will create. However, the application has to keep track of all the various types of shapes that are created, and it has to update them in response to user mouse actions. You can use polymorphism to solve this problem in two basic steps: 1. Create a class hierarchy in which each specific shape class derives from a common base class. 1. Use a virtual method to invoke the appropriate method on any derived class through a single call to the base class method. First, create a base class called `Shape`, and derived classes such as `Rectangle`, `Circle`, and `Triangle`. Give the `Shape` class a virtual method called `Draw`, and override it in each derived class to draw the particular shape that the class represents. Create a `List` object and add a `Circle`, `Triangle`, and `Rectangle` to it. -[!code-csharp[Polymorphism overview](~/samples/snippets/csharp/objectoriented/Inheritance.cs#PolymorphismOverview)] +:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetPolymorphismOverview"::: To update the drawing surface, use a [foreach](../../language-reference/keywords/foreach-in.md) loop to iterate through the list and call the `Draw` method on each `Shape` object in the list. Even though each object in the list has a declared type of `Shape`, it's the run-time type (the overridden version of the method in each derived class) that will be invoked. -[!code-csharp[Polymorphism overview](~/samples/snippets/csharp/objectoriented/Inheritance.cs#UsePolymorphism)] +:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetUsePolymorphism"::: In C#, every type is polymorphic because all types, including user-defined types, inherit from . @@ -41,11 +40,11 @@ When a derived class inherits from a base class, it gains all the methods, field A derived class can override a base class member only if the base class member is declared as [virtual](../../language-reference/keywords/virtual.md) or [abstract](../../language-reference/keywords/abstract.md). The derived member must use the [override](../../language-reference/keywords/override.md) keyword to explicitly indicate that the method is intended to participate in virtual invocation. The following code provides an example: -[!code-csharp[Virtual overview](~/samples/snippets/csharp/objectoriented/Inheritance.cs#VirtualMethods)] +:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetVirtualMethods"::: -Fields cannot be virtual; only methods, properties, events, and indexers can be virtual. When a derived class overrides a virtual member, that member is called even when an instance of that class is being accessed as an instance of the base class. The following code provides an example: +Fields can't be virtual; only methods, properties, events, and indexers can be virtual. When a derived class overrides a virtual member, that member is called even when an instance of that class is being accessed as an instance of the base class. The following code provides an example: -[!code-csharp[Virtual overview example](~/samples/snippets/csharp/objectoriented/Inheritance.cs#SnippetTestVirtualMethods)] +:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetTestVirtualMethods"::: Virtual methods and properties enable derived classes to extend a base class without needing to use the base class implementation of a method. For more information, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md). An interface provides another way to define a method or set of methods whose implementation is left to derived classes. @@ -53,25 +52,25 @@ Virtual methods and properties enable derived classes to extend a base class wit If you want your derived class to have a member with the same name as a member in a base class, you can use the [new](../../language-reference/keywords/new-modifier.md) keyword to hide the base class member. The `new` keyword is put before the return type of a class member that is being replaced. The following code provides an example: -[!code-csharp[New method overview example](~/samples/snippets/csharp/objectoriented/Inheritance.cs#NewMethods)] +:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetNewMethods"::: Hidden base class members may be accessed from client code by casting the instance of the derived class to an instance of the base class. For example: -[!code-csharp[New method overview usage](~/samples/snippets/csharp/objectoriented/Inheritance.cs#UseNewMethods)] +:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetUseNewMethods"::: ### Prevent derived classes from overriding virtual members Virtual members remain virtual, regardless of how many classes have been declared between the virtual member and the class that originally declared it. If class `A` declares a virtual member, and class `B` derives from `A`, and class `C` derives from `B`, class `C` inherits the virtual member, and may override it, regardless of whether class `B` declared an override for that member. The following code provides an example: -[!code-csharp[Basic hierarchy](~/samples/snippets/csharp/objectoriented/Hierarchy.cs#FirstHierarchy)] +:::code language="csharp" source="./snippets/inheritance/Hierarchy.cs" ID="SnippetFirstHierarchy"::: A derived class can stop virtual inheritance by declaring an override as [sealed](../../language-reference/keywords/sealed.md). Stopping inheritance requires putting the `sealed` keyword before the `override` keyword in the class member declaration. The following code provides an example: -[!code-csharp[A sealed overridden member](~/samples/snippets/csharp/objectoriented/Hierarchy.cs#SealedOverride)] +:::code language="csharp" source="./snippets/inheritance/Hierarchy.cs" ID="SnippetSealedOverride"::: In the previous example, the method `DoWork` is no longer virtual to any class derived from `C`. It's still virtual for instances of `C`, even if they're cast to type `B` or type `A`. Sealed methods can be replaced by derived classes by using the `new` keyword, as the following example shows: -[!code-csharp[New method declaration](~/samples/snippets/csharp/objectoriented/Hierarchy.cs#NewDeclaration)] +:::code language="csharp" source="./snippets/inheritance/Hierarchy.cs" ID="SnippetNewDeclaration"::: In this case, if `DoWork` is called on `D` using a variable of type `D`, the new `DoWork` is called. If a variable of type `C`, `B`, or `A` is used to access an instance of `D`, a call to `DoWork` will follow the rules of virtual inheritance, routing those calls to the implementation of `DoWork` on class `C`. diff --git a/samples/snippets/csharp/programming-guide/classes-and-structs/class1.cs b/docs/csharp/fundamentals/object-oriented/snippets/index/class1.cs similarity index 92% rename from samples/snippets/csharp/programming-guide/classes-and-structs/class1.cs rename to docs/csharp/fundamentals/object-oriented/snippets/index/class1.cs index 5519b441bdf95..c3b29cca1966a 100644 --- a/samples/snippets/csharp/programming-guide/classes-and-structs/class1.cs +++ b/docs/csharp/fundamentals/object-oriented/snippets/index/class1.cs @@ -1,7 +1,6 @@ -// -using System; +using System; -namespace ProgrammingGuide +namespace Fundamentals { // Class definition. public class CustomClass @@ -43,4 +42,3 @@ static void Main(string[] args) } // The example displays the following output: // The result is 108. -// diff --git a/docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj b/docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj new file mode 100644 index 0000000000000..2082704286ba4 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/samples/snippets/csharp/objectoriented/Inheritance.cs b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Inheritance.cs similarity index 82% rename from samples/snippets/csharp/objectoriented/Inheritance.cs rename to docs/csharp/fundamentals/object-oriented/snippets/inheritance/Inheritance.cs index 13a52b6262ae4..a9b200dabbf7a 100644 --- a/samples/snippets/csharp/objectoriented/Inheritance.cs +++ b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Inheritance.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; -namespace objectoriented +namespace inheritance { // // WorkItem implicitly inherits from the Object class. @@ -92,7 +92,6 @@ public ChangeRequest(string title, string desc, TimeSpan jobLen, } // - // public class Shape { @@ -241,39 +240,4 @@ public static void Example() } } - static class Inheritance - { - public static void Example() - { - // - // Create an instance of WorkItem by using the constructor in the - // base class that takes three arguments. - WorkItem item = new WorkItem("Fix Bugs", - "Fix all bugs in my code branch", - new TimeSpan(3, 4, 0, 0)); - - // Create an instance of ChangeRequest by using the constructor in - // the derived class that takes four arguments. - ChangeRequest change = new ChangeRequest("Change Base Class Design", - "Add members to the class", - new TimeSpan(4, 0, 0), - 1); - - // Use the ToString method defined in WorkItem. - Console.WriteLine(item.ToString()); - - // Use the inherited Update method to change the title of the - // ChangeRequest object. - change.Update("Change the Design of the Base Class", - new TimeSpan(4, 0, 0)); - - // ChangeRequest inherits WorkItem's override of ToString. - Console.WriteLine(change.ToString()); - /* Output: - 1 - Fix Bugs - 2 - Change the Design of the Base Class - */ - // - } - } } diff --git a/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Program.cs b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Program.cs new file mode 100644 index 0000000000000..b4b54be33c550 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Program.cs @@ -0,0 +1,45 @@ +using System; + +namespace inheritance +{ + class Program + { + static void Main(string[] args) + { + // + // Create an instance of WorkItem by using the constructor in the + // base class that takes three arguments. + WorkItem item = new WorkItem("Fix Bugs", + "Fix all bugs in my code branch", + new TimeSpan(3, 4, 0, 0)); + + // Create an instance of ChangeRequest by using the constructor in + // the derived class that takes four arguments. + ChangeRequest change = new ChangeRequest("Change Base Class Design", + "Add members to the class", + new TimeSpan(4, 0, 0), + 1); + + // Use the ToString method defined in WorkItem. + Console.WriteLine(item.ToString()); + + // Use the inherited Update method to change the title of the + // ChangeRequest object. + change.Update("Change the Design of the Base Class", + new TimeSpan(4, 0, 0)); + + // ChangeRequest inherits WorkItem's override of ToString. + Console.WriteLine(change.ToString()); + /* Output: + 1 - Fix Bugs + 2 - Change the Design of the Base Class + */ + // + + Polymorphism.Example(); + Polymorphism.VirtualExamples(); + NewMethodHierarchy.NewMethods.Example(); + + } + } +} diff --git a/samples/snippets/csharp/objectoriented/hierarchy.cs b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/hierarchy.cs similarity index 100% rename from samples/snippets/csharp/objectoriented/hierarchy.cs rename to docs/csharp/fundamentals/object-oriented/snippets/inheritance/hierarchy.cs diff --git a/docs/csharp/fundamentals/object-oriented/snippets/inheritance/inheritance.csproj b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/inheritance.csproj new file mode 100644 index 0000000000000..2082704286ba4 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/inheritance.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs b/docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs new file mode 100644 index 0000000000000..569c1ff97d9c4 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs @@ -0,0 +1,48 @@ +using System; + +namespace Example +{ + public struct Person + { + public string Name; + public int Age; + public Person(string name, int age) + { + Name = name; + Age = age; + } + } + + public class Application + { + static void Main() + { + // Create struct instance and initialize by using "new". + // Memory is allocated on thread stack. + Person p1 = new Person("Alex", 9); + Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age); + + // Create new struct object. Note that struct can be initialized + // without using "new". + Person p2 = p1; + + // Assign values to p2 members. + p2.Name = "Spencer"; + p2.Age = 7; + Console.WriteLine("p2 Name = {0} Age = {1}", p2.Name, p2.Age); + + // p1 values remain unchanged because p2 is copy. + Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age); + + // Keep the console open in debug mode. + Console.WriteLine("Press any key to exit."); + Console.ReadKey(); + } + } + /* + Output: + p1 Name = Alex Age = 9 + p2 Name = Spencer Age = 7 + p1 Name = Alex Age = 9 + */ +} diff --git a/docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs b/docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs new file mode 100644 index 0000000000000..7e88a60df6a63 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs @@ -0,0 +1,42 @@ +using System; + +public class Person +{ + public string Name { get; set; } + public int Age { get; set; } + public Person(string name, int age) + { + Name = name; + Age = age; + } + // Other properties, methods, events... +} + +class Program +{ + static void Main() + { + Person person1 = new Person("Leopold", 6); + Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age); + + // Declare new person, assign person1 to it. + Person person2 = person1; + + // Change the name of person2, and person1 also changes. + person2.Name = "Molly"; + person2.Age = 16; + + Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age); + Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age); + + // Keep the console open in debug mode. + Console.WriteLine("Press any key to exit."); + Console.ReadKey(); + } +} +/* + Output: + person1 Name = Leopold Age = 6 + person2 Name = Molly Age = 16 + person1 Name = Molly Age = 16 +*/ diff --git a/docs/csharp/fundamentals/object-oriented/snippets/objects/objects.csproj b/docs/csharp/fundamentals/object-oriented/snippets/objects/objects.csproj new file mode 100644 index 0000000000000..8ad4cdb45a19e --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/snippets/objects/objects.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Program + + + diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs index 800857038c313..e362c8a2dc86b 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs @@ -652,106 +652,8 @@ string S() } } - namespace ObjectsTopic - { - using System; - - // - public class Person - { - public string Name { get; set; } - public int Age { get; set; } - public Person(string name, int age) - { - Name = name; - Age = age; - } - // Other properties, methods, events... - } - - class Program - { - static void Main() - { - Person person1 = new Person("Leopold", 6); - Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age); - - // Declare new person, assign person1 to it. - Person person2 = person1; - - // Change the name of person2, and person1 also changes. - person2.Name = "Molly"; - person2.Age = 16; - - Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age); - Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age); - - // Keep the console open in debug mode. - Console.WriteLine("Press any key to exit."); - Console.ReadKey(); - } - } - /* - Output: - person1 Name = Leopold Age = 6 - person2 Name = Molly Age = 16 - person1 Name = Molly Age = 16 - */ - // - } - - // Separate namespace to have struct and class by same name in code examples - // for comparison in Objects (C# PRogramming Guide) namespace ObjectsTopic2 { - using System; - - // - public struct Person - { - public string Name; - public int Age; - public Person(string name, int age) - { - Name = name; - Age = age; - } - } - - public class Application - { - static void Main() - { - // Create struct instance and initialize by using "new". - // Memory is allocated on thread stack. - Person p1 = new Person("Alex", 9); - Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age); - - // Create new struct object. Note that struct can be initialized - // without using "new". - Person p2 = p1; - - // Assign values to p2 members. - p2.Name = "Spencer"; - p2.Age = 7; - Console.WriteLine("p2 Name = {0} Age = {1}", p2.Name, p2.Age); - - // p1 values remain unchanged because p2 is copy. - Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age); - - // Keep the console open in debug mode. - Console.WriteLine("Press any key to exit."); - Console.ReadKey(); - } - } - /* - Output: - p1 Name = Alex Age = 9 - p2 Name = Spencer Age = 7 - p1 Name = Alex Age = 9 - */ - // - class Equality { static void Main() diff --git a/samples/snippets/csharp/objectoriented/Program.cs b/samples/snippets/csharp/objectoriented/Program.cs index ffbf475bbe2e7..0fdd9aa7fc7a2 100644 --- a/samples/snippets/csharp/objectoriented/Program.cs +++ b/samples/snippets/csharp/objectoriented/Program.cs @@ -6,11 +6,7 @@ class Program { static void Main(string[] args) { - Inheritance.Example(); NestedTypes.Example(); - Polymorphism.Example(); - Polymorphism.VirtualExamples(); - NewMethodHierarchy.NewMethods.Example(); } } } From 0c863aefa19893349cf9a31deda5243f2b63bbb2 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 11:50:39 -0400 Subject: [PATCH 33/60] finish oo tutorials --- .../object-oriented/tutorials/classes.md | 5 +-- .../object-oriented/tutorials/oop.md | 2 +- ...ng-pattern-matching-is-and-as-operators.md | 12 +++--- .../snippets}/safelycast/asandis/Program.cs | 0 .../safelycast/asandis/asandis.csproj | 0 .../nullablepatternmatching/Program.cs | 0 .../nullablepatternmatching.csproj | 0 .../safelycast/patternmatching/Program.cs | 0 .../patternmatching/patternmatching.csproj | 0 .../csharp/how-to/safelycast/safelycast.sln | 37 ------------------- 10 files changed, 9 insertions(+), 47 deletions(-) rename {samples/snippets/csharp/how-to => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/safelycast/asandis/Program.cs (100%) rename {samples/snippets/csharp/how-to => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/safelycast/asandis/asandis.csproj (100%) rename {samples/snippets/csharp/how-to => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/safelycast/nullablepatternmatching/Program.cs (100%) rename {samples/snippets/csharp/how-to => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/safelycast/nullablepatternmatching/nullablepatternmatching.csproj (100%) rename {samples/snippets/csharp/how-to => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/safelycast/patternmatching/Program.cs (100%) rename {samples/snippets/csharp/how-to => docs/csharp/fundamentals/object-oriented/tutorials/snippets}/safelycast/patternmatching/patternmatching.csproj (100%) delete mode 100644 samples/snippets/csharp/how-to/safelycast/safelycast.sln diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/classes.md b/docs/csharp/fundamentals/object-oriented/tutorials/classes.md index abca66b1fb1c9..55b3f02c80440 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/classes.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/classes.md @@ -1,8 +1,7 @@ --- -title: Classes and objects - Introduction to C# tutorial +title: Classes and objects - C# Fundamentals tutorial description: Create your first C# program and explore object oriented concepts -ms.date: 10/11/2017 -ms.custom: mvc +ms.date: 05/14/2021 --- # Explore object oriented programming with classes and objects diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/oop.md b/docs/csharp/fundamentals/object-oriented/tutorials/oop.md index 98bdd1d4a98e6..693400c982525 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/oop.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/oop.md @@ -1,7 +1,7 @@ --- title: "Object-Oriented Programming (C#)" description: C# provides full support for object-oriented programming including abstraction, encapsulation, inheritance, and polymorphism. -ms.date: 09/30/2020 +ms.date: 05/14/2021 --- # Object-Oriented programming (C#) diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md index 68cf18294542d..08f3e8564da3d 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md @@ -1,7 +1,7 @@ --- title: "How to safely cast by using pattern matching and the is and as operators" description: Learn to use pattern matching techniques to safely cast variables to a different type. You can use pattern matching as well as the is and as operators to safely convert types. -ms.date: 09/05/2018 +ms.date: 05/14/2021 helpviewer_keywords: - "cast operators [C#], as and is operators" - "as operator [C#]" @@ -13,20 +13,20 @@ Because objects are polymorphic, it's possible for a variable of a base class ty The following example shows how to use the pattern matching `is` statement: -:::code language="csharp" source="../../../../../samples/snippets/csharp/how-to/safelycast/patternmatching/Program.cs" id="PatternMatchingIs"::: +:::code language="csharp" source="./snippets/safelycast/patternmatching/Program.cs" ID="PatternMatchingIs"::: -The preceding sample demonstrates a few features of pattern matching syntax. The `if (a is Mammal m)` statement combines the test with an initialization assignment. The assignment occurs only when the test succeeds. The variable `m` is only in scope in the embedded `if` statement where it has been assigned. You cannot access `m` later in the same method. The preceding example also shows how to use the [`as` operator](../../../language-reference/operators/type-testing-and-cast.md#as-operator) to convert an object to a specified type. +The preceding sample demonstrates a few features of pattern matching syntax. The `if (a is Mammal m)` statement combines the test with an initialization assignment. The assignment occurs only when the test succeeds. The variable `m` is only in scope in the embedded `if` statement where it has been assigned. You can't access `m` later in the same method. The preceding example also shows how to use the [`as` operator](../../../language-reference/operators/type-testing-and-cast.md#as-operator) to convert an object to a specified type. You can also use the same syntax for testing if a [nullable value type](../../../language-reference/builtin-types/nullable-value-types.md) has a value, as shown in the following example: -:::code language="csharp" source="../../../../../samples/snippets/csharp/how-to/safelycast/nullablepatternmatching/Program.cs" id="PatternMatchingNullable"::: +:::code language="csharp" source="./snippets/safelycast/nullablepatternmatching/Program.cs" ID="PatternMatchingNullable"::: -The preceding sample demonstrates other features of pattern matching to use with conversions. You can test a variable for the null pattern by checking specifically for the `null` value. When the runtime value of the variable is `null`, an `is` statement checking for a type always returns `false`. The pattern matching `is` statement doesn't allow a nullable value type, such as `int?` or `Nullable`, but you can test for any other value type. The `is` patterns from the preceding example are not limited to the nullable value types. You can also use those patterns to test if a variable of a reference type has a value or it's `null`. +The preceding sample demonstrates other features of pattern matching to use with conversions. You can test a variable for the null pattern by checking specifically for the `null` value. When the runtime value of the variable is `null`, an `is` statement checking for a type always returns `false`. The pattern matching `is` statement doesn't allow a nullable value type, such as `int?` or `Nullable`, but you can test for any other value type. The `is` patterns from the preceding example aren't limited to the nullable value types. You can also use those patterns to test if a variable of a reference type has a value or it's `null`. The preceding sample also shows how you use the type pattern in a `switch` statement where the variable may be one of many different types. If you want to test if a variable is a given type, but not assign it to a new variable, you can use the `is` and `as` operators for reference types and nullable value types. The following code shows how to use the `is` and `as` statements that were part of the C# language before pattern matching was introduced to test if a variable is of a given type: -:::code language="csharp" source="../../../../../samples/snippets/csharp/how-to/safelycast/asandis/Program.cs" id="IsAndAs"::: +:::code language="csharp" source="./snippets/safelycast/asandis/Program.cs" ID="IsAndAs"::: As you can see by comparing this code with the pattern matching code, the pattern matching syntax provides more robust features by combining the test and the assignment in a single statement. Use the pattern matching syntax whenever possible. diff --git a/samples/snippets/csharp/how-to/safelycast/asandis/Program.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/Program.cs similarity index 100% rename from samples/snippets/csharp/how-to/safelycast/asandis/Program.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/Program.cs diff --git a/samples/snippets/csharp/how-to/safelycast/asandis/asandis.csproj b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/asandis.csproj similarity index 100% rename from samples/snippets/csharp/how-to/safelycast/asandis/asandis.csproj rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/asandis.csproj diff --git a/samples/snippets/csharp/how-to/safelycast/nullablepatternmatching/Program.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/Program.cs similarity index 100% rename from samples/snippets/csharp/how-to/safelycast/nullablepatternmatching/Program.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/Program.cs diff --git a/samples/snippets/csharp/how-to/safelycast/nullablepatternmatching/nullablepatternmatching.csproj b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/nullablepatternmatching.csproj similarity index 100% rename from samples/snippets/csharp/how-to/safelycast/nullablepatternmatching/nullablepatternmatching.csproj rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/nullablepatternmatching.csproj diff --git a/samples/snippets/csharp/how-to/safelycast/patternmatching/Program.cs b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/Program.cs similarity index 100% rename from samples/snippets/csharp/how-to/safelycast/patternmatching/Program.cs rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/Program.cs diff --git a/samples/snippets/csharp/how-to/safelycast/patternmatching/patternmatching.csproj b/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/patternmatching.csproj similarity index 100% rename from samples/snippets/csharp/how-to/safelycast/patternmatching/patternmatching.csproj rename to docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/patternmatching.csproj diff --git a/samples/snippets/csharp/how-to/safelycast/safelycast.sln b/samples/snippets/csharp/how-to/safelycast/safelycast.sln deleted file mode 100644 index 85a01dc782be2..0000000000000 --- a/samples/snippets/csharp/how-to/safelycast/safelycast.sln +++ /dev/null @@ -1,37 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28029.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "patternmatching", "patternmatching\patternmatching.csproj", "{64F025C1-F843-4E18-8E70-D1B49C723035}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "nullablepatternmatching", "nullablepatternmatching\nullablepatternmatching.csproj", "{A19B27AC-36D2-496A-9F73-8122758ECAF3}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "asandis", "asandis\asandis.csproj", "{EF74E2FF-B274-4688-BCA0-25DE80541931}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {64F025C1-F843-4E18-8E70-D1B49C723035}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {64F025C1-F843-4E18-8E70-D1B49C723035}.Debug|Any CPU.Build.0 = Debug|Any CPU - {64F025C1-F843-4E18-8E70-D1B49C723035}.Release|Any CPU.ActiveCfg = Release|Any CPU - {64F025C1-F843-4E18-8E70-D1B49C723035}.Release|Any CPU.Build.0 = Release|Any CPU - {A19B27AC-36D2-496A-9F73-8122758ECAF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A19B27AC-36D2-496A-9F73-8122758ECAF3}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A19B27AC-36D2-496A-9F73-8122758ECAF3}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A19B27AC-36D2-496A-9F73-8122758ECAF3}.Release|Any CPU.Build.0 = Release|Any CPU - {EF74E2FF-B274-4688-BCA0-25DE80541931}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EF74E2FF-B274-4688-BCA0-25DE80541931}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EF74E2FF-B274-4688-BCA0-25DE80541931}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EF74E2FF-B274-4688-BCA0-25DE80541931}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {FE13E063-992C-4F4A-95C8-0DB5A9B9D35F} - EndGlobalSection -EndGlobal From f9a2d2a3d1ee04755ea8273f5b1da405d6f8f126 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 12:05:48 -0400 Subject: [PATCH 34/60] fix issues filed on these articles --- .../object-oriented/snippets/inheritance/Inheritance.cs | 2 +- .../fundamentals/object-oriented/tutorials/classes.md | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Inheritance.cs b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Inheritance.cs index a9b200dabbf7a..08301e65c314f 100644 --- a/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Inheritance.cs +++ b/docs/csharp/fundamentals/object-oriented/snippets/inheritance/Inheritance.cs @@ -176,7 +176,7 @@ public static void VirtualExamples() DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method. - BaseClass A = (BaseClass)B; + BaseClass A = B; A.DoWork(); // Also calls the new method. // } diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/classes.md b/docs/csharp/fundamentals/object-oriented/tutorials/classes.md index 55b3f02c80440..7ecd00cf625d6 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/classes.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/classes.md @@ -138,7 +138,7 @@ This example shows an important aspect of ***properties***. You're now computing Next, implement the `MakeDeposit` and `MakeWithdrawal` methods. These methods will enforce the final two rules: that the initial balance must be positive, and that any withdrawal must not create a negative balance. -This introduces the concept of ***exceptions***. The standard way of indicating that a method cannot complete its work successfully is to throw an exception. The type of exception and the message associated with it describe the error. Here, the `MakeDeposit` method throws an exception if the amount of the deposit is negative. The `MakeWithdrawal` method throws an exception if the withdrawal amount is negative, or if applying the withdrawal results in a negative balance. Add the following code after the declaration of the `allTransactions` list: +This introduces the concept of ***exceptions***. The standard way of indicating that a method cannot complete its work successfully is to throw an exception. The type of exception and the message associated with it describe the error. Here, the `MakeDeposit` method throws an exception if the amount of the deposit is not greater than 0. The `MakeWithdrawal` method throws an exception if the withdrawal amount is not greater than 0, or if applying the withdrawal results in a negative balance. Add the following code after the declaration of the `allTransactions` list: :::code language="csharp" source="./snippets/introduction-to-classes/BankAccount.cs" id="DepositAndWithdrawal"::: @@ -161,14 +161,16 @@ Next, test that you are catching error conditions by trying to create an account ```csharp // Test that the initial balances must be positive. +BankAccount invalidAccount; try { - var invalidAccount = new BankAccount("invalid", -55); + invalidAccount = new BankAccount("invalid", -55); } catch (ArgumentOutOfRangeException e) { Console.WriteLine("Exception caught creating account with negative balance"); Console.WriteLine(e.ToString()); + return; } ``` From 3d422af88c7635d3d51ea99eb34f8fbf46bed0c8 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 15:18:45 -0400 Subject: [PATCH 35/60] funtional is in place. --- .../fundamentals/functional/deconstruct.md | 32 +++++++++---------- .../fundamentals/functional/discards.md | 6 ++-- .../functional/pattern-matching.md | 11 ++++--- .../snippets/patterns/OrderProcessor.cs | 20 ++++++------ .../functional/snippets/patterns/Program.cs | 25 +++++++++------ .../functional/tutorials/pattern-matching.md | 10 +++--- 6 files changed, 56 insertions(+), 48 deletions(-) diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index 42adb8efcd307..dfc26767433be 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -6,17 +6,17 @@ ms.date: 03/22/2021 --- # Deconstructing tuples and other types -A tuple provides a lightweight way to retrieve multiple values from a method call. But once you retrieve the tuple, you have to handle its individual elements. Doing this on an element-by-element basis is cumbersome, as the following example shows. The `QueryCityData` method returns a 3-tuple, and each of its elements is assigned to a variable in a separate operation. +A tuple provides a lightweight way to retrieve multiple values from a method call. But once you retrieve the tuple, you have to handle its individual elements. Working on an element-by-element basis is cumbersome, as the following example shows. The `QueryCityData` method returns a three-tuple, and each of its elements is assigned to a variable in a separate operation. :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple1.cs"::: -Retrieving multiple field and property values from an object can be equally cumbersome: you have to assign a field or property value to a variable on a member-by-member basis. +Retrieving multiple field and property values from an object can be equally cumbersome: you must assign a field or property value to a variable on a member-by-member basis. -Starting with C# 7.0, you can retrieve multiple elements from a tuple or retrieve multiple field, property, and computed values from an object in a single *deconstruct* operation. When you deconstruct a tuple, you assign its elements to individual variables. When you deconstruct an object, you assign selected values to individual variables. +In C# 7.0 and later, you can retrieve multiple elements from a tuple or retrieve multiple field, property, and computed values from an object in a single *deconstruct* operation. To deconstruct a tuple, you assign its elements to individual variables. When you deconstruct an object, you assign selected values to individual variables. ## Deconstructing a tuple -C# features built-in support for deconstructing tuples, which lets you unpackage all the items in a tuple in a single operation. The general syntax for deconstructing a tuple is similar to the syntax for defining one: you enclose the variables to which each element is to be assigned in parentheses in the left side of an assignment statement. For example, the following statement assigns the elements of a 4-tuple to four separate variables: +C# features built-in support for deconstructing tuples, which lets you unpackage all the items in a tuple in a single operation. The general syntax for deconstructing a tuple is similar to the syntax for defining one: you enclose the variables to which each element is to be assigned in parentheses in the left side of an assignment statement. For example, the following statement assigns the elements of a four-tuple to four separate variables: ```csharp var (name, address, city, zip) = contact.GetAddressInfo(); @@ -24,11 +24,11 @@ var (name, address, city, zip) = contact.GetAddressInfo(); There are three ways to deconstruct a tuple: -- You can explicitly declare the type of each field inside parentheses. The following example uses this approach to deconstruct the 3-tuple returned by the `QueryCityData` method. +- You can explicitly declare the type of each field inside parentheses. The following example uses this approach to deconstruct the three-tuple returned by the `QueryCityData` method. :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple2.cs" ID="Snippet1"::: -- You can use the `var` keyword so that C# infers the type of each variable. You place the `var` keyword outside of the parentheses. The following example uses type inference when deconstructing the 3-tuple returned by the `QueryCityData` method. +- You can use the `var` keyword so that C# infers the type of each variable. You place the `var` keyword outside of the parentheses. The following example uses type inference when deconstructing the three-tuple returned by the `QueryCityData` method. :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple3.cs" ID="Snippet1"::: @@ -36,34 +36,34 @@ There are three ways to deconstruct a tuple: :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple4.cs" ID="Snippet1"::: - This is cumbersome and is not recommended. + This is cumbersome and isn't recommended. - Lastly, you may deconstruct the tuple into variables that have already been declared. :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple5.cs" ID="Snippet1"::: -Note that you cannot specify a specific type outside the parentheses even if every field in the tuple has the -same type. This generates compiler error CS8136, "Deconstruction 'var (...)' form disallows a specific type for 'var'.". +You can't specify a specific type outside the parentheses even if every field in the tuple has the +same type. Doing so generates compiler error CS8136, "Deconstruction 'var (...)' form disallows a specific type for 'var'.". -Note that you must also assign each element of the tuple to a variable. If you omit any elements, the compiler generates error CS8132, "Cannot deconstruct a tuple of 'x' elements into 'y' variables." +You must assign each element of the tuple to a variable. If you omit any elements, the compiler generates error CS8132, "Can't deconstruct a tuple of 'x' elements into 'y' variables." -Note that you cannot mix declarations and assignments to existing variables on the left-hand side of a deconstruction. The compiler generates error CS8184, "a deconstruction cannot mix declarations and expressions on the left-hand-side." when the members include newly declared and existing variables. +You can't mix declarations and assignments to existing variables on the left-hand side of a deconstruction. The compiler generates error CS8184, "a deconstruction can't mix declarations and expressions on the left-hand-side." when the members include newly declared and existing variables. ## Deconstructing tuple elements with discards -Often when deconstructing a tuple, you're interested in the values of only some elements. Starting with C# 7.0, you can take advantage of C#'s support for *discards*, which are write-only variables whose values you've chosen to ignore. A discard is designated by an underscore character ("\_") in an assignment. You can discard as many values as you like; all are represented by the single discard, `_`. +Often when deconstructing a tuple, you're interested in the values of only some elements. Starting with C# 7.0, you can take advantage of C#'s support for *discards*, which are write-only variables whose values you've chosen to ignore. A discard is chosen by an underscore character ("\_") in an assignment. You can discard as many values as you like; all are represented by the single discard, `_`. -The following example illustrates the use of tuples with discards. The `QueryCityDataForYears` method returns a 6-tuple with the name of a city, its area, a year, the city's population for that year, a second year, and the city's population for that second year. The example shows the change in population between those two years. Of the data available from the tuple, we're unconcerned with the city area, and we know the city name and the two dates at design-time. As a result, we're only interested in the two population values stored in the tuple, and can handle its remaining values as discards. +The following example illustrates the use of tuples with discards. The `QueryCityDataForYears` method returns a six-tuple with the name of a city, its area, a year, the city's population for that year, a second year, and the city's population for that second year. The example shows the change in population between those two years. Of the data available from the tuple, we're unconcerned with the city area, and we know the city name and the two dates at design-time. As a result, we're only interested in the two population values stored in the tuple, and can handle its remaining values as discards. :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple1.cs"::: ## Deconstructing user-defined types -C# does not offer built-in support for deconstructing non-tuple types other than the [`record`](#deconstructing-a-record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: +C# doesn't offer built-in support for deconstructing non-tuple types other than the [`record`](#deconstructing-a-record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple2.cs" ID="Snippet1"::: -You can then deconstruct an instance of the `Person` class named `p` with an assignment like the following: +You can then deconstruct an instance of the `Person` class named `p` with an assignment like the following code: :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple3.cs" ID="Snippet1"::: @@ -89,7 +89,7 @@ The following example deconstructs a `Person` object into four strings (the firs If you didn't author a class, struct, or interface, you can still deconstruct objects of that type by implementing one or more `Deconstruct` [extension methods](../../programming-guide/classes-and-structs/extension-methods.md) to return the values in which you're interested. -The following example defines two `Deconstruct` extension methods for the class. The first returns a set of values that indicate the characteristics of the property, including its type, whether it's static or instance, whether it's read-only, and whether it's indexed. The second indicates the property's accessibility. Because the accessibility of get and set accessors can differ, Boolean values indicate whether the property has separate get and set accessors and, if it does, whether they have the same accessibility. If there is only one accessor or both the get and the set accessor have the same accessibility, the `access` variable indicates the accessibility of the property as a whole. Otherwise, the accessibility of the get and set accessors are indicated by the `getAccess` and `setAccess` variables. +The following example defines two `Deconstruct` extension methods for the class. The first returns a set of values that indicate the characteristics of the property, including its type, whether it's static or instance, whether it's read-only, and whether it's indexed. The second indicates the property's accessibility. Because the accessibility of get and set accessors can differ, Boolean values indicate whether the property has separate get and set accessors and, if it does, whether they have the same accessibility. If there's only one accessor or both the get and the set accessor have the same accessibility, the `access` variable indicates the accessibility of the property as a whole. Otherwise, the accessibility of the get and set accessors are indicated by the `getAccess` and `setAccess` variables. [!code-csharp[Extension-deconstruct](./snippets/deconstructing-tuples/deconstruct-extension1.cs)] diff --git a/docs/csharp/fundamentals/functional/discards.md b/docs/csharp/fundamentals/functional/discards.md index 8ce1baa6c56f6..ac254eba7f417 100644 --- a/docs/csharp/fundamentals/functional/discards.md +++ b/docs/csharp/fundamentals/functional/discards.md @@ -1,10 +1,10 @@ --- -title: Discards - C# Guide +title: Discards - unassigned discardable variables description: Describes C#'s support for discards, which are unassigned, discardable variables, and the ways in which discards can be used. ms.technology: csharp-fundamentals -ms.date: 09/22/2020 +ms.date: 05/14/2021 --- -# Discards - C# Guide +# Discards - C# Fundamentals Starting with C# 7.0, C# supports discards, which are placeholder variables that are intentionally unused in application code. Discards are equivalent to unassigned variables; they don't have a value. A discard communicates intent to the compiler and others that read your code: You intended to ignore the result of an expression. You may want to ignore the result of an expression, one or more members of a tuple expression, an `out` parameter to a method, or the target of a pattern matching expression. diff --git a/docs/csharp/fundamentals/functional/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md index 4d5b2a4498978..6589290a05330 100644 --- a/docs/csharp/fundamentals/functional/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -1,8 +1,7 @@ --- title: Pattern matching overview - C# guide description: Learn about pattern matching expressions in C# -ms.date: 04/26/2021 -ms.technology: csharp-fundamentals +ms.date: 05/14/2021 --- # Pattern matching overview @@ -25,9 +24,9 @@ The preceding example used a [*constant pattern*](../../language-reference/opera ## Type tests -Another common use for pattern matching is to test a variable to see if it matches a given type. For example, the following code tests if a variable is non-null and implements the interface. If it does, it calls the method on that object. The declaration pattern doesn't match a `null` value, regardless of the compile-time type of the variable. The code below guards against `null`, in addition to guarding against a type that doesn't implement `IDisposable`. +Another common use for pattern matching is to test a variable to see if it matches a given type. For example, the following code tests if a variable is non-null and implements the interface. If it does, it calls the method on that list. The declaration pattern doesn't match a `null` value, regardless of the compile-time type of the variable. The code below guards against `null`, in addition to guarding against a type that doesn't implement `IList`. -:::code language="csharp" source="snippets/patterns/Program.cs" ID="TypeCheckDisposable"::: +:::code language="csharp" source="snippets/patterns/Program.cs" ID="MidPoint"::: The same tests can be applied in a `switch` expression to test a variable against multiple different types. You can use that information to create better algorithms based on the specific run-time type. @@ -47,10 +46,12 @@ The preceding example shows the same algorithm, but uses string values instead o You can use [*relational patterns*](../../language-reference/operators/patterns.md#relational-patterns) to test how a value compares to constants. For example, the following code returns the state of water based on the temperature in Fahrenheit: -:::code language="csharp" source="snippets/patterns/Simulation.cs" ID="RelationalPattern"::: +:::code language="csharp" source="snippets/patterns/Simulation.cs" ID="RelationalPattern" interactive="try-dotnet"::: The preceding code also demonstrates the conjunctive `and` [*logical pattern*](../../language-reference/operators/patterns.md#logical-patterns) to check that both relational patterns match. You can also use a disjunctive `or` pattern to check that either pattern matches. The two relational patterns are surrounded by parentheses, which you can use around any pattern for clarity. The final two switch arms handle the cases for the melting point and the boiling point. Without those two arms, the compiler warns you that your logic doesn't cover every possible input. +The preceding code also demonstrates another important feature the compiler provides for pattern matching expressions: The compiler warns you if you don't handle every input value. The compiler also issues a warning if a switch arm is already handled by a previous switch arm. That gives you freedom to refactor and reorder switch expressions. Try it yourself by refactoring the preceding code in the interactive window and see when the compiler issues warnings. You'll see squiggles under the code that has warnings. + ## Multiple inputs All the patterns you've seen so far have been checking one input. You can write patterns that examine multiple properties of an object. Consider the following `Order` record: diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs b/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs index 3bfdfe63d24e4..45f67d682de39 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs +++ b/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs @@ -13,14 +13,14 @@ public record Order(int Items, decimal Cost); class OrderProcessor { // - public double CalculateDiscount(Order order) => + public decimal CalculateDiscount(Order order) => order switch { - (Items: > 10, Cost: > 1000.00m) => 0.10, - (Items: > 5, Cost: > 500.00m) => 0.05, - Order { Cost: > 250.00m } => 0.02, + (Items: > 10, Cost: > 1000.00m) => 0.10m, + (Items: > 5, Cost: > 500.00m) => 0.05m, + Order { Cost: > 250.00m } => 0.02m, null => throw new ArgumentNullException(nameof(order), "Can't calculate discount on null order"), - var o => 0, + var order => 0m, }; // } @@ -28,14 +28,14 @@ public double CalculateDiscount(Order order) => class OrderProcessing { // - public double CalculateDiscount(Order order) => + public decimal CalculateDiscount(Order order) => order switch { - ( > 10, > 1000.00m) => 0.10, - ( > 5, > 50.00m) => 0.05, - Order { Cost: > 250.00m } => 0.02, + ( > 10, > 1000.00m) => 0.10m, + ( > 5, > 50.00m) => 0.05m, + Order { Cost: > 250.00m } => 0.02m, null => throw new ArgumentNullException(nameof(order), "Can't calculate discount on null order"), - var o => 0, + var order => 0m, }; // diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs index 4fef19c1bb09a..07cb1960d397d 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs +++ b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace patterns { @@ -10,21 +11,27 @@ static void Main(string[] args) NullReferenceCheck(); - TypeCheckDisposable(); + MidPointCheck(); } - private static void TypeCheckDisposable() + // + public static T MidPoint(IEnumerable sequence) { - object? heldReference = default; - - // - if (heldReference is IDisposable disposable) + if (sequence is IList list) + { + return list[list.Count / 2]; + } + else if (sequence is null) + { + throw new ArgumentNullException(nameof(sequence), "Sequence can't be null."); + } else { - disposable.Dispose(); + int halfLength = sequence.Count() / 2 - 1; + if (halfLength < 0) halfLength = 0; + return sequence.Skip(halfLength).First(); } - heldReference = null; - // } + // private static void NullReferenceCheck() { diff --git a/docs/csharp/fundamentals/functional/tutorials/pattern-matching.md b/docs/csharp/fundamentals/functional/tutorials/pattern-matching.md index bd46d50f135ba..6c760dfee0cae 100644 --- a/docs/csharp/fundamentals/functional/tutorials/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/tutorials/pattern-matching.md @@ -35,7 +35,7 @@ Consider a major metropolitan area that is using tolls and peak time pricing to From that brief description, you may have quickly sketched out an object hierarchy to model this system. However, your data is coming from multiple sources like other vehicle registration management systems. These systems provide different classes to model that data and you don't have a single object model you can use. In this tutorial, you'll use these simplified classes to model for the vehicle data from these external systems, as shown in the following code: -[!code-csharp[ExternalSystems](./snippets/patterns/start/toll-calculator/ExternalSystems.cs)] +:::code language="csharp" source="./snippets/patterns/start/toll-calculator/ExternalSystems.cs"::: You can download the starter code from the [dotnet/samples](https://github.com/dotnet/samples/tree/main/csharp/tutorials/patterns/start) GitHub repository. You can see that the vehicle classes are from different systems, and are in different namespaces. No common base class, other than `System.Object` can be leveraged. @@ -331,17 +331,17 @@ private static bool IsWeekDay(DateTime timeOfToll) => That method is correct, but it's repetitious. You can simplify it, as shown in the following code: -[!code-csharp[IsWeekDay](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#IsWeekDay)] +:::code language="csharp" source="./snippets/patterns/finished/toll-calculator/TollCalculator.cs" ID="IsWeekDay"::: Next, add a similar function to categorize the time into the blocks: -[!code-csharp[GetTimeBand](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#GetTimeBand)] +:::code language="csharp" source="./snippets/patterns/finished/toll-calculator/TollCalculator.cs" ID="GetTimeBand"::: You add a private `enum` to convert each range of time to a discrete value. Then, the `GetTimeBand` method uses [relational patterns](../../../language-reference/operators/patterns.md#relational-patterns), and [conjunctive `or` patterns](../../../language-reference/operators/patterns.md#logical-patterns), both added in C# 9.0. A relational pattern lets you test a numeric value using `<`, `>`, `<=`, or `>=`. The `or` pattern tests if an expression matches one or more patterns. You can also use an `and` pattern to ensure that an expression matches two distinct patterns, and a `not` pattern to test that an expression doesn't match a pattern. After you create those methods, you can use another `switch` expression with the **tuple pattern** to calculate the pricing premium. You could build a `switch` expression with all 16 arms: -[!code-csharp[FullTuplePattern](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#TuplePatternOne)] +:::code language="csharp" source="./snippets/patterns/finished/toll-calculator/TollCalculator.cs" ID="TuplePatternOne"::: The above code works, but it can be simplified. All eight combinations for the weekend have the same toll. You can replace all eight with the following line: @@ -374,7 +374,7 @@ public decimal PeakTimePremium(DateTime timeOfToll, bool inbound) => Finally, you can remove the two rush hour times that pay the regular price. Once you remove those arms, you can replace the `false` with a discard (`_`) in the final switch arm. You'll have the following finished method: -[!code-csharp[SimplifiedTuplePattern](./snippets/patterns/finished/toll-calculator/TollCalculator.cs#FinalTuplePattern)] +:::code language="csharp" source="./snippets/patterns/finished/toll-calculator/TollCalculator.cs" range="FinalTuplePattern"::: This example highlights one of the advantages of pattern matching: the pattern branches are evaluated in order. If you rearrange them so that an earlier branch handles one of your later cases, the compiler warns you about the unreachable code. Those language rules made it easier to do the preceding simplifications with confidence that the code didn't change. From 70d87242473a58a836472f7c4469153b7daa1b6d Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 15:53:54 -0400 Subject: [PATCH 36/60] light edit on exceptions section --- .../compiler-generated-exceptions.md | 7 +++---- .../creating-and-throwing-exceptions.md | 16 ++++++++++------ .../exceptions/exception-handling.md | 18 +++++++++--------- ...ow-to-execute-cleanup-code-using-finally.md | 7 +++---- ...w-to-handle-an-exception-using-try-catch.md | 9 ++++----- docs/csharp/fundamentals/exceptions/index.md | 7 +++---- .../exceptions/using-exceptions.md | 15 +++++++-------- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md b/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md index bb788a844c1fa..30e3c262b88ac 100644 --- a/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md +++ b/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md @@ -1,12 +1,11 @@ --- -title: "Compiler-Generated Exceptions - C# Programming Guide" +title: "Compiler-Generated Exceptions" description: Learn about compiler-generated exceptions. Review a list of automatically thrown exceptions and their error conditions. -ms.date: 12/09/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "exceptions [C#], compiler-generated" -ms.assetid: 53b52f97-b366-4ed7-b05b-9eb78096b7f9 --- -# Compiler-Generated Exceptions (C# Programming Guide) +# Compiler-Generated Exceptions Some exceptions are thrown automatically by the .NET runtime when basic operations fail. These exceptions and their error conditions are listed in the following table. diff --git a/docs/csharp/fundamentals/exceptions/creating-and-throwing-exceptions.md b/docs/csharp/fundamentals/exceptions/creating-and-throwing-exceptions.md index fc6f7fad926f5..309e3837bb071 100644 --- a/docs/csharp/fundamentals/exceptions/creating-and-throwing-exceptions.md +++ b/docs/csharp/fundamentals/exceptions/creating-and-throwing-exceptions.md @@ -1,25 +1,29 @@ --- -title: "Creating and Throwing Exceptions - C# Programming Guide" +title: "Creating and Throwing Exceptions" description: Learn about creating and throwing exceptions. Exceptions are used to indicate that an error has occurred while running a program. -ms.date: 12/09/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "catching exceptions [C#]" - "throwing exceptions [C#]" - "exceptions [C#], creating" - "exceptions [C#], throwing" -ms.assetid: 6bbba495-a115-4c6d-90cc-1f4d7b5f39e2 --- -# Creating and Throwing Exceptions (C# Programming Guide) +# Creating and Throwing Exceptions -Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then *thrown* with the [throw](../../language-reference/keywords/throw.md) keyword. The runtime then searches for the most compatible exception handler. +Exceptions are used to indicate that an error has occurred while running the program. Exception objects that describe an error are created and then *thrown* with the [`throw`](../../language-reference/keywords/throw.md) keyword. The runtime then searches for the most compatible exception handler. Programmers should throw exceptions when one or more of the following conditions are true: - The method can't complete its defined functionality. For example, if a parameter to a method has an invalid value: + :::code language="csharp" source="snippets/exceptions/Program.cs" ID="CantComplete"::: + - An inappropriate call to an object is made, based on the object state. One example might be trying to write to a read-only file. In cases where an object state doesn't allow an operation, throw an instance of or an object based on a derivation of this class. The following code is an example of a method that throws an object: + :::code language="csharp" source="snippets/exceptions/ProgramLog.cs" ID="ProgramLog"::: + - When an argument to a method causes an exception. In this case, the original exception should be caught and an instance should be created. The original exception should be passed to the constructor of the as the parameter: + :::code language="csharp" source="snippets/exceptions/Program.cs" ID="InvalidArg"::: Exceptions contain a property named . This string contains the name of the methods on the current call stack, together with the file name and line number where the exception was thrown for each method. A object is created automatically by the common language runtime (CLR) from the point of the `throw` statement, so that exceptions must be thrown from the point where the stack trace should begin. @@ -41,7 +45,7 @@ The following list identifies practices to avoid when throwing exceptions: Programs can throw a predefined exception class in the namespace (except where previously noted), or create their own exception classes by deriving from . The derived classes should define at least four constructors: one parameterless constructor, one that sets the message property, and one that sets both the and properties. The fourth constructor is used to serialize the exception. New exception classes should be serializable. For example: -:::code language="csharp" source="snippets/exceptions/InvalidDepartmentException.cs" id="DefineExceptionClass"::: +:::code language="csharp" source="snippets/exceptions/InvalidDepartmentException.cs" ID="DefineExceptionClass"::: Add new properties to the exception class when the data they provide is useful to resolving the exception. If new properties are added to the derived exception class, `ToString()` should be overridden to return the added information. diff --git a/docs/csharp/fundamentals/exceptions/exception-handling.md b/docs/csharp/fundamentals/exceptions/exception-handling.md index cf48c989b93b3..48aea63b1c2fc 100644 --- a/docs/csharp/fundamentals/exceptions/exception-handling.md +++ b/docs/csharp/fundamentals/exceptions/exception-handling.md @@ -1,7 +1,7 @@ --- title: "Exception Handling - C# Programming Guide" description: Learn about exception handling. See examples of try-catch, try-finally, and try-catch-finally statements. -ms.date: 12/09/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "exception handling [C#], about exception handling" - "exceptions [C#], handling" @@ -13,11 +13,11 @@ A [try](../../language-reference/keywords/try-catch.md) block is used by C# prog The following examples show a `try-catch` statement, a `try-finally` statement, and a `try-catch-finally` statement. -:::code language="csharp" source="snippets/exceptions/Program.cs" id="TryCatchStructure"::: +:::code language="csharp" source="snippets/exceptions/Program.cs" ID="TryCatchStructure"::: -:::code language="csharp" source="snippets/exceptions/Program.cs" id="TryFinallyStructure"::: +:::code language="csharp" source="snippets/exceptions/Program.cs" ID="TryFinallyStructure"::: -:::code language="csharp" source="snippets/exceptions/Program.cs" id="TryCatchFinallyStructure"::: +:::code language="csharp" source="snippets/exceptions/Program.cs" ID="TryCatchFinallyStructure"::: A `try` block without a `catch` or `finally` block causes a compiler error. @@ -31,11 +31,11 @@ Catch exceptions when the following conditions are true: - You have a good understanding of why the exception might be thrown, and you can implement a specific recovery, such as prompting the user to enter a new file name when you catch a object. - You can create and throw a new, more specific exception. - :::code language="csharp" source="snippets/exceptions/Program.cs" id="ThrowMoreSpecificException"::: -- You want to partially handle an exception before passing it on for additional handling. In the following example, a `catch` block is used to add an entry to an error log before rethrowing the exception. - :::code language="csharp" source="snippets/exceptions/Program.cs" id="RethrowError"::: + :::code language="csharp" source="snippets/exceptions/Program.cs" ID="ThrowMoreSpecificException"::: +- You want to partially handle an exception before passing it on for more handling. In the following example, a `catch` block is used to add an entry to an error log before rethrowing the exception. + :::code language="csharp" source="snippets/exceptions/Program.cs" ID="RethrowError"::: -You can also specify *exception filters* to add a boolean expression to a catch clause. These indicate that a specific catch clause matches only when that condition is true. In the following example, both catch clauses use the same exception class, but an additional condition is checked to create a different error message: +You can also specify *exception filters* to add a boolean expression to a catch clause. Exception filters indicate that a specific catch clause matches only when that condition is true. In the following example, both catch clauses use the same exception class, but an extra condition is checked to create a different error message: :::code language="csharp" source="snippets/exceptions/ExceptionFilter.cs" ID="DemonstrateExceptionFilter"::: @@ -53,7 +53,7 @@ The `finally` block can be used to release resources such as file streams, datab In the following example, the `finally` block is used to close a file that is opened in the `try` block. Notice that the state of the file handle is checked before the file is closed. If the `try` block can't open the file, the file handle still has the value `null` and the `finally` block doesn't try to close it. Instead, if the file is opened successfully in the `try` block, the `finally` block closes the open file. -:::code language="csharp" source="snippets/exceptions/Program.cs" id="CleanupIfNotNull"::: +:::code language="csharp" source="snippets/exceptions/Program.cs" ID="CleanupIfNotNull"::: ## C# Language Specification diff --git a/docs/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md b/docs/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md index 80c5b2a1a4c71..43a4743c77b76 100644 --- a/docs/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md +++ b/docs/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md @@ -1,15 +1,14 @@ --- -title: "How to execute cleanup code using finally - C# Programming Guide" +title: "How to execute cleanup code using finally" description: Learn how to execute cleanup code using a 'finally' statement. Finally statements ensure that any necessary cleanup of objects occurs immediately. ms.topic: how-to -ms.date: 12/09/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "try/finally blocks [C#]" - "exceptions [C#], try/finally block" - "exception handling [C#], try/finally block" -ms.assetid: 1b1e5aef-3f32-4a88-9d39-b5fffb33bdaf --- -# How to execute cleanup code using finally (C# Programming Guide) +# How to execute cleanup code using finally The purpose of a `finally` statement is to ensure that the necessary cleanup of objects, usually objects that are holding external resources, occurs immediately, even if an exception is thrown. One example of such cleanup is calling on a immediately after use instead of waiting for the object to be garbage collected by the common language runtime, as follows: diff --git a/docs/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md b/docs/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md index 4f17db8495f72..98ba4515b3641 100644 --- a/docs/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md +++ b/docs/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md @@ -1,15 +1,14 @@ --- -title: "How to handle an exception using try-catch - C# Programming Guide" +title: "How to handle an exception using try-catch" description: Learn how to handle an exception using a try-catch block. See a code example and view additional available resources. ms.topic: how-to -ms.date: 12/09/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "exception handling [C#], try/catch blocks" - "exceptions [C#], try/catch blocks" - "try/catch blocks [C#]" -ms.assetid: ca8e3773-980e-4767-8633-7408540e9818 --- -# How to handle an exception using try/catch (C# Programming Guide) +# How to handle an exception using try/catch The purpose of a [try-catch](../../language-reference/keywords/try-catch.md) block is to catch and handle an exception generated by working code. Some exceptions can be handled in a `catch` block and the problem solved without the exception being rethrown; however, more often the only thing that you can do is make sure that the appropriate exception is thrown. @@ -17,7 +16,7 @@ The purpose of a [try-catch](../../language-reference/keywords/try-catch.md) blo In this example, isn't the most appropriate exception: makes more sense for the method because the error is caused by the `index` argument passed in by the caller. -:::code language="csharp" source="snippets/exceptions/ExampleTryCatch.cs" id="ExampleTryCatch"::: +:::code language="csharp" source="snippets/exceptions/ExampleTryCatch.cs" ID="ExampleTryCatch"::: ## Comments diff --git a/docs/csharp/fundamentals/exceptions/index.md b/docs/csharp/fundamentals/exceptions/index.md index 2f0209973b441..ca33fd013a04d 100644 --- a/docs/csharp/fundamentals/exceptions/index.md +++ b/docs/csharp/fundamentals/exceptions/index.md @@ -1,14 +1,13 @@ --- -title: "Exceptions and Exception Handling - C# Programming Guide" +title: "Exceptions and Exception Handling" description: Learn about exceptions and exception handling. These C# features help deal with unexpected or exceptional situations that happen when a program is running. -ms.date: 12/09/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "exception handling [C#]" - "exceptions [C#]" - "C# language, exceptions" -ms.assetid: 0001887f-4fa2-47e2-8034-2819477e2344 --- -# Exceptions and Exception Handling (C# Programming Guide) +# Exceptions and Exception Handling The C# language's exception handling features help you deal with any unexpected or exceptional situations that occur when a program is running. Exception handling uses the `try`, `catch`, and `finally` keywords to try actions that may not succeed, to handle failures when you decide that it's reasonable to do so, and to clean up resources afterward. Exceptions can be generated by the common language runtime (CLR), by .NET or third-party libraries, or by application code. Exceptions are created by using the `throw` keyword. diff --git a/docs/csharp/fundamentals/exceptions/using-exceptions.md b/docs/csharp/fundamentals/exceptions/using-exceptions.md index 164302420a5c4..dc3c34b7aafb6 100644 --- a/docs/csharp/fundamentals/exceptions/using-exceptions.md +++ b/docs/csharp/fundamentals/exceptions/using-exceptions.md @@ -1,33 +1,32 @@ --- -title: "Using Exceptions - C# Programming Guide" +title: "Using Exceptions" description: Learn how to use exceptions. Exceptions are thrown by code that encounters an error and caught by code that corrects the error. -ms.date: 12/09/2020 +ms.date: 05/14/2021 helpviewer_keywords: - "exception handling [C#], about exception handling" - "exceptions [C#], about exceptions" -ms.assetid: 71472c62-320a-470a-97d2-67995180389d --- -# Use exceptions (C# programming guide) +# Use exceptions In C#, errors in the program at run time are propagated through the program by using a mechanism called exceptions. Exceptions are thrown by code that encounters an error and caught by code that can correct the error. Exceptions can be thrown by the .NET runtime or by code in a program. Once an exception is thrown, it propagates up the call stack until a `catch` statement for the exception is found. Uncaught exceptions are handled by a generic exception handler provided by the system that displays a dialog box. Exceptions are represented by classes derived from . This class identifies the type of exception and contains properties that have details about the exception. Throwing an exception involves creating an instance of an exception-derived class, optionally configuring properties of the exception, and then throwing the object by using the `throw` keyword. For example: -:::code language="csharp" source="snippets/exceptions/Program.cs" id="ThrowException"::: +:::code language="csharp" source="snippets/exceptions/Program.cs" ID="ThrowException"::: After an exception is thrown, the runtime checks the current statement to see whether it is within a `try` block. If it is, any `catch` blocks associated with the `try` block are checked to see whether they can catch the exception. `Catch` blocks typically specify exception types; if the type of the `catch` block is the same type as the exception, or a base class of the exception, the `catch` block can handle the method. For example: -:::code language="csharp" source="snippets/exceptions/Program.cs" id="CatchException"::: +:::code language="csharp" source="snippets/exceptions/Program.cs" ID="CatchException"::: If the statement that throws an exception isn't within a `try` block or if the `try` block that encloses it has no matching `catch` block, the runtime checks the calling method for a `try` statement and `catch` blocks. The runtime continues up the calling stack, searching for a compatible `catch` block. After the `catch` block is found and executed, control is passed to the next statement after that `catch` block. -A `try` statement can contain more than one `catch` block. The first `catch` statement that can handle the exception is executed; any following `catch` statements, even if they're compatible, are ignored. Catch blocks should always be ordered from most specific (or most-derived) to least specific. For example: +A `try` statement can contain more than one `catch` block. The first `catch` statement that can handle the exception is executed; any following `catch` statements, even if they're compatible, are ignored. Order catch blocks from most specific (or most-derived) to least specific. For example: :::code language="csharp" source="snippets/exceptions/CatchOrder.cs"::: Before the `catch` block is executed, the runtime checks for `finally` blocks. `Finally` blocks enable the programmer to clean up any ambiguous state that could be left over from an aborted `try` block, or to release any external resources (such as graphics handles, database connections, or file streams) without waiting for the garbage collector in the runtime to finalize the objects. For example: -:::code language="csharp" source="snippets/exceptions/Program.cs" id="TestFinally"::: +:::code language="csharp" source="snippets/exceptions/Program.cs" ID="TestFinally"::: If `WriteByte()` threw an exception, the code in the second `try` block that tries to reopen the file would fail if `file.Close()` isn't called, and the file would remain locked. Because `finally` blocks are executed even if an exception is thrown, the `finally` block in the previous example allows for the file to be closed correctly and helps avoid an error. From 3b90c34e425321f601096050337dcd29d1b677df Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 16:25:12 -0400 Subject: [PATCH 37/60] fix warnings --- .../fundamentals/coding-style/coding-conventions.md | 11 ++--------- .../fundamentals/coding-style/identifier-names.md | 4 ++-- .../fundamentals/functional/pattern-matching.md | 2 +- .../functional/snippets/patterns/Program.cs | 2 +- 4 files changed, 6 insertions(+), 13 deletions(-) diff --git a/docs/csharp/fundamentals/coding-style/coding-conventions.md b/docs/csharp/fundamentals/coding-style/coding-conventions.md index 7d2e18b8db2ce..ed3bd2e309b48 100644 --- a/docs/csharp/fundamentals/coding-style/coding-conventions.md +++ b/docs/csharp/fundamentals/coding-style/coding-conventions.md @@ -1,5 +1,5 @@ --- -title: "C# Coding Conventions - C# Programming Guide" +title: "C# Coding Conventions" description: Learn about coding conventions in C#. Coding conventions create a consistent look to the code and facilitate copying, changing, and maintaining the code. ms.date: 05/14/2021 helpviewer_keywords: @@ -8,7 +8,7 @@ helpviewer_keywords: - "C# language, coding conventions" --- -# C# Coding Conventions (C# Programming Guide) +# C# Coding Conventions Coding conventions serve the following purposes: @@ -145,13 +145,9 @@ Good layout uses formatting to emphasize the structure of your code and to make - Use the default Code Editor settings (smart indenting, four-character indents, tabs saved as spaces). For more information, see [Options, Text Editor, C#, Formatting](/visualstudio/ide/reference/options-text-editor-csharp-formatting). - Write only one statement per line. - - Write only one declaration per line. - - If continuation lines are not indented automatically, indent them one tab stop (four spaces). - - Add at least one blank line between method definitions and property definitions. - - Use parentheses to make clauses in an expression apparent, as shown in the following code. :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet2"::: @@ -159,11 +155,8 @@ Good layout uses formatting to emphasize the structure of your code and to make ## Commenting conventions - Place the comment on a separate line, not at the end of a line of code. - - Begin comment text with an uppercase letter. - - End comment text with a period. - - Insert one space between the comment delimiter (//) and the comment text, as shown in the following example. :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet3"::: diff --git a/docs/csharp/fundamentals/coding-style/identifier-names.md b/docs/csharp/fundamentals/coding-style/identifier-names.md index 5199537f111d3..c4b26d59e9c9c 100644 --- a/docs/csharp/fundamentals/coding-style/identifier-names.md +++ b/docs/csharp/fundamentals/coding-style/identifier-names.md @@ -15,12 +15,12 @@ For a complete definition of valid identifiers, see the [Identifiers topic in th ## Naming conventions -In addition to the rules, there are a number of identifier [naming conventions](../../../standard/design-guidelines/naming-guidelines.md) used throughout the .NET APIs. By convention, C# programs use `PascalCase` for type names, namespaces, and all public members. In addition, the following conventions are common: +In addition to the rules, there are many identifier [naming conventions](../../../standard/design-guidelines/naming-guidelines.md) used throughout the .NET APIs. By convention, C# programs use `PascalCase` for type names, namespaces, and all public members. In addition, the following conventions are common: - Interface names start with a capital `I`. - Attribute types end with the word `Attribute`. - Enum types use a singular noun for non-flags, and a plural noun for flags. -- Identifiers should not contain two consecutive `_` characters. Those names are reserved for compiler generated identifiers. +- Identifiers shouldn't contain two consecutive `_` characters. Those names are reserved for compiler-generated identifiers. ## C# Language Specification diff --git a/docs/csharp/fundamentals/functional/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md index 6589290a05330..1f97ddd18b280 100644 --- a/docs/csharp/fundamentals/functional/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -24,7 +24,7 @@ The preceding example used a [*constant pattern*](../../language-reference/opera ## Type tests -Another common use for pattern matching is to test a variable to see if it matches a given type. For example, the following code tests if a variable is non-null and implements the interface. If it does, it calls the method on that list. The declaration pattern doesn't match a `null` value, regardless of the compile-time type of the variable. The code below guards against `null`, in addition to guarding against a type that doesn't implement `IList`. +Another common use for pattern matching is to test a variable to see if it matches a given type. For example, the following code tests if a variable is non-null and implements the interface. If it does, it uses the property on that list. The declaration pattern doesn't match a `null` value, regardless of the compile-time type of the variable. The code below guards against `null`, in addition to guarding against a type that doesn't implement `IList`. :::code language="csharp" source="snippets/patterns/Program.cs" ID="MidPoint"::: diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs index 07cb1960d397d..a403562d3c3c8 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs +++ b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs @@ -17,7 +17,7 @@ static void Main(string[] args) // public static T MidPoint(IEnumerable sequence) { - if (sequence is IList list) + if (sequence is ICollection list) { return list[list.Count / 2]; } From e4f9a72012833e31d0100f3510be06c6886eb2e9 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 16:37:07 -0400 Subject: [PATCH 38/60] finish moving coding conventions --- .../coding-style/coding-conventions.md | 80 +++++++++---------- .../coding-conventions.csproj | 14 +--- .../snippets/coding-conventions}/program.cs | 0 3 files changed, 44 insertions(+), 50 deletions(-) rename samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/coding conventions examples.csproj => docs/csharp/fundamentals/coding-style/snippets/coding-conventions/coding-conventions.csproj (57%) rename {samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs => docs/csharp/fundamentals/coding-style/snippets/coding-conventions}/program.cs (100%) diff --git a/docs/csharp/fundamentals/coding-style/coding-conventions.md b/docs/csharp/fundamentals/coding-style/coding-conventions.md index ed3bd2e309b48..9713079ae8e2d 100644 --- a/docs/csharp/fundamentals/coding-style/coding-conventions.md +++ b/docs/csharp/fundamentals/coding-style/coding-conventions.md @@ -134,7 +134,7 @@ For more information on C# naming conventions, see [C# Coding Style](https://git - Examples that don't include [using directives](../../language-reference/keywords/using-directive.md), use namespace qualifications. If you know that a namespace is imported by default in a project, you don't have to fully qualify the names from that namespace. Qualified names can be broken after a dot (.) if they are too long for a single line, as shown in the following example. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet1"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet1"::: - You don't have to change the names of objects that were created by using the Visual Studio designer tools to make them fit other guidelines. @@ -150,7 +150,7 @@ Good layout uses formatting to emphasize the structure of your code and to make - Add at least one blank line between method definitions and property definitions. - Use parentheses to make clauses in an expression apparent, as shown in the following code. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet2"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet2"::: ## Commenting conventions @@ -159,7 +159,7 @@ Good layout uses formatting to emphasize the structure of your code and to make - End comment text with a period. - Insert one space between the comment delimiter (//) and the comment text, as shown in the following example. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet3"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet3"::: - Don't create formatted blocks of asterisks around comments. @@ -171,39 +171,39 @@ The following sections describe practices that the C# team follows to prepare co - Use [string interpolation](../../language-reference/tokens/interpolated.md) to concatenate short strings, as shown in the following code. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet6"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet6"::: - To append strings in loops, especially when you're working with large amounts of text, use a object. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet7"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet7"::: ### Implicitly typed local variables - Use [implicit typing](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md) for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet8"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet8"::: - Don't use [var](../../language-reference/keywords/var.md) when the type is not apparent from the right side of the assignment. Don't assume the type is clear from a method name. A variable type is considered clear if it's a `new` operator or an explicit cast. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet9"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet9"::: - Don't rely on the variable name to specify the type of the variable. It might not be correct. In the following example, the variable name `inputInt` is misleading. It's a string. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet10"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet10"::: - Avoid the use of `var` in place of [dynamic](../../language-reference/builtin-types/reference-types.md). Use `dynamic` when you want run-time type inference. For more information, see [Using type dynamic (C# Programming Guide)](../../programming-guide/types/using-type-dynamic.md). -- Use implicit typing to determine the type of the loop variable in [for](../../language-reference/keywords/for.md) loops. +- Use implicit typing to determine the type of the loop variable in [`for`](../../language-reference/keywords/for.md) loops. The following example uses implicit typing in a `for` statement. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet7"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet7"::: -- Don't use implicit typing to determine the type of the loop variable in [foreach](../../language-reference/keywords/foreach-in.md) loops. +- Don't use implicit typing to determine the type of the loop variable in [`foreach`](../../language-reference/keywords/foreach-in.md) loops. The following example uses explicit typing in a `foreach` statement. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet12"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet12"::: > [!NOTE] > Be careful not to accidentally change a type of an element of the iterable collection. For example, it is easy to switch from to in a `foreach` statement, which changes the execution of a query. @@ -216,63 +216,63 @@ In general, use `int` rather than unsigned types. The use of `int` is common thr Use the concise syntax when you initialize arrays on the declaration line. In the following example, note that you can't use `var` instead of `string[]`. -:::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet13a"::: +:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet13a"::: If you use explicit instantiation, you can use `var`. -:::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet13b"::: +:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet13b"::: If you specify an array size, you have to initialize the elements one at a time. -:::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet13c"::: +:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet13c"::: ### Delegates Use [`Func<>` and `Action<>`](../../../standard/delegates-lambdas.md) instead of defining delegate types. In a class, define the delegate method. -:::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet14a"::: +:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet14a"::: Call the method using the signature defined by the `Func<>` or `Action<>` delegate. -:::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet15a"::: +:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet15a"::: If you create instances of a delegate type, use the concise syntax. In a class, define the delegate type and a method that has a matching signature. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet14b"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet14b"::: Create an instance of the delegate type and call it. The following declaration shows the condensed syntax. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet15b"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet15b"::: The following declaration uses the full syntax. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet15c"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet15c"::: ### `try`-`catch` and `using` statements in exception handling - Use a [try-catch](../../language-reference/keywords/try-catch.md) statement for most exception handling. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet16"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet16"::: - Simplify your code by using the C# [using statement](../../language-reference/keywords/using-statement.md). If you have a [try-finally](../../language-reference/keywords/try-finally.md) statement in which the only code in the `finally` block is a call to the method, use a `using` statement instead. In the following example, the `try`-`finally` statement only calls `Dispose` in the `finally` block. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet17a"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet17a"::: You can do the same thing with a `using` statement. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet17b"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet17b"::: In C# 8 and later versions, use the new [`using` syntax](../../language-reference/keywords/using-statement.md) that doesn't require braces: - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet17c"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet17c"::: ### `&&` and `||` operators To avoid exceptions and increase performance by skipping unnecessary comparisons, use [`&&`](../../language-reference/operators/boolean-logical-operators.md#conditional-logical-and-operator-) instead of [`&`](../../language-reference/operators/boolean-logical-operators.md#logical-and-operator-) and [`||`](../../language-reference/operators/boolean-logical-operators.md#conditional-logical-or-operator-) instead of [`|`](../../language-reference/operators/boolean-logical-operators.md#logical-or-operator-) when you perform comparisons, as shown in the following example. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet18"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet18"::: If the divisor is 0, the second clause in the `if` statement would cause a run-time error. But the && operator short-circuits when the first expression is false. That is, it doesn't evaluate the second expression. The & operator would evaluate both, resulting in a run-time error when `divisor` is 0. @@ -280,7 +280,7 @@ If the divisor is 0, the second clause in the `if` statement would cause a run-t - Use one of the concise forms of object instantiation, as shown in the following declarations. The second example shows syntax that is available starting in C# 9. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet19"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet19"::: ```csharp ExampleClass instance2 = new(); @@ -288,25 +288,25 @@ If the divisor is 0, the second clause in the `if` statement would cause a run-t The preceding declarations are equivalent to the following declaration. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet20"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet20"::: - Use object initializers to simplify object creation, as shown in the following example. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet21a"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet21a"::: The following example sets the same properties as the preceding example but doesn't use initializers. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet21b"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet21b"::: ### Event handling If you're defining an event handler that you don't need to remove later, use a lambda expression. -:::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet22"::: +:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet22"::: The lambda expression shortens the following traditional definition. -:::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet23"::: +:::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet23"::: ### Static members @@ -316,29 +316,29 @@ Call [static](../../language-reference/keywords/static.md) members by using the - Use meaningful names for query variables. The following example uses `seattleCustomers` for customers who are located in Seattle. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet25"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet25"::: - Use aliases to make sure that property names of anonymous types are correctly capitalized, using Pascal casing. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet26"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet26"::: - Rename properties when the property names in the result would be ambiguous. For example, if your query returns a customer name and a distributor ID, instead of leaving them as `Name` and `ID` in the result, rename them to clarify that `Name` is the name of a customer, and `ID` is the ID of a distributor. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet27"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet27"::: - Use implicit typing in the declaration of query variables and range variables. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet25"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet25"::: -- Align query clauses under the [from](../../language-reference/keywords/from-clause.md) clause, as shown in the previous examples. +- Align query clauses under the [`from`](../../language-reference/keywords/from-clause.md) clause, as shown in the previous examples. -- Use [where](../../language-reference/keywords/where-clause.md) clauses before other query clauses to ensure that later query clauses operate on the reduced, filtered set of data. +- Use [`where`](../../language-reference/keywords/where-clause.md) clauses before other query clauses to ensure that later query clauses operate on the reduced, filtered set of data. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet29"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet29"::: -- Use multiple `from` clauses instead of a [join](../../language-reference/keywords/join-clause.md) clause to access inner collections. For example, a collection of `Student` objects might each contain a collection of test scores. When the following query is executed, it returns each score that is over 90, along with the last name of the student who received the score. +- Use multiple `from` clauses instead of a [`join`](../../language-reference/keywords/join-clause.md) clause to access inner collections. For example, a collection of `Student` objects might each contain a collection of test scores. When the following query is executed, it returns each score that is over 90, along with the last name of the student who received the score. - :::code language="csharp" source="../../../../samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs" id="Snippet30"::: + :::code language="csharp" source="./snippets/coding-conventions/program.cs" id="Snippet30"::: ## Security diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/coding conventions examples.csproj b/docs/csharp/fundamentals/coding-style/snippets/coding-conventions/coding-conventions.csproj similarity index 57% rename from samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/coding conventions examples.csproj rename to docs/csharp/fundamentals/coding-style/snippets/coding-conventions/coding-conventions.csproj index 1c0b1cf8c5c8e..5630d66d66aef 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/coding conventions examples.csproj +++ b/docs/csharp/fundamentals/coding-style/snippets/coding-conventions/coding-conventions.csproj @@ -1,17 +1,11 @@  - Exe - net48 - 8.0 + WinExe + net5.0-windows + Coding_Conventions_Examples.Program Coding_Conventions_Examples Coding Conventions Examples - - Coding_Conventions_Examples.Program + true - - - - - diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs b/docs/csharp/fundamentals/coding-style/snippets/coding-conventions/program.cs similarity index 100% rename from samples/snippets/csharp/VS_Snippets_VBCSharp/csprogguidecodingconventions/cs/program.cs rename to docs/csharp/fundamentals/coding-style/snippets/coding-conventions/program.cs From 27b3e0594d312d1a91f0ab3b2b0ac29da381a801 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 16:45:50 -0400 Subject: [PATCH 39/60] fix warnings --- docs/csharp/fundamentals/functional/pattern-matching.md | 2 +- .../functional/snippets/patterns/OrderProcessor.cs | 4 ++-- .../fundamentals/functional/snippets/patterns/Program.cs | 7 +++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/csharp/fundamentals/functional/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md index 1f97ddd18b280..074fa979a548e 100644 --- a/docs/csharp/fundamentals/functional/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -24,7 +24,7 @@ The preceding example used a [*constant pattern*](../../language-reference/opera ## Type tests -Another common use for pattern matching is to test a variable to see if it matches a given type. For example, the following code tests if a variable is non-null and implements the interface. If it does, it uses the property on that list. The declaration pattern doesn't match a `null` value, regardless of the compile-time type of the variable. The code below guards against `null`, in addition to guarding against a type that doesn't implement `IList`. +Another common use for pattern matching is to test a variable to see if it matches a given type. For example, the following code tests if a variable is non-null and implements the interface. If it does, it uses the property on that list to find the middle index. The declaration pattern doesn't match a `null` value, regardless of the compile-time type of the variable. The code below guards against `null`, in addition to guarding against a type that doesn't implement `IList`. :::code language="csharp" source="snippets/patterns/Program.cs" ID="MidPoint"::: diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs b/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs index 45f67d682de39..0c80174d41c18 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs +++ b/docs/csharp/fundamentals/functional/snippets/patterns/OrderProcessor.cs @@ -20,7 +20,7 @@ public decimal CalculateDiscount(Order order) => (Items: > 5, Cost: > 500.00m) => 0.05m, Order { Cost: > 250.00m } => 0.02m, null => throw new ArgumentNullException(nameof(order), "Can't calculate discount on null order"), - var order => 0m, + var someObject => 0m, }; // } @@ -35,7 +35,7 @@ public decimal CalculateDiscount(Order order) => ( > 5, > 50.00m) => 0.05m, Order { Cost: > 250.00m } => 0.02m, null => throw new ArgumentNullException(nameof(order), "Can't calculate discount on null order"), - var order => 0m, + var someObject => 0m, }; // diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs index a403562d3c3c8..3728c45b2371d 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs +++ b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace patterns { @@ -11,13 +12,15 @@ static void Main(string[] args) NullReferenceCheck(); - MidPointCheck(); + var sequence = new List {1,2,3,4,5,6,7}; + var middle = MidPoint(sequence); + Console.WriteLine(middle); } // public static T MidPoint(IEnumerable sequence) { - if (sequence is ICollection list) + if (sequence is IList list) { return list[list.Count / 2]; } From f1467a33f12d57ac71f2e90b36e7e5ff78ba5314 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 14 May 2021 16:56:21 -0400 Subject: [PATCH 40/60] don't use interactive on C# 9 feature --- .../fundamentals/functional/pattern-matching.md | 8 ++++++-- .../functional/snippets/patterns/Simulation.cs | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/csharp/fundamentals/functional/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md index 074fa979a548e..ad63af0a333f0 100644 --- a/docs/csharp/fundamentals/functional/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -46,11 +46,15 @@ The preceding example shows the same algorithm, but uses string values instead o You can use [*relational patterns*](../../language-reference/operators/patterns.md#relational-patterns) to test how a value compares to constants. For example, the following code returns the state of water based on the temperature in Fahrenheit: -:::code language="csharp" source="snippets/patterns/Simulation.cs" ID="RelationalPattern" interactive="try-dotnet"::: +:::code language="csharp" source="snippets/patterns/Simulation.cs" ID="RelationalPattern"::: The preceding code also demonstrates the conjunctive `and` [*logical pattern*](../../language-reference/operators/patterns.md#logical-patterns) to check that both relational patterns match. You can also use a disjunctive `or` pattern to check that either pattern matches. The two relational patterns are surrounded by parentheses, which you can use around any pattern for clarity. The final two switch arms handle the cases for the melting point and the boiling point. Without those two arms, the compiler warns you that your logic doesn't cover every possible input. -The preceding code also demonstrates another important feature the compiler provides for pattern matching expressions: The compiler warns you if you don't handle every input value. The compiler also issues a warning if a switch arm is already handled by a previous switch arm. That gives you freedom to refactor and reorder switch expressions. Try it yourself by refactoring the preceding code in the interactive window and see when the compiler issues warnings. You'll see squiggles under the code that has warnings. +The preceding code also demonstrates another important feature the compiler provides for pattern matching expressions: The compiler warns you if you don't handle every input value. The compiler also issues a warning if a switch arm is already handled by a previous switch arm. That gives you freedom to refactor and reorder switch expressions. Another way to write the same expression could be: + +:::code language="csharp" source="snippets/patterns/Simulation.cs" ID="RelationalPattern2"::: + +The key lesson in this, and any other refactoring or reordering is that the compiler validates that you've covered all inputs. ## Multiple inputs diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/Simulation.cs b/docs/csharp/fundamentals/functional/snippets/patterns/Simulation.cs index 6db55974c1d0b..bfad185dd6190 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/Simulation.cs +++ b/docs/csharp/fundamentals/functional/snippets/patterns/Simulation.cs @@ -59,6 +59,17 @@ string WaterState(int tempInFahrenheit) => }; // + // + string WaterState2(int tempInFahrenheit) => + tempInFahrenheit switch + { + < 32 => "solid", + 32 => "solid/liquid transition", + < 212 => "liquid", + 212 => "liquid / gas transition", + _ => "gas", + }; + // private State ResetToReady() => State.Ready; private State StopSystem() => State.Off; From 69ead81ccc2c0237c6e3644a433d482cc7246783 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 10:47:18 -0400 Subject: [PATCH 41/60] clean up Some samples don't run correctly in interactive mode. Turn off interactive mode where the sample uses features that aren't supported. --- .../object-oriented/snippets/objects/Application.cs | 4 ---- .../object-oriented/snippets/objects/Program.cs | 4 ---- docs/csharp/fundamentals/types/records.md | 6 +++--- docs/csharp/fundamentals/types/snippets/classes/Program.cs | 3 --- docs/csharp/toc.yml | 2 +- 5 files changed, 4 insertions(+), 15 deletions(-) diff --git a/docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs b/docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs index 569c1ff97d9c4..2baab36cae59f 100644 --- a/docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs +++ b/docs/csharp/fundamentals/object-oriented/snippets/objects/Application.cs @@ -33,10 +33,6 @@ static void Main() // p1 values remain unchanged because p2 is copy. Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age); - - // Keep the console open in debug mode. - Console.WriteLine("Press any key to exit."); - Console.ReadKey(); } } /* diff --git a/docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs b/docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs index 7e88a60df6a63..fd208fd6d57f4 100644 --- a/docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs +++ b/docs/csharp/fundamentals/object-oriented/snippets/objects/Program.cs @@ -28,10 +28,6 @@ static void Main() Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age); Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age); - - // Keep the console open in debug mode. - Console.WriteLine("Press any key to exit."); - Console.ReadKey(); } } /* diff --git a/docs/csharp/fundamentals/types/records.md b/docs/csharp/fundamentals/types/records.md index ca8dbf27bf323..8c6e906713176 100644 --- a/docs/csharp/fundamentals/types/records.md +++ b/docs/csharp/fundamentals/types/records.md @@ -43,15 +43,15 @@ The same syntax that [declares](classes.md#declaring-classes) and [instantiates] The following example defines a public record that uses positional parameters to declare and instantiate a record. It then prints the type name and property values: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" ID="InstantiatePositional"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" ID="InstantiatePositional"::: The following example demonstrates value equality in records: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" ID="Equality"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" ID="Equality"::: The following example demonstrates use of a `with` expression to copy an immutable object and change one of the properties: -:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" interactive="try-dotnet" ID="WithExpressions"::: +:::code language="csharp" source="../../language-reference/builtin-types/snippets/shared/RecordType.cs" ID="WithExpressions"::: For more information, see [Records (C# reference)](../../language-reference/builtin-types/record.md). diff --git a/docs/csharp/fundamentals/types/snippets/classes/Program.cs b/docs/csharp/fundamentals/types/snippets/classes/Program.cs index 6d804900cd5ad..2f7814d30c36e 100644 --- a/docs/csharp/fundamentals/types/snippets/classes/Program.cs +++ b/docs/csharp/fundamentals/types/snippets/classes/Program.cs @@ -36,9 +36,6 @@ static void Main() Console.WriteLine(person2.Name); // Get the string representation of the person2 instance. Console.WriteLine(person2); - - Console.WriteLine("Press any key to exit."); - Console.ReadKey(); } } // Output: diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index a7a688929eede..5b203e2c15bce 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -69,7 +69,7 @@ items: - name: Anonymous Types href: fundamentals/types/anonymous-types.md # TODO: Delegates, lambdas and events - - name: Tutorials + # TODO: Tutorials # TODO: DTOs (tuples, anonymous types and records) # TODO: Values (record structs and structs) # TODO: Classes and objects (interfaces and classes) From 02652d30e9e5dafd41f38b0f655f03e64969dbbf Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 10:51:46 -0400 Subject: [PATCH 42/60] replace redirected links cleanrepo --replace-links --- docs/core/introduction.md | 2 +- docs/core/whats-new/dotnet-core-3-0.md | 2 +- docs/csharp/delegate-class.md | 2 +- docs/csharp/delegates-strongly-typed.md | 2 +- .../fundamentals/object-oriented/objects.md | 2 +- ...using-pattern-matching-is-and-as-operators.md | 2 +- docs/csharp/fundamentals/types/classes.md | 4 ++-- docs/csharp/fundamentals/types/index.md | 4 ++-- docs/csharp/fundamentals/types/interfaces.md | 4 ++-- docs/csharp/fundamentals/types/namespaces.md | 2 +- docs/csharp/how-to/index.md | 10 +++++----- docs/csharp/iterators.md | 2 +- .../language-reference/builtin-types/record.md | 2 +- .../builtin-types/reference-types.md | 2 +- .../language-reference/builtin-types/struct.md | 2 +- .../builtin-types/value-tuples.md | 4 ++-- .../compiler-messages/cs0106.md | 2 +- .../compiler-messages/cs0116.md | 8 ++++---- .../compiler-messages/cs0120.md | 2 +- .../compiler-messages/cs0151.md | 2 +- .../compiler-messages/cs0229.md | 2 +- .../compiler-messages/cs1001.md | 2 +- .../compiler-messages/cs1058.md | 2 +- .../compiler-messages/cs1061.md | 2 +- .../compiler-messages/cs1519.md | 4 ++-- .../compiler-messages/cs1540.md | 4 ++-- .../compiler-messages/cs1721.md | 4 ++-- .../compiler-messages/cs1729.md | 2 +- .../compiler-options/advanced.md | 4 ++-- .../compiler-options/output.md | 10 +++++----- docs/csharp/language-reference/keywords/class.md | 4 ++-- docs/csharp/language-reference/keywords/event.md | 4 ++-- docs/csharp/language-reference/keywords/index.md | 2 +- .../language-reference/keywords/interface.md | 4 ++-- .../language-reference/keywords/namespace.md | 2 +- .../keywords/new-constraint.md | 2 +- .../language-reference/keywords/override.md | 6 +++--- .../language-reference/keywords/partial-type.md | 2 +- ...restrictions-on-using-accessibility-levels.md | 4 ++-- .../csharp/language-reference/keywords/sealed.md | 2 +- .../language-reference/keywords/select-clause.md | 2 +- .../csharp/language-reference/keywords/switch.md | 2 +- .../keywords/using-directive.md | 2 +- .../language-reference/keywords/using-static.md | 2 +- docs/csharp/language-reference/keywords/using.md | 2 +- .../language-reference/keywords/virtual.md | 2 +- .../keywords/where-generic-type-constraint.md | 2 +- .../csharp/language-reference/operators/await.md | 4 ++-- .../operators/delegate-operator.md | 2 +- docs/csharp/language-reference/operators/is.md | 2 +- .../operators/lambda-expressions.md | 2 +- .../operators/member-access-operators.md | 2 +- .../language-reference/operators/new-operator.md | 2 +- .../language-reference/operators/patterns.md | 6 +++--- .../operators/switch-expression.md | 2 +- .../operators/type-testing-and-cast.md | 2 +- docs/csharp/linq/group-query-results.md | 2 +- docs/csharp/linq/perform-grouped-joins.md | 2 +- docs/csharp/linq/perform-inner-joins.md | 2 +- docs/csharp/linq/perform-left-outer-joins.md | 2 +- docs/csharp/methods.md | 2 +- docs/csharp/misc/cs0017.md | 2 +- docs/csharp/misc/cs0023.md | 2 +- docs/csharp/misc/cs0026.md | 2 +- docs/csharp/misc/cs0028.md | 2 +- docs/csharp/misc/cs0031.md | 2 +- docs/csharp/misc/cs0061.md | 2 +- docs/csharp/misc/cs0068.md | 2 +- docs/csharp/misc/cs0069.md | 2 +- docs/csharp/misc/cs0080.md | 2 +- docs/csharp/misc/cs0081.md | 2 +- docs/csharp/misc/cs0101.md | 2 +- docs/csharp/misc/cs0138.md | 2 +- docs/csharp/misc/cs0144.md | 2 +- docs/csharp/misc/cs0146.md | 2 +- docs/csharp/misc/cs0155.md | 2 +- docs/csharp/misc/cs0156.md | 2 +- docs/csharp/misc/cs0157.md | 2 +- docs/csharp/misc/cs0160.md | 2 +- docs/csharp/misc/cs0238.md | 2 +- docs/csharp/misc/cs0255.md | 2 +- docs/csharp/misc/cs0426.md | 2 +- docs/csharp/misc/cs0666.md | 2 +- docs/csharp/misc/cs0736.md | 2 +- docs/csharp/misc/cs0737.md | 2 +- docs/csharp/misc/cs0738.md | 2 +- docs/csharp/misc/cs0746.md | 2 +- docs/csharp/misc/cs0833.md | 2 +- docs/csharp/misc/cs1015.md | 2 +- docs/csharp/misc/cs1017.md | 2 +- docs/csharp/misc/cs1524.md | 2 +- docs/csharp/misc/cs1527.md | 2 +- docs/csharp/misc/cs1558.md | 2 +- docs/csharp/misc/cs1559.md | 2 +- docs/csharp/misc/cs1913.md | 2 +- docs/csharp/misc/cs5001.md | 2 +- .../arrays/implicitly-typed-arrays.md | 2 +- ...tract-and-sealed-classes-and-class-members.md | 4 ++-- .../classes-and-structs/access-modifiers.md | 4 ++-- .../classes-and-structs/constants.md | 4 ++-- .../classes-and-structs/constructors.md | 2 +- .../classes-and-structs/extension-methods.md | 2 +- .../classes-and-structs/fields.md | 4 ++-- ...w-to-declare-and-use-read-write-properties.md | 2 +- .../how-to-define-abstract-properties.md | 2 +- .../how-to-define-constants.md | 2 +- ...g-a-struct-and-passing-a-class-to-a-method.md | 2 +- .../how-to-override-the-tostring-method.md | 2 +- ...n-subsets-of-element-properties-in-a-query.md | 2 +- ...variables-and-arrays-in-a-query-expression.md | 2 +- .../how-to-write-a-copy-constructor.md | 6 +++--- .../implicitly-typed-local-variables.md | 6 +++--- .../classes-and-structs/instance-constructors.md | 2 +- .../classes-and-structs/interface-properties.md | 2 +- ...wing-when-to-use-override-and-new-keywords.md | 2 +- .../classes-and-structs/members.md | 2 +- .../classes-and-structs/methods.md | 8 ++++---- .../classes-and-structs/nested-types.md | 2 +- .../object-and-collection-initializers.md | 4 ++-- .../partial-classes-and-methods.md | 4 ++-- .../classes-and-structs/private-constructors.md | 2 +- .../static-classes-and-static-class-members.md | 2 +- .../classes-and-structs/static-constructors.md | 2 +- .../classes-and-structs/using-constructors.md | 2 +- .../classes-and-structs/using-properties.md | 2 +- ...sioning-with-the-override-and-new-keywords.md | 4 ++-- .../programming-guide/concepts/iterators.md | 2 +- .../concepts/linq/basic-linq-query-operations.md | 2 +- .../linq/data-transformations-with-linq.md | 2 +- .../concepts/linq/features-that-support-linq.md | 2 +- ...ect-collections-from-multiple-sources-linq.md | 2 +- .../concepts/linq/join-operations.md | 2 +- .../concepts/linq/linq-and-generic-types.md | 4 ++-- .../generics/constraints-on-type-parameters.md | 2 +- ...-between-cpp-templates-and-csharp-generics.md | 2 +- .../generics/generic-classes.md | 4 ++-- .../generics/generic-delegates.md | 2 +- .../generics/generic-interfaces.md | 4 ++-- .../generics/generic-methods.md | 2 +- .../generics/generic-type-parameters.md | 4 ++-- .../generics/generics-and-arrays.md | 2 +- .../generics/generics-and-attributes.md | 2 +- .../generics/generics-and-reflection.md | 2 +- .../generics/generics-in-the-run-time.md | 2 +- docs/csharp/programming-guide/index.md | 16 ++++++++-------- .../indexers/indexers-in-interfaces.md | 2 +- .../explicit-interface-implementation.md | 6 +++--- ...-to-explicitly-implement-interface-members.md | 4 ++-- ...icitly-implement-members-of-two-interfaces.md | 4 ++-- .../namespaces/how-to-use-the-my-namespace.md | 2 +- .../namespaces/using-namespaces.md | 2 +- .../equality-comparisons.md | 4 ++-- .../how-to-define-value-equality-for-a-type.md | 2 +- .../types/casting-and-type-conversions.md | 6 +++--- .../how-to-convert-a-byte-array-to-an-int.md | 2 +- ...ween-hexadecimal-strings-and-numeric-types.md | 2 +- .../csharp/programming-guide/xmldoc/exception.md | 2 +- .../csharp/programming-guide/xmldoc/typeparam.md | 2 +- .../programming-guide/xmldoc/typeparamref.md | 2 +- docs/csharp/toc.yml | 8 ++++---- docs/csharp/tour-of-csharp/features.md | 4 ++-- docs/csharp/tour-of-csharp/index.md | 4 ++-- .../tutorials/arrays-and-collections.md | 2 +- docs/csharp/tour-of-csharp/types.md | 4 ++-- docs/csharp/tutorials/console-webapiclient.md | 2 +- .../exploration/interpolated-strings-local.md | 2 +- docs/csharp/whats-new/csharp-7.md | 6 +++--- docs/csharp/whats-new/csharp-8.md | 2 +- docs/csharp/whats-new/csharp-9.md | 2 +- docs/csharp/whats-new/csharp-version-history.md | 8 ++++---- .../mixins-with-default-interface-methods.md | 2 +- .../whats-new/tutorials/top-level-statements.md | 2 +- docs/csharp/write-safe-efficient-code.md | 2 +- .../language-reference/anonymous-records.md | 2 +- .../code-analysis/quality-rules/ca1000.md | 2 +- .../code-analysis/quality-rules/ca1002.md | 2 +- .../code-analysis/quality-rules/ca1003.md | 2 +- .../code-analysis/quality-rules/ca1005.md | 2 +- .../code-analysis/quality-rules/ca1010.md | 2 +- .../code-analysis/quality-rules/ca1033.md | 2 +- .../code-analysis/quality-rules/ca1801.md | 2 +- .../code-analysis/quality-rules/ca2119.md | 2 +- .../code-analysis/style-rules/ide0019.md | 4 ++-- .../code-analysis/style-rules/ide0020-ide0038.md | 4 ++-- .../code-analysis/style-rules/ide0050.md | 4 ++-- .../code-analysis/style-rules/ide0058.md | 4 ++-- .../code-analysis/style-rules/ide0059.md | 4 ++-- .../code-analysis/style-rules/ide0060.md | 2 +- .../style-rules/pattern-matching-preferences.md | 2 +- .../choosing-between-anonymous-and-tuple.md | 4 ++-- docs/standard/generics.md | 2 +- .../generics/covariance-and-contravariance.md | 2 +- docs/standard/generics/index.md | 4 ++-- ...epts-terminology-functional-transformation.md | 2 +- docs/standard/linq/project-anonymous-type.md | 4 ++-- docs/whats-new/2021-03.md | 4 ++-- 196 files changed, 276 insertions(+), 276 deletions(-) diff --git a/docs/core/introduction.md b/docs/core/introduction.md index fcf52f6e28ccc..8449e35408e36 100644 --- a/docs/core/introduction.md +++ b/docs/core/introduction.md @@ -91,7 +91,7 @@ For more information, see [Releases and support for .NET Core and .NET 5](releas Here are some of the capabilities that .NET languages support: * [Type safety](../standard/base-types/common-type-system.md) -* Type inference - [C#](../csharp/programming-guide/types/index.md#specifying-types-in-variable-declarations), [F#](../fsharp/language-reference/type-inference.md), [Visual Basic](../visual-basic/programming-guide/language-features/variables/local-type-inference.md) +* Type inference - [C#](/dotnet/csharp/fundamentals/types#specifying-types-in-variable-declarations), [F#](../fsharp/language-reference/type-inference.md), [Visual Basic](../visual-basic/programming-guide/language-features/variables/local-type-inference.md) * [Generic types](../standard/generics.md) * [Delegates](../standard/delegates-lambdas.md) * [Lambdas](../standard/delegates-lambdas.md) diff --git a/docs/core/whats-new/dotnet-core-3-0.md b/docs/core/whats-new/dotnet-core-3-0.md index 3af8b40f5e1f1..32ff3406b7bc3 100644 --- a/docs/core/whats-new/dotnet-core-3-0.md +++ b/docs/core/whats-new/dotnet-core-3-0.md @@ -28,7 +28,7 @@ Tutorials related to C# 8.0 language features: - [Tutorial: Express your design intent more clearly with nullable and non-nullable reference types](../../csharp/whats-new/tutorials/nullable-reference-types.md) - [Tutorial: Generate and consume async streams using C# 8.0 and .NET Core 3.0](../../csharp/whats-new/tutorials/generate-consume-asynchronous-stream.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../csharp/tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) Language enhancements were added to support the following API features detailed below: diff --git a/docs/csharp/delegate-class.md b/docs/csharp/delegate-class.md index b30942bf2de6b..74059db4e6646 100644 --- a/docs/csharp/delegate-class.md +++ b/docs/csharp/delegate-class.md @@ -38,7 +38,7 @@ The compiler generates a class, derived from `System.Delegate` that matches the signature used (in this case, a method that returns an integer, and has two arguments). The type of that delegate is `Comparison`. The `Comparison` delegate -type is a generic type. For details on generics see [here](programming-guide/generics/index.md). +type is a generic type. For details on generics see [here](/dotnet/csharp/fundamentals/types/generics). Notice that the syntax may appear as though it is declaring a variable, but it is actually declaring a *type*. You can diff --git a/docs/csharp/delegates-strongly-typed.md b/docs/csharp/delegates-strongly-typed.md index 58e790ea27916..c2fb1cb429b5d 100644 --- a/docs/csharp/delegates-strongly-typed.md +++ b/docs/csharp/delegates-strongly-typed.md @@ -25,7 +25,7 @@ after a time. Every new feature requires new delegate types. Thankfully, this isn't necessary. The .NET Core framework contains several types that you can reuse whenever you need delegate types. These are -[generic](programming-guide/generics/index.md) definitions so you can declare customizations +[generic](/dotnet/csharp/fundamentals/types/generics) definitions so you can declare customizations when you need new method declarations. The first of these types is the type, and several variations: diff --git a/docs/csharp/fundamentals/object-oriented/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md index 839b7930abed7..c02b9fe4522a6 100644 --- a/docs/csharp/fundamentals/object-oriented/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -47,7 +47,7 @@ When you compare two objects for equality, you must first distinguish whether yo For more information: -- [Classes](../../programming-guide/classes-and-structs/classes.md) +- [Classes](/dotnet/csharp/fundamentals/types/classes) - [Constructors](../../programming-guide/classes-and-structs/constructors.md) - [Finalizers](../../programming-guide/classes-and-structs/destructors.md) - [Events](../../programming-guide/events/index.md) diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md index 08f3e8564da3d..ecb45bc5c38a7 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md @@ -9,7 +9,7 @@ helpviewer_keywords: --- # How to safely cast by using pattern matching and the is and as operators -Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](../../../programming-guide/types/index.md). To access the derived type's instance members, it's necessary to [cast](../../../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](../../../pattern-matching.md) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../../../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../../../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. +Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](/dotnet/csharp/fundamentals/types). To access the derived type's instance members, it's necessary to [cast](../../../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../../../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../../../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. The following example shows how to use the pattern matching `is` statement: diff --git a/docs/csharp/fundamentals/types/classes.md b/docs/csharp/fundamentals/types/classes.md index baad55e18adfe..c7341a7390111 100644 --- a/docs/csharp/fundamentals/types/classes.md +++ b/docs/csharp/fundamentals/types/classes.md @@ -34,7 +34,7 @@ When the object is created, enough memory is allocated on the managed heap for t } ``` - The `class` keyword is preceded by the access level. Because [`public`](../../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 name of the class must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). 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*. + The `class` keyword is preceded by the access level. Because [`public`](../../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 name of the class must be a valid C# [identifier name](/dotnet/csharp/fundamentals/coding-style/identifier-names). 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 @@ -75,7 +75,7 @@ Inheritance is accomplished by using a *derivation*, which means a class is decl } ``` -When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). +When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). 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 one or more interfaces. For more information, see [Interfaces](interfaces.md). diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 3431611996bef..1e01113aed133 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -60,7 +60,7 @@ You use the [`struct`](../../language-reference/builtin-types/struct.md), [`clas It's important to understand two fundamental points about the type system in .NET: -- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: [`object`](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). +- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: [`object`](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). - Each type in the CTS is defined as either a *value type* or a *reference type*. These types include all custom types in the .NET class library and also your own user-defined types. Types that you define by using the `struct` keyword are value types; all the built-in numeric types are `structs`. Types that you define by using the `class` or `record` keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior. The following illustration shows the relationship between value types and reference types in the CTS. @@ -116,7 +116,7 @@ All arrays are reference types, even if their elements are value types. Arrays i :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="ArrayDeclaration"::: -Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md). For more information about inheritance and virtual methods, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). +Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented). For more information about inheritance and virtual methods, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). ## Types of literal values diff --git a/docs/csharp/fundamentals/types/interfaces.md b/docs/csharp/fundamentals/types/interfaces.md index 5685a030c426b..0d6e52fe5922e 100644 --- a/docs/csharp/fundamentals/types/interfaces.md +++ b/docs/csharp/fundamentals/types/interfaces.md @@ -16,7 +16,7 @@ You define an interface by using the [`interface`](../../language-reference/keyw :::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" ID="Equatable"::: -The name of an interface must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). By convention, interface names begin with a capital `I`. +The name of an interface must be a valid C# [identifier name](/dotnet/csharp/fundamentals/coding-style/identifier-names). By convention, interface names begin with a capital `I`. Any class or struct that implements the interface must contain a definition for an method that matches the signature that the interface specifies. As a result, you can count on a class that implements `IEquatable` to contain an `Equals` method with which an instance of the class can determine whether it's equal to another instance of the same class. @@ -38,7 +38,7 @@ Properties and indexers of a class can define extra accessors for a property or Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces. That class may be implicitly converted to the derived interface or any of its base interfaces. A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces inherit. However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (`class ClassName : InterfaceName`). If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface. However, the derived class can reimplement any virtual interface members instead of using the inherited implementation. When interfaces declare a default implementation of a method, any class implementing that interface inherits that implementation. Implementations defined in interfaces are virtual and the implementing class may override that implementation. -A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members. For more information about virtual members, see [Polymorphism](../../programming-guide/classes-and-structs/polymorphism.md). +A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members. For more information about virtual members, see [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism). ## Interfaces summary diff --git a/docs/csharp/fundamentals/types/namespaces.md b/docs/csharp/fundamentals/types/namespaces.md index ccfc7ad36584c..ad240ee5e2ef6 100644 --- a/docs/csharp/fundamentals/types/namespaces.md +++ b/docs/csharp/fundamentals/types/namespaces.md @@ -24,7 +24,7 @@ Second, declaring your own namespaces can help you control the scope of class an [!code-csharp[csProgGuideNamespaces#6](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs#6)] -The name of the namespace must be a valid C# [identifier name](../../programming-guide/inside-a-program/identifier-names.md). +The name of the namespace must be a valid C# [identifier name](/dotnet/csharp/fundamentals/coding-style/identifier-names). ## Namespaces overview diff --git a/docs/csharp/how-to/index.md b/docs/csharp/how-to/index.md index 54dc98cf8e475..d896452c1f8ce 100644 --- a/docs/csharp/how-to/index.md +++ b/docs/csharp/how-to/index.md @@ -59,7 +59,7 @@ You may need to convert an object to a different type. - [Convert a string to a `DateTime`](../../standard/base-types/parsing-datetime.md). - [Convert a byte array to an int](../programming-guide/types/how-to-convert-a-byte-array-to-an-int.md). - [Convert a string to a number](../programming-guide/types/how-to-convert-a-string-to-a-number.md). -- [Use pattern matching, the `as` and `is` operators to safely cast to a different type](safely-cast-using-pattern-matching-is-and-as-operators.md). +- [Use pattern matching, the `as` and `is` operators to safely cast to a different type](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). - [Define custom type conversions](../language-reference/operators/user-defined-conversion-operators.md). - [Determine if a type is a nullable value type](../language-reference/builtin-types/nullable-value-types.md#how-to-identify-a-nullable-value-type). - [Convert between nullable and non-nullable value types](../language-reference/builtin-types/nullable-value-types.md#conversion-from-a-nullable-value-type-to-an-underlying-type). @@ -78,9 +78,9 @@ natural ordering among objects of that type. successfully complete their work by throwing exceptions. In these articles you'll learn to work with exceptions. -- [Handle exceptions using `try` and `catch`](../programming-guide/exceptions/how-to-handle-an-exception-using-try-catch.md). -- [Cleanup resources using `finally` clauses](../programming-guide/exceptions/how-to-execute-cleanup-code-using-finally.md). -- [Recover from non-CLS (Common Language Specification) exceptions](../programming-guide/exceptions/how-to-catch-a-non-cls-exception.md). +- [Handle exceptions using `try` and `catch`](/dotnet/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch). +- [Cleanup resources using `finally` clauses](/dotnet/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally). +- [Recover from non-CLS (Common Language Specification) exceptions](/dotnet/csharp/how-to/how-to-catch-a-non-cls-exception). ## Delegates and events @@ -133,4 +133,4 @@ to use these techniques. Typically, C# programs have command line arguments. These articles teach you to access and process those command line arguments. -- [Retrieve all command line arguments with `for`](../programming-guide/main-and-command-args/how-to-display-command-line-arguments.md). +- [Retrieve all command line arguments with `for`](/dotnet/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments). diff --git a/docs/csharp/iterators.md b/docs/csharp/iterators.md index e63d69da945a6..bb010aba45f62 100644 --- a/docs/csharp/iterators.md +++ b/docs/csharp/iterators.md @@ -47,7 +47,7 @@ to generate the code necessary to iterate a collection: `IEnumerable` and `IEnumerator`. This mechanism is explained in more detail below. Both of these interfaces also have non-generic counterparts: `IEnumerable` and -`IEnumerator`. The [generic](programming-guide/generics/index.md) versions are preferred for modern code. +`IEnumerator`. The [generic](/dotnet/csharp/fundamentals/types/generics) versions are preferred for modern code. ## Enumeration sources with iterator methods diff --git a/docs/csharp/language-reference/builtin-types/record.md b/docs/csharp/language-reference/builtin-types/record.md index b7f10dea6ad6d..d156be1690fe9 100644 --- a/docs/csharp/language-reference/builtin-types/record.md +++ b/docs/csharp/language-reference/builtin-types/record.md @@ -204,5 +204,5 @@ For more information about features introduced in C# 9 and later, see the follow - [C# reference](../index.md) - [Design guidelines - Choosing between class and struct](../../../standard/design-guidelines/choosing-between-class-and-struct.md) - [Design guidelines - Struct design](../../../standard/design-guidelines/struct.md) -- [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [`with` expression](../operators/with-expression.md) diff --git a/docs/csharp/language-reference/builtin-types/reference-types.md b/docs/csharp/language-reference/builtin-types/reference-types.md index 55c5adf134aee..d4b81ea4ae1c7 100644 --- a/docs/csharp/language-reference/builtin-types/reference-types.md +++ b/docs/csharp/language-reference/builtin-types/reference-types.md @@ -172,7 +172,7 @@ The following example uses `dynamic` in several declarations. The `Main` method - [Basic String Operations](../../../standard/base-types/basic-string-operations.md) - [Creating New Strings](../../../standard/base-types/creating-new.md) - [Type-testing and cast operators](../operators/type-testing-and-cast.md) -- [How to safely cast using pattern matching and the as and is operators](../../how-to/safely-cast-using-pattern-matching-is-and-as-operators.md) +- [How to safely cast using pattern matching and the as and is operators](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) - [Walkthrough: creating and using dynamic objects](../../programming-guide/types/walkthrough-creating-and-using-dynamic-objects.md) - - diff --git a/docs/csharp/language-reference/builtin-types/struct.md b/docs/csharp/language-reference/builtin-types/struct.md index b2d7d9a54a0ae..eb3ff579411ba 100644 --- a/docs/csharp/language-reference/builtin-types/struct.md +++ b/docs/csharp/language-reference/builtin-types/struct.md @@ -147,4 +147,4 @@ For more information about features introduced in C# 7.2 and later, see the foll - [C# reference](../index.md) - [Design guidelines - Choosing between class and struct](../../../standard/design-guidelines/choosing-between-class-and-struct.md) - [Design guidelines - Struct design](../../../standard/design-guidelines/struct.md) -- [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) diff --git a/docs/csharp/language-reference/builtin-types/value-tuples.md b/docs/csharp/language-reference/builtin-types/value-tuples.md index e2545ed68a860..49bc04158dcce 100644 --- a/docs/csharp/language-reference/builtin-types/value-tuples.md +++ b/docs/csharp/language-reference/builtin-types/value-tuples.md @@ -34,7 +34,7 @@ One of the most common use cases of tuples is as a method return type. That is, As the preceding example shows, you can work with the returned tuple instance directly or [deconstruct](#tuple-assignment-and-deconstruction) it in separate variables. -You can also use tuple types instead of [anonymous types](../../programming-guide/classes-and-structs/anonymous-types.md); for example, in LINQ queries. For more information, see [Choosing between anonymous and tuple types](../../../standard/base-types/choosing-between-anonymous-and-tuple.md). +You can also use tuple types instead of [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types); for example, in LINQ queries. For more information, see [Choosing between anonymous and tuple types](../../../standard/base-types/choosing-between-anonymous-and-tuple.md). Typically, you use tuples to group loosely related data elements. That is usually useful within private and internal utility methods. In the case of public API, consider defining a [class](../keywords/class.md) or a [structure](struct.md) type. @@ -88,7 +88,7 @@ You can also use the assignment operator `=` to *deconstruct* a tuple instance i [!code-csharp-interactive[existing variables](snippets/shared/ValueTuples.cs#DeconstructExisting)] -For more information about deconstruction of tuples and other types, see [Deconstructing tuples and other types](../../deconstruct.md). +For more information about deconstruction of tuples and other types, see [Deconstructing tuples and other types](/dotnet/csharp/fundamentals/functional/deconstruct). ## Tuple equality diff --git a/docs/csharp/language-reference/compiler-messages/cs0106.md b/docs/csharp/language-reference/compiler-messages/cs0106.md index a060c044d1f20..b2ced15e1deaf 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0106.md +++ b/docs/csharp/language-reference/compiler-messages/cs0106.md @@ -24,7 +24,7 @@ The modifier 'modifier' is not valid for this item In prior releases of Visual Studio, the `static` modifier was not permitted on a class, but `static` classes are allowed starting with Visual Studio 2005. - For more information, see [Interfaces](../../programming-guide/interfaces/index.md). + For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). ## Example diff --git a/docs/csharp/language-reference/compiler-messages/cs0116.md b/docs/csharp/language-reference/compiler-messages/cs0116.md index f81680dc57e9a..92c28ff676f62 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0116.md +++ b/docs/csharp/language-reference/compiler-messages/cs0116.md @@ -58,7 +58,7 @@ namespace x } ``` -Note that in C#, methods and variables must be declared and defined within a struct or class. For more information on program structure in C#, see the [General Structure of a C# Program](../../programming-guide/inside-a-program/general-structure-of-a-csharp-program.md) article. To fix this error, rewrite your code such that all methods and fields are contained within either a struct or a class: +Note that in C#, methods and variables must be declared and defined within a struct or class. For more information on program structure in C#, see the [General Structure of a C# Program](/dotnet/csharp/fundamentals/program-structure) article. To fix this error, rewrite your code such that all methods and fields are contained within either a struct or a class: ```csharp namespace x @@ -99,6 +99,6 @@ namespace x ## See also -- [General Structure of a C# Program](../../programming-guide/inside-a-program/general-structure-of-a-csharp-program.md) -- [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md) -- [Namespaces](../../programming-guide/namespaces/index.md) +- [General Structure of a C# Program](/dotnet/csharp/fundamentals/program-structure) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) +- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) diff --git a/docs/csharp/language-reference/compiler-messages/cs0120.md b/docs/csharp/language-reference/compiler-messages/cs0120.md index cf5657cab3595..22b75108d6587 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0120.md +++ b/docs/csharp/language-reference/compiler-messages/cs0120.md @@ -96,4 +96,4 @@ public class MyClass ## See also -- [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) diff --git a/docs/csharp/language-reference/compiler-messages/cs0151.md b/docs/csharp/language-reference/compiler-messages/cs0151.md index 773c9a6e637c5..31ab99e1c95bb 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0151.md +++ b/docs/csharp/language-reference/compiler-messages/cs0151.md @@ -12,7 +12,7 @@ ms.assetid: 1adda08b-6be5-46c8-96f9-5ac7c7bfe48c A value of an integral type expected -A variable was used in a situation where an integral data type was required. For more information, see [Types](../../programming-guide/types/index.md). +A variable was used in a situation where an integral data type was required. For more information, see [Types](/dotnet/csharp/fundamentals/types). ## Example of ambiguous conversion diff --git a/docs/csharp/language-reference/compiler-messages/cs0229.md b/docs/csharp/language-reference/compiler-messages/cs0229.md index c463fe5be87e1..5a9a0edf2f3ba 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0229.md +++ b/docs/csharp/language-reference/compiler-messages/cs0229.md @@ -12,7 +12,7 @@ ms.assetid: f1ff7e91-1243-4d36-b792-26ba69186f8f Ambiguity between 'member1' and 'member2' - Members of different interfaces have the same name. If you want to keep the same names, you must qualify the names. For more information, see [Interfaces](../../programming-guide/interfaces/index.md). + Members of different interfaces have the same name. If you want to keep the same names, you must qualify the names. For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). > [!NOTE] > In some cases, this ambiguity can be resolved by providing an explicit prefix to the identifier via a [using](../keywords/using-directive.md) alias. diff --git a/docs/csharp/language-reference/compiler-messages/cs1001.md b/docs/csharp/language-reference/compiler-messages/cs1001.md index 9112f4fce3290..b4ab9ff9bc3f5 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1001.md +++ b/docs/csharp/language-reference/compiler-messages/cs1001.md @@ -63,4 +63,4 @@ class CMyTest : IMyTest ## See also - [Statements, Expressions, and Operators](../../programming-guide/statements-expressions-operators/index.md) -- [Types](../../programming-guide/types/index.md) +- [Types](/dotnet/csharp/fundamentals/types) diff --git a/docs/csharp/language-reference/compiler-messages/cs1058.md b/docs/csharp/language-reference/compiler-messages/cs1058.md index e9c854fabbfc1..d0311c99a1734 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1058.md +++ b/docs/csharp/language-reference/compiler-messages/cs1058.md @@ -14,7 +14,7 @@ A previous catch clause already catches all exceptions. All exceptions thrown wi This attribute causes CS1058 if a `catch()` block has no specified exception type after a `catch (System.Exception e)` block. The warning advises that the `catch()` block will not catch any exceptions. -A `catch()` block after a `catch (System.Exception e)` block can catch non-CLS exceptions if the `RuntimeCompatibilityAttribute` is set to false in the AssemblyInfo.cs file: `[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]`. If this attribute is not set explicitly to false, all thrown non-CLS exceptions are wrapped as Exceptions and the `catch (System.Exception e)` block catches them. For more information, see [How to catch a non-CLS exception](../../programming-guide/exceptions/how-to-catch-a-non-cls-exception.md). +A `catch()` block after a `catch (System.Exception e)` block can catch non-CLS exceptions if the `RuntimeCompatibilityAttribute` is set to false in the AssemblyInfo.cs file: `[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]`. If this attribute is not set explicitly to false, all thrown non-CLS exceptions are wrapped as Exceptions and the `catch (System.Exception e)` block catches them. For more information, see [How to catch a non-CLS exception](/dotnet/csharp/how-to/how-to-catch-a-non-cls-exception). ## Example diff --git a/docs/csharp/language-reference/compiler-messages/cs1061.md b/docs/csharp/language-reference/compiler-messages/cs1061.md index 6b3dc4bbcc99c..2525823bd0e2d 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1061.md +++ b/docs/csharp/language-reference/compiler-messages/cs1061.md @@ -52,5 +52,5 @@ public class Program ## See also -- [Classes, structs, and records](../../programming-guide/classes-and-structs/index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Extension Methods](../../programming-guide/classes-and-structs/extension-methods.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs1519.md b/docs/csharp/language-reference/compiler-messages/cs1519.md index 2b6414ceb3a6b..baa2dda873ac0 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1519.md +++ b/docs/csharp/language-reference/compiler-messages/cs1519.md @@ -40,7 +40,7 @@ class Test 42 ## See also -- [Classes](../../programming-guide/classes-and-structs/classes.md) +- [Classes](/dotnet/csharp/fundamentals/types/classes) - [Structure types](../builtin-types/struct.md) -- [Interfaces](../../programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) - [Methods](../../programming-guide/classes-and-structs/methods.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs1540.md b/docs/csharp/language-reference/compiler-messages/cs1540.md index 8c087b2a8e91d..e7a42153f3173 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1540.md +++ b/docs/csharp/language-reference/compiler-messages/cs1540.md @@ -71,7 +71,7 @@ namespace CS1540 ## See also -- [Inheritance](../../programming-guide/classes-and-structs/inheritance.md) -- [Polymorphism](../../programming-guide/classes-and-structs/polymorphism.md) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism) - [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md) - [Abstract and Sealed Classes and Class Members](../../programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs1721.md b/docs/csharp/language-reference/compiler-messages/cs1721.md index 6ca9f28a705dc..3ffa31c2349ed 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1721.md +++ b/docs/csharp/language-reference/compiler-messages/cs1721.md @@ -47,5 +47,5 @@ The following are different ways to correct this error: ## See also -- [Polymorphism](../../programming-guide/classes-and-structs/polymorphism.md) -- [Interfaces](../../programming-guide/interfaces/index.md) +- [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) diff --git a/docs/csharp/language-reference/compiler-messages/cs1729.md b/docs/csharp/language-reference/compiler-messages/cs1729.md index 721a68c4eb514..7687d10f6e058 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1729.md +++ b/docs/csharp/language-reference/compiler-messages/cs1729.md @@ -72,5 +72,5 @@ public class Child2 : Parent ## See also -- [Inheritance](../../programming-guide/classes-and-structs/inheritance.md) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) - [Constructors](../../programming-guide/classes-and-structs/constructors.md) diff --git a/docs/csharp/language-reference/compiler-options/advanced.md b/docs/csharp/language-reference/compiler-options/advanced.md index a0cbb5a86075d..7988780ab4937 100644 --- a/docs/csharp/language-reference/compiler-options/advanced.md +++ b/docs/csharp/language-reference/compiler-options/advanced.md @@ -58,10 +58,10 @@ or MyNamespace.Program ``` -Where `Program` is the type that contains the `Main` method. The provided class name must be fully qualified; it must include the full namespace containing the class, followed by the class name. For example, when the `Main` method is located inside the `Program` class in the `MyApplication.Core` namespace, the compiler option has to be `-main:MyApplication.Core.Program`. If your compilation includes more than one type with a [`Main`](../../programming-guide/main-and-command-args/index.md) method, you can specify which type contains the `Main` method. +Where `Program` is the type that contains the `Main` method. The provided class name must be fully qualified; it must include the full namespace containing the class, followed by the class name. For example, when the `Main` method is located inside the `Program` class in the `MyApplication.Core` namespace, the compiler option has to be `-main:MyApplication.Core.Program`. If your compilation includes more than one type with a [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method, you can specify which type contains the `Main` method. > [!NOTE] -> This option can't be used for a project that includes [top-level statements](../../programming-guide/main-and-command-args/top-level-statements.md), even if that project contains one or more `Main` methods. +> This option can't be used for a project that includes [top-level statements](/dotnet/csharp/fundamentals/program-structure/top-level-statements), even if that project contains one or more `Main` methods. ## PdbFile diff --git a/docs/csharp/language-reference/compiler-options/output.md b/docs/csharp/language-reference/compiler-options/output.md index 040f5c57283d5..61dc345e40361 100644 --- a/docs/csharp/language-reference/compiler-options/output.md +++ b/docs/csharp/language-reference/compiler-options/output.md @@ -116,11 +116,11 @@ If you create an assembly, you can indicate that all or part of your code is CLS ### library -The **library** option causes the compiler to create a dynamic-link library (DLL) rather than an executable file (EXE). The DLL will be created with the *.dll* extension. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. When building a *.dll* file, a [`Main`](../../programming-guide/main-and-command-args/index.md) method isn't required. +The **library** option causes the compiler to create a dynamic-link library (DLL) rather than an executable file (EXE). The DLL will be created with the *.dll* extension. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. When building a *.dll* file, a [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method isn't required. ### exe -The **exe** option causes the compiler to create an executable (EXE), console application. The executable file will be created with the .exe extension. Use **winexe** to create a Windows program executable. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the entry point ([Main](../../programming-guide/main-and-command-args/index.md) method or top-level statements). One and only one entry point is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) compiler option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. +The **exe** option causes the compiler to create an executable (EXE), console application. The executable file will be created with the .exe extension. Use **winexe** to create a Windows program executable. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the entry point ([Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method or top-level statements). One and only one entry point is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) compiler option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. ### module @@ -128,16 +128,16 @@ This option causes the compiler to not generate an assembly manifest. By default ### winexe -The **winexe** option causes the compiler to create an executable (EXE), Windows program. The executable file will be created with the .exe extension. A Windows program is one that provides a user interface from either the .NET library or with the Windows APIs. Use **exe** to create a console application. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](../../programming-guide/main-and-command-args/index.md) method. One and only one `Main` method is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. +The **winexe** option causes the compiler to create an executable (EXE), Windows program. The executable file will be created with the .exe extension. A Windows program is one that provides a user interface from either the .NET library or with the Windows APIs. Use **exe** to create a console application. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. One and only one `Main` method is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. ### winmdobj If you use the **winmdobj** option, the compiler creates an intermediate *.winmdobj* file that you can convert to a Windows Runtime binary (*.winmd*) file. The *.winmd* file can then be consumed by JavaScript and C++ programs, in addition to managed language programs. -The **winmdobj** setting signals to the compiler that an intermediate module is required. The *.winmdobj* file can then be fed through the export tool to produce a Windows metadata (*.winmd*) file. The *.winmd* file contains both the code from the original library and the WinMD metadata that is used by JavaScript or C++ and by the Windows Runtime. The output of a file that’s compiled by using the **winmdobj** compiler option is used only as input for the WimMDExp export tool. The *.winmdobj* file itself isn’t referenced directly. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. A [`Main`](../../programming-guide/main-and-command-args/index.md) method isn’t required. +The **winmdobj** setting signals to the compiler that an intermediate module is required. The *.winmdobj* file can then be fed through the export tool to produce a Windows metadata (*.winmd*) file. The *.winmd* file contains both the code from the original library and the WinMD metadata that is used by JavaScript or C++ and by the Windows Runtime. The output of a file that’s compiled by using the **winmdobj** compiler option is used only as input for the WimMDExp export tool. The *.winmdobj* file itself isn’t referenced directly. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. A [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method isn’t required. ### appcontainerexe If you use the **appcontainerexe** compiler option, the compiler creates a Windows executable (*.exe*) file that must be run in an app container. This option is equivalent to [-target:winexe](output.md) but is designed for Windows 8.x Store apps. -To require the app to run in an app container, this option sets a bit in the [Portable Executable](/windows/desktop/Debug/pe-format) (PE) file. When that bit is set, an error occurs if the CreateProcess method tries to launch the executable file outside an app container. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](../../programming-guide/main-and-command-args/index.md) method. +To require the app to run in an app container, this option sets a bit in the [Portable Executable](/windows/desktop/Debug/pe-format) (PE) file. When that bit is set, an error occurs if the CreateProcess method tries to launch the executable file outside an app container. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. diff --git a/docs/csharp/language-reference/keywords/class.md b/docs/csharp/language-reference/keywords/class.md index 5bb5aa534d131..a3130e52be571 100644 --- a/docs/csharp/language-reference/keywords/class.md +++ b/docs/csharp/language-reference/keywords/class.md @@ -62,9 +62,9 @@ A class can contain declarations of the following members: - [Delegates](../../programming-guide/delegates/index.md) -- [Classes](../../programming-guide/classes-and-structs/classes.md) +- [Classes](/dotnet/csharp/fundamentals/types/classes) -- [Interfaces](../../programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) - [Structure types](../builtin-types/struct.md) diff --git a/docs/csharp/language-reference/keywords/event.md b/docs/csharp/language-reference/keywords/event.md index 08e5e86834f4d..c8d05aea81b93 100644 --- a/docs/csharp/language-reference/keywords/event.md +++ b/docs/csharp/language-reference/keywords/event.md @@ -32,13 +32,13 @@ The following keywords apply to events. |Keyword|Description|For more information| |-------------|-----------------|--------------------------| |[static](./static.md)|Makes the event available to callers at any time, even if no instance of the class exists.|[Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md)| -|[virtual](./virtual.md)|Allows derived classes to override the event behavior by using the [override](./override.md) keyword.|[Inheritance](../../programming-guide/classes-and-structs/inheritance.md)| +|[virtual](./virtual.md)|Allows derived classes to override the event behavior by using the [override](./override.md) keyword.|[Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance)| |[sealed](./sealed.md)|Specifies that for derived classes it is no longer virtual.|| |[abstract](./abstract.md)|The compiler will not generate the `add` and `remove` event accessor blocks and therefore derived classes must provide their own implementation.|| An event may be declared as a static event by using the [static](./static.md) keyword. This makes the event available to callers at any time, even if no instance of the class exists. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md). -An event can be marked as a virtual event by using the [virtual](./virtual.md) keyword. This enables derived classes to override the event behavior by using the [override](./override.md) keyword. For more information, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). An event overriding a virtual event can also be [sealed](./sealed.md), which specifies that for derived classes it is no longer virtual. Lastly, an event can be declared [abstract](./abstract.md), which means that the compiler will not generate the `add` and `remove` event accessor blocks. Therefore derived classes must provide their own implementation. +An event can be marked as a virtual event by using the [virtual](./virtual.md) keyword. This enables derived classes to override the event behavior by using the [override](./override.md) keyword. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). An event overriding a virtual event can also be [sealed](./sealed.md), which specifies that for derived classes it is no longer virtual. Lastly, an event can be declared [abstract](./abstract.md), which means that the compiler will not generate the `add` and `remove` event accessor blocks. Therefore derived classes must provide their own implementation. ## C# language specification diff --git a/docs/csharp/language-reference/keywords/index.md b/docs/csharp/language-reference/keywords/index.md index 1d7a5444e27c9..889a572a74175 100644 --- a/docs/csharp/language-reference/keywords/index.md +++ b/docs/csharp/language-reference/keywords/index.md @@ -144,7 +144,7 @@ A contextual keyword is used to provide a specific meaning in the code, but it i [orderby](orderby-clause.md) [partial (type)](partial-type.md) [partial (method)](partial-method.md) - [record](../../programming-guide/classes-and-structs/records.md) + [record](/dotnet/csharp/fundamentals/types/records) [remove](remove.md) [select](select-clause.md) :::column-end::: diff --git a/docs/csharp/language-reference/keywords/interface.md b/docs/csharp/language-reference/keywords/interface.md index b1686053aa2e0..60eed951798f9 100644 --- a/docs/csharp/language-reference/keywords/interface.md +++ b/docs/csharp/language-reference/keywords/interface.md @@ -14,7 +14,7 @@ An interface defines a contract. Any [`class`](class.md) or [`struct`](../builti In the following example, class `ImplementationClass` must implement a method named `SampleMethod` that has no parameters and returns `void`. -For more information and examples, see [Interfaces](../../programming-guide/interfaces/index.md). +For more information and examples, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). ## Example @@ -70,6 +70,6 @@ For more information, see the [Interfaces](~/_csharplang/spec/interfaces.md) sec - [C# Programming Guide](../../programming-guide/index.md) - [C# Keywords](index.md) - [Reference Types](reference-types.md) -- [Interfaces](../../programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) - [Using Properties](../../programming-guide/classes-and-structs/using-properties.md) - [Using Indexers](../../programming-guide/indexers/using-indexers.md) diff --git a/docs/csharp/language-reference/keywords/namespace.md b/docs/csharp/language-reference/keywords/namespace.md index 2cd8f0eb745f4..157d6cd8e45af 100644 --- a/docs/csharp/language-reference/keywords/namespace.md +++ b/docs/csharp/language-reference/keywords/namespace.md @@ -57,4 +57,4 @@ For more information, see the [Namespaces](~/_csharplang/spec/namespaces.md) sec - [using](using-directive.md) - [using static](using-static.md) - [Namespace alias qualifier `::`](../operators/namespace-alias-qualifier.md) -- [Namespaces](../../programming-guide/namespaces/index.md) +- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) diff --git a/docs/csharp/language-reference/keywords/new-constraint.md b/docs/csharp/language-reference/keywords/new-constraint.md index b6c21ef92cc32..347fb95983312 100644 --- a/docs/csharp/language-reference/keywords/new-constraint.md +++ b/docs/csharp/language-reference/keywords/new-constraint.md @@ -31,4 +31,4 @@ For more information, see the [Type parameter constraints](~/_csharplang/spec/cl - [C# Reference](../index.md) - [C# Programming Guide](../../programming-guide/index.md) - [C# Keywords](index.md) -- [Generics](../../programming-guide/generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/csharp/language-reference/keywords/override.md b/docs/csharp/language-reference/keywords/override.md index 6a9015d03189c..62a6eced07a53 100644 --- a/docs/csharp/language-reference/keywords/override.md +++ b/docs/csharp/language-reference/keywords/override.md @@ -27,7 +27,7 @@ You cannot use the `new`, `static`, or `virtual` modifiers to modify an `overrid An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be `virtual`, `abstract`, or `override`. -For more information about how to use the `override` keyword, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md) and [Knowing when to use Override and New Keywords](../../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). For information about inheritance, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). +For more information about how to use the `override` keyword, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md) and [Knowing when to use Override and New Keywords](../../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). For information about inheritance, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). ## Example @@ -44,10 +44,10 @@ For more information about covariant return types, see the [feature proposal not ## See also - [C# reference](../index.md) -- [Inheritance](../../programming-guide/classes-and-structs/inheritance.md) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) - [C# keywords](index.md) - [Modifiers](index.md) - [abstract](abstract.md) - [virtual](virtual.md) - [new (modifier)](new-modifier.md) -- [Polymorphism](../../programming-guide/classes-and-structs/polymorphism.md) +- [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism) diff --git a/docs/csharp/language-reference/keywords/partial-type.md b/docs/csharp/language-reference/keywords/partial-type.md index 15384a260f267..4c1ded76d571a 100644 --- a/docs/csharp/language-reference/keywords/partial-type.md +++ b/docs/csharp/language-reference/keywords/partial-type.md @@ -34,4 +34,4 @@ Splitting a class, struct or interface type over several files can be useful whe - [C# Reference](../index.md) - [C# Programming Guide](../../programming-guide/index.md) - [Modifiers](index.md) -- [Introduction to Generics](../../programming-guide/generics/index.md) +- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md b/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md index c8f39a11b7574..894d93ea5fa61 100644 --- a/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md +++ b/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md @@ -19,8 +19,8 @@ The following table summarizes the restrictions on declared accessibility levels |Context|Remarks| |-------------|-------------| -|[Classes](../../programming-guide/classes-and-structs/classes.md)|The direct base class of a class type must be at least as accessible as the class type itself.| -|[Interfaces](../../programming-guide/interfaces/index.md)|The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.| +|[Classes](/dotnet/csharp/fundamentals/types/classes)|The direct base class of a class type must be at least as accessible as the class type itself.| +|[Interfaces](/dotnet/csharp/fundamentals/types/interfaces)|The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.| |[Delegates](../../programming-guide/delegates/index.md)|The return type and parameter types of a delegate type must be at least as accessible as the delegate type itself.| |[Constants](../../programming-guide/classes-and-structs/constants.md)|The type of a constant must be at least as accessible as the constant itself.| |[Fields](../../programming-guide/classes-and-structs/fields.md)|The type of a field must be at least as accessible as the field itself.| diff --git a/docs/csharp/language-reference/keywords/sealed.md b/docs/csharp/language-reference/keywords/sealed.md index 52fb6d9aec937..7bd7a48ec1d75 100644 --- a/docs/csharp/language-reference/keywords/sealed.md +++ b/docs/csharp/language-reference/keywords/sealed.md @@ -34,7 +34,7 @@ When applied to a method or property, the `sealed` modifier must always be used Because structs are implicitly sealed, they cannot be inherited. -For more information, see [Inheritance](../../programming-guide/classes-and-structs/inheritance.md). +For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). For more examples, see [Abstract and Sealed Classes and Class Members](../../programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md). diff --git a/docs/csharp/language-reference/keywords/select-clause.md b/docs/csharp/language-reference/keywords/select-clause.md index 789793d1b044d..ed21f5413bc79 100644 --- a/docs/csharp/language-reference/keywords/select-clause.md +++ b/docs/csharp/language-reference/keywords/select-clause.md @@ -38,6 +38,6 @@ At compile time, the `select` clause is translated to a method call to the [!NOTE] > For an introduction to asynchronous programming, see [Asynchronous programming with async and await](../../programming-guide/concepts/async/index.md). Asynchronous programming with `async` and `await` follows the [task-based asynchronous pattern](../../../standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap.md). @@ -40,7 +40,7 @@ You use the `await using` statement to work with an asynchronously disposable ob ## await operator in the Main method -Beginning with C# 7.1, the [`Main` method](../../programming-guide/main-and-command-args/index.md), which is the application entry point, can return `Task` or `Task`, enabling it to be async so you can use the `await` operator in its body. In earlier C# versions, to ensure that the `Main` method waits for the completion of an asynchronous operation, you can retrieve the value of the property of the instance that is returned by the corresponding async method. For asynchronous operations that don't produce a value, you can call the method. For information about how to select the language version, see [C# language versioning](../configure-language-version.md). +Beginning with C# 7.1, the [`Main` method](/dotnet/csharp/fundamentals/program-structure/main-command-line), which is the application entry point, can return `Task` or `Task`, enabling it to be async so you can use the `await` operator in its body. In earlier C# versions, to ensure that the `Main` method waits for the completion of an asynchronous operation, you can retrieve the value of the property of the instance that is returned by the corresponding async method. For asynchronous operations that don't produce a value, you can call the method. For information about how to select the language version, see [C# language versioning](../configure-language-version.md). ## C# language specification diff --git a/docs/csharp/language-reference/operators/delegate-operator.md b/docs/csharp/language-reference/operators/delegate-operator.md index f83c87983f82f..c3f5195bc9b74 100644 --- a/docs/csharp/language-reference/operators/delegate-operator.md +++ b/docs/csharp/language-reference/operators/delegate-operator.md @@ -25,7 +25,7 @@ When you use the `delegate` operator, you might omit the parameter list. If you That's the only functionality of anonymous methods that is not supported by lambda expressions. In all other cases, a lambda expression is a preferred way to write inline code. -Beginning with C# 9.0, you can use [discards](../../discards.md) to specify two or more input parameters of an anonymous method that aren't used by the method: +Beginning with C# 9.0, you can use [discards](/dotnet/csharp/fundamentals/functional/discards) to specify two or more input parameters of an anonymous method that aren't used by the method: :::code language="csharp" source="snippets/shared/DelegateOperator.cs" id="SnippetDiscards" ::: diff --git a/docs/csharp/language-reference/operators/is.md b/docs/csharp/language-reference/operators/is.md index 8301437b4648a..243377a39c64d 100644 --- a/docs/csharp/language-reference/operators/is.md +++ b/docs/csharp/language-reference/operators/is.md @@ -51,5 +51,5 @@ For more information, see [The is operator](~/_csharplang/spec/expressions.md#th - [C# reference](../index.md) - [C# operators and expressions](index.md) - [Patterns](patterns.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) - [Type-testing and cast operators](../operators/type-testing-and-cast.md) diff --git a/docs/csharp/language-reference/operators/lambda-expressions.md b/docs/csharp/language-reference/operators/lambda-expressions.md index 6516af6893c69..3a7e2bb5e3332 100644 --- a/docs/csharp/language-reference/operators/lambda-expressions.md +++ b/docs/csharp/language-reference/operators/lambda-expressions.md @@ -86,7 +86,7 @@ Sometimes the compiler can't infer the types of input parameters. You can specif Input parameter types must be all explicit or all implicit; otherwise, a [CS0748](../../misc/cs0748.md) compiler error occurs. -Beginning with C# 9.0, you can use [discards](../../discards.md) to specify two or more input parameters of a lambda expression that aren't used in the expression: +Beginning with C# 9.0, you can use [discards](/dotnet/csharp/fundamentals/functional/discards) to specify two or more input parameters of a lambda expression that aren't used in the expression: :::code language="csharp" source="snippets/lambda-expressions/GeneralExamples.cs" id="SnippetDiscards"::: diff --git a/docs/csharp/language-reference/operators/member-access-operators.md b/docs/csharp/language-reference/operators/member-access-operators.md index fe4282c991f75..661f0c4a6658c 100644 --- a/docs/csharp/language-reference/operators/member-access-operators.md +++ b/docs/csharp/language-reference/operators/member-access-operators.md @@ -58,7 +58,7 @@ You use the `.` token to access a member of a namespace or a type, as the follow Use a [`using` directive](../keywords/using-directive.md) to make the use of qualified names optional. -- Use `.` to access [type members](../../programming-guide/classes-and-structs/index.md#members), static and non-static, as the following code shows: +- Use `.` to access [type members](/dotnet/csharp/fundamentals/object-oriented#members), static and non-static, as the following code shows: [!code-csharp-interactive[type members](snippets/shared/MemberAccessOperators.cs#TypeMemberAccess)] diff --git a/docs/csharp/language-reference/operators/new-operator.md b/docs/csharp/language-reference/operators/new-operator.md index 804f3d1d361c9..7d4f0b16259b6 100644 --- a/docs/csharp/language-reference/operators/new-operator.md +++ b/docs/csharp/language-reference/operators/new-operator.md @@ -46,7 +46,7 @@ For more information about arrays, see [Arrays](../../programming-guide/arrays/i ## Instantiation of anonymous types -To create an instance of an [anonymous type](../../programming-guide/classes-and-structs/anonymous-types.md), use the `new` operator and object initializer syntax: +To create an instance of an [anonymous type](/dotnet/csharp/fundamentals/types/anonymous-types), use the `new` operator and object initializer syntax: [!code-csharp-interactive[anonymous type](snippets/shared/NewOperator.cs#AnonymousType)] diff --git a/docs/csharp/language-reference/operators/patterns.md b/docs/csharp/language-reference/operators/patterns.md index 2f6d24bc1b012..e2edeb690c7ff 100644 --- a/docs/csharp/language-reference/operators/patterns.md +++ b/docs/csharp/language-reference/operators/patterns.md @@ -34,7 +34,7 @@ In those constructs, you can match an input expression against any of the follow [Logical](#logical-patterns), [property](#property-pattern), and [positional](#positional-pattern) patterns are *recursive* patterns. That is, they can contain *nested* patterns. -For the example of how to use those patterns to build a data-driven algorithm, see [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../tutorials/pattern-matching.md). +For the example of how to use those patterns to build a data-driven algorithm, see [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching). ## Declaration and type patterns @@ -168,7 +168,7 @@ Beginning with C# 8.0, you use a *positional pattern* to deconstruct an expressi :::code language="csharp" source="snippets/patterns/PositionalPattern.cs" id="BasicExample"::: -At the preceding example, the type of an expression contains the [Deconstruct](../../deconstruct.md) method, which is used to deconstruct an expression result. You can also match expressions of [tuple types](../builtin-types/value-tuples.md) against positional patterns. In that way, you can match multiple inputs against various patterns, as the following example shows: +At the preceding example, the type of an expression contains the [Deconstruct](/dotnet/csharp/fundamentals/functional/deconstruct) method, which is used to deconstruct an expression result. You can also match expressions of [tuple types](../builtin-types/value-tuples.md) against positional patterns. In that way, you can match multiple inputs against various patterns, as the following example shows: :::code language="csharp" source="snippets/patterns/PositionalPattern.cs" id="MatchTuple"::: @@ -244,4 +244,4 @@ For more information, see the following feature proposal notes: - [C# reference](../index.md) - [C# operators and expressions](index.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) diff --git a/docs/csharp/language-reference/operators/switch-expression.md b/docs/csharp/language-reference/operators/switch-expression.md index e88ff46ea302c..003467dbb382f 100644 --- a/docs/csharp/language-reference/operators/switch-expression.md +++ b/docs/csharp/language-reference/operators/switch-expression.md @@ -56,5 +56,5 @@ For more information, see the [`switch` expression](~/_csharplang/proposals/csha - [C# reference](../index.md) - [C# operators and expressions](index.md) - [Patterns](patterns.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) - [`switch` statement](../keywords/switch.md) diff --git a/docs/csharp/language-reference/operators/type-testing-and-cast.md b/docs/csharp/language-reference/operators/type-testing-and-cast.md index 8a944d9a3a927..01c80fc2a0a21 100644 --- a/docs/csharp/language-reference/operators/type-testing-and-cast.md +++ b/docs/csharp/language-reference/operators/type-testing-and-cast.md @@ -152,5 +152,5 @@ For more information, see the following sections of the [C# language specificati - [C# reference](../index.md) - [C# operators and expressions](index.md) -- [How to safely cast by using pattern matching and the is and as operators](../../how-to/safely-cast-using-pattern-matching-is-and-as-operators.md) +- [How to safely cast by using pattern matching and the is and as operators](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) - [Generics in .NET](../../../standard/generics/index.md) diff --git a/docs/csharp/linq/group-query-results.md b/docs/csharp/linq/group-query-results.md index b5111c5d91879..3d9fb4d369c24 100644 --- a/docs/csharp/linq/group-query-results.md +++ b/docs/csharp/linq/group-query-results.md @@ -74,7 +74,7 @@ Paste the following method into the `StudentClass` class. Change the calling sta - - [Language Integrated Query (LINQ)](index.md) - [group clause](../language-reference/keywords/group-clause.md) -- [Anonymous Types](../programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) - [Perform a Subquery on a Grouping Operation](perform-a-subquery-on-a-grouping-operation.md) - [Create a Nested Group](create-a-nested-group.md) - [Grouping Data](../programming-guide/concepts/linq/grouping-data.md) diff --git a/docs/csharp/linq/perform-grouped-joins.md b/docs/csharp/linq/perform-grouped-joins.md index 5b5e55d843cad..f0f03eb4b00bd 100644 --- a/docs/csharp/linq/perform-grouped-joins.md +++ b/docs/csharp/linq/perform-grouped-joins.md @@ -36,4 +36,4 @@ Group joins are ideal for creating XML by using LINQ to XML. The following examp - - [Perform inner joins](perform-inner-joins.md) - [Perform left outer joins](perform-left-outer-joins.md) -- [Anonymous types](../programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) diff --git a/docs/csharp/linq/perform-inner-joins.md b/docs/csharp/linq/perform-inner-joins.md index 4579bab0fa611..c30dfdf2809a3 100644 --- a/docs/csharp/linq/perform-inner-joins.md +++ b/docs/csharp/linq/perform-inner-joins.md @@ -64,4 +64,4 @@ The result of `query1` is equivalent to the result set that would have been obta - - [Perform grouped joins](perform-grouped-joins.md) - [Perform left outer joins](perform-left-outer-joins.md) -- [Anonymous types](../programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) diff --git a/docs/csharp/linq/perform-left-outer-joins.md b/docs/csharp/linq/perform-left-outer-joins.md index 0373a56dcadc6..efe3ae9939c95 100644 --- a/docs/csharp/linq/perform-left-outer-joins.md +++ b/docs/csharp/linq/perform-left-outer-joins.md @@ -27,4 +27,4 @@ The second step is to include each element of the first (left) collection in the - - [Perform inner joins](perform-inner-joins.md) - [Perform grouped joins](perform-grouped-joins.md) -- [Anonymous types](../programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) diff --git a/docs/csharp/methods.md b/docs/csharp/methods.md index fa5782b0c0fe8..fb18b7b1315f6 100644 --- a/docs/csharp/methods.md +++ b/docs/csharp/methods.md @@ -282,7 +282,7 @@ For more information, see [Iterators](programming-guide/concepts/iterators.md). - [Access Modifiers](language-reference/keywords/access-modifiers.md) - [Static Classes and Static Class Members](programming-guide/classes-and-structs/static-classes-and-static-class-members.md) -- [Inheritance](programming-guide/classes-and-structs/inheritance.md) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) - [Abstract and Sealed Classes and Class Members](programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md) - [params](language-reference/keywords/params.md) - [out](language-reference/keywords/out-parameter-modifier.md) diff --git a/docs/csharp/misc/cs0017.md b/docs/csharp/misc/cs0017.md index 29e7a87a190ec..cfa05d0bd09a1 100644 --- a/docs/csharp/misc/cs0017.md +++ b/docs/csharp/misc/cs0017.md @@ -12,7 +12,7 @@ ms.assetid: 5e2a3eb3-6f6e-485d-8293-ceabea4d6905 Program 'output file name' has more than one entry point defined. Compile with /main to specify the type that contains the entry point. - A program can only have one [Main](../programming-guide/main-and-command-args/index.md) method. + A program can only have one [Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. To resolve this error, you can either delete all Main methods in your code, except one, or you can use the [**StartupObject**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option to specify which Main method you want to use. diff --git a/docs/csharp/misc/cs0023.md b/docs/csharp/misc/cs0023.md index 43811c14b6dad..67e37485fb8d5 100644 --- a/docs/csharp/misc/cs0023.md +++ b/docs/csharp/misc/cs0023.md @@ -12,7 +12,7 @@ ms.assetid: 7a30073c-99de-41fa-ac6d-4a0dfbb76de9 Operator 'operator' cannot be applied to operand of type 'type' - An attempt was made to apply an operator to a variable whose type was not designed to work with the operator. For more information, see [Types](../programming-guide/types/index.md) and [C# Operators](../language-reference/operators/index.md). + An attempt was made to apply an operator to a variable whose type was not designed to work with the operator. For more information, see [Types](/dotnet/csharp/fundamentals/types) and [C# Operators](../language-reference/operators/index.md). The following sample generates CS0023: diff --git a/docs/csharp/misc/cs0026.md b/docs/csharp/misc/cs0026.md index c63762af9f064..71c5aea074fe1 100644 --- a/docs/csharp/misc/cs0026.md +++ b/docs/csharp/misc/cs0026.md @@ -12,7 +12,7 @@ ms.assetid: 8767fbc1-8ba7-4e88-a9f9-7e620411882b Keyword 'this' is not valid in a static property, static method, or static field initializer - The [this](../language-reference/keywords/this.md) keyword refers to an object, which is an instance of a type. Since static methods are independent of any instance of the containing class, the "this" keyword is meaningless and is therefore not allowed. For more information, see [Static Classes and Static Class Members](../programming-guide/classes-and-structs/static-classes-and-static-class-members.md) and [Objects](../programming-guide/classes-and-structs/objects.md). + The [this](../language-reference/keywords/this.md) keyword refers to an object, which is an instance of a type. Since static methods are independent of any instance of the containing class, the "this" keyword is meaningless and is therefore not allowed. For more information, see [Static Classes and Static Class Members](../programming-guide/classes-and-structs/static-classes-and-static-class-members.md) and [Objects](/dotnet/csharp/fundamentals/object-oriented/objects). ## Example diff --git a/docs/csharp/misc/cs0028.md b/docs/csharp/misc/cs0028.md index a700049f50593..dd0e6813b46b9 100644 --- a/docs/csharp/misc/cs0028.md +++ b/docs/csharp/misc/cs0028.md @@ -12,7 +12,7 @@ ms.assetid: 47df919f-01fa-45ae-a4b7-912445e679e2 'function declaration' has the wrong signature to be an entry point - The method declaration for `Main` was invalid: it was declared with an invalid signature. `Main` must be declared as static and it must return either [int](../language-reference/builtin-types/integral-numeric-types.md) or [void](../language-reference/builtin-types/void.md). For more information, see [Main() and Command-Line Arguments](../programming-guide/main-and-command-args/index.md). + The method declaration for `Main` was invalid: it was declared with an invalid signature. `Main` must be declared as static and it must return either [int](../language-reference/builtin-types/integral-numeric-types.md) or [void](../language-reference/builtin-types/void.md). For more information, see [Main() and Command-Line Arguments](/dotnet/csharp/fundamentals/program-structure/main-command-line). The following sample generates CS0028: diff --git a/docs/csharp/misc/cs0031.md b/docs/csharp/misc/cs0031.md index 57c74f2733656..c4c7b06a0313a 100644 --- a/docs/csharp/misc/cs0031.md +++ b/docs/csharp/misc/cs0031.md @@ -12,7 +12,7 @@ ms.assetid: 91f11ae9-9143-41f4-8002-5c38c8ee0651 Constant value 'value' cannot be converted to a 'type'. -An attempt was made to assign a value to a variable whose type cannot store the value. For more information, see [Types](../programming-guide/types/index.md). +An attempt was made to assign a value to a variable whose type cannot store the value. For more information, see [Types](/dotnet/csharp/fundamentals/types). The following sample generates CS0031 in both checked and unchecked contexts: diff --git a/docs/csharp/misc/cs0061.md b/docs/csharp/misc/cs0061.md index 863e8ddad3e41..399d25a3bfb30 100644 --- a/docs/csharp/misc/cs0061.md +++ b/docs/csharp/misc/cs0061.md @@ -14,7 +14,7 @@ Inconsistent accessibility: base interface 'interface 1' is less accessible than A [public](../language-reference/keywords/public.md) construct must return a publicly accessible object. - Interface accessibility cannot be narrowed in a derived interface. For more information, see [Interfaces](../programming-guide/interfaces/index.md) and [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md). + Interface accessibility cannot be narrowed in a derived interface. For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) and [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md). The following sample generates CS0061. diff --git a/docs/csharp/misc/cs0068.md b/docs/csharp/misc/cs0068.md index 20ac57d446f4c..df102c507d635 100644 --- a/docs/csharp/misc/cs0068.md +++ b/docs/csharp/misc/cs0068.md @@ -12,7 +12,7 @@ ms.assetid: 9c9ac915-e12f-4ceb-8eb0-806790f11a09 'event': event in interface cannot have initializer - An event in an interface cannot have an initializer. For more information, see [Interfaces](../programming-guide/interfaces/index.md). + An event in an interface cannot have an initializer. For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). The following sample generates CS0068: diff --git a/docs/csharp/misc/cs0069.md b/docs/csharp/misc/cs0069.md index 329b452b81398..b5a519be8307b 100644 --- a/docs/csharp/misc/cs0069.md +++ b/docs/csharp/misc/cs0069.md @@ -12,7 +12,7 @@ ms.assetid: a1b32906-7773-47c6-8515-162a201a9be5 An event in an interface cannot have add or remove accessors - You cannot define an event's accessor functions in an [interface](../language-reference/keywords/interface.md). For more information, see [Events](../programming-guide/events/index.md) and [Interfaces](../programming-guide/interfaces/index.md). + You cannot define an event's accessor functions in an [interface](../language-reference/keywords/interface.md). For more information, see [Events](../programming-guide/events/index.md) and [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). The following sample generates CS0069: diff --git a/docs/csharp/misc/cs0080.md b/docs/csharp/misc/cs0080.md index c0460af47404f..0c2a204450602 100644 --- a/docs/csharp/misc/cs0080.md +++ b/docs/csharp/misc/cs0080.md @@ -12,7 +12,7 @@ ms.assetid: 99035371-37d1-48b2-a8b9-e8a1ebd04f0f Constraints are not allowed on non-generic declarations - The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](../programming-guide/generics/index.md). + The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](/dotnet/csharp/fundamentals/types/generics). The following sample generates CS0080 because MyClass is not a generic class and Foo is not a generic method. diff --git a/docs/csharp/misc/cs0081.md b/docs/csharp/misc/cs0081.md index fcd80d60959a0..72c52db2f2aec 100644 --- a/docs/csharp/misc/cs0081.md +++ b/docs/csharp/misc/cs0081.md @@ -32,4 +32,4 @@ class MyClass ## See also -- [Generics](../programming-guide/generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/csharp/misc/cs0101.md b/docs/csharp/misc/cs0101.md index 95de145549ea0..b55d94f7476d7 100644 --- a/docs/csharp/misc/cs0101.md +++ b/docs/csharp/misc/cs0101.md @@ -12,7 +12,7 @@ ms.assetid: edb5246b-c16b-4845-bb2d-0ef769d014c7 The namespace 'namespace' already contains a definition for 'type' - A [namespace](../language-reference/keywords/namespace.md) has duplicate identifiers. Rename or delete one of the duplicate identifiers. For more information, see [Namespaces](../programming-guide/namespaces/index.md) + A [namespace](../language-reference/keywords/namespace.md) has duplicate identifiers. Rename or delete one of the duplicate identifiers. For more information, see [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) The following sample generates CS0101: diff --git a/docs/csharp/misc/cs0138.md b/docs/csharp/misc/cs0138.md index 22c7a1cd12879..61c7bf9ae1b1f 100644 --- a/docs/csharp/misc/cs0138.md +++ b/docs/csharp/misc/cs0138.md @@ -12,7 +12,7 @@ ms.assetid: 970545f8-5ee5-428e-921a-3aa29f68d16d A using namespace directive can only be applied to namespaces; 'type' is a type not a namespace - A [using](../language-reference/keywords/using.md) directive can only take the name of a namespace as a parameter. For more information, see [Namespaces](../programming-guide/namespaces/index.md). + A [using](../language-reference/keywords/using.md) directive can only take the name of a namespace as a parameter. For more information, see [Namespaces](/dotnet/csharp/fundamentals/types/namespaces). The following sample generates CS0138: diff --git a/docs/csharp/misc/cs0144.md b/docs/csharp/misc/cs0144.md index f9c2b739a1b3f..c469aad4c3886 100644 --- a/docs/csharp/misc/cs0144.md +++ b/docs/csharp/misc/cs0144.md @@ -12,7 +12,7 @@ ms.assetid: 3904cab1-05bd-44ec-81d0-e36c5656f742 Cannot create an instance of the abstract class or interface 'interface' - You cannot create an instance of an [abstract](../language-reference/keywords/abstract.md) class or an [interface](../language-reference/keywords/interface.md). For more information, see [Interfaces](../programming-guide/interfaces/index.md). + You cannot create an instance of an [abstract](../language-reference/keywords/abstract.md) class or an [interface](../language-reference/keywords/interface.md). For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). The following sample generates CS0144: diff --git a/docs/csharp/misc/cs0146.md b/docs/csharp/misc/cs0146.md index ba33b65190a94..fd743c8df79f4 100644 --- a/docs/csharp/misc/cs0146.md +++ b/docs/csharp/misc/cs0146.md @@ -12,7 +12,7 @@ ms.assetid: 2be796e5-da2c-4939-af12-3145cd1828c8 Circular base class dependency involving 'class1' and 'class2' - The inheritance list for a class includes a direct or indirect reference to itself. A class cannot inherit from itself. For more information, see [Inheritance](../programming-guide/classes-and-structs/inheritance.md). + The inheritance list for a class includes a direct or indirect reference to itself. A class cannot inherit from itself. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). The following sample generates CS0146: diff --git a/docs/csharp/misc/cs0155.md b/docs/csharp/misc/cs0155.md index 4755d1a8451cf..33a819716acfc 100644 --- a/docs/csharp/misc/cs0155.md +++ b/docs/csharp/misc/cs0155.md @@ -12,7 +12,7 @@ ms.assetid: 6c92984a-2b10-453e-9cb7-e6a1d1b98aa6 The type caught or thrown must be derived from System.Exception - An attempt was made to pass a data type that does not derive from **System.Exception** into a [catch](../language-reference/keywords/try-catch.md) block. Only data types that derive from **System.Exception** can be passed into a **catch** block. For more information, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). + An attempt was made to pass a data type that does not derive from **System.Exception** into a [catch](../language-reference/keywords/try-catch.md) block. Only data types that derive from **System.Exception** can be passed into a **catch** block. For more information, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). The following sample generates CS0155: diff --git a/docs/csharp/misc/cs0156.md b/docs/csharp/misc/cs0156.md index 1ca25a79de3b0..3b6cf0e6cb0c0 100644 --- a/docs/csharp/misc/cs0156.md +++ b/docs/csharp/misc/cs0156.md @@ -14,7 +14,7 @@ A throw statement with no arguments is not allowed in a finally clause that is n A [throw](../language-reference/keywords/throw.md) statement with no parameters can only appear in a **catch** clause that takes no parameters. - For more information, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). + For more information, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). The following sample generates CS0156: diff --git a/docs/csharp/misc/cs0157.md b/docs/csharp/misc/cs0157.md index 842bbcb10576f..d3ddc460f19a6 100644 --- a/docs/csharp/misc/cs0157.md +++ b/docs/csharp/misc/cs0157.md @@ -12,7 +12,7 @@ ms.assetid: a5d9d506-81f8-47dd-85b6-85f8170bcbef Control cannot leave the body of a finally clause - All of the statements in a [finally](../language-reference/keywords/try-catch-finally.md) clause must execute. For more information, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). + All of the statements in a [finally](../language-reference/keywords/try-catch-finally.md) clause must execute. For more information, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). The following sample generates CS0157: diff --git a/docs/csharp/misc/cs0160.md b/docs/csharp/misc/cs0160.md index d52c86a7ddb16..cbaa3578239a0 100644 --- a/docs/csharp/misc/cs0160.md +++ b/docs/csharp/misc/cs0160.md @@ -14,7 +14,7 @@ A previous catch clause already catches all exceptions of this or of a super typ A series of [catch](../language-reference/keywords/try-catch.md) statements needs to be in decreasing order of derivation. For example, the most derived objects must appear first. - For more information, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). + For more information, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). The following sample generates CS0160: diff --git a/docs/csharp/misc/cs0238.md b/docs/csharp/misc/cs0238.md index 686f0a68905ea..889fc06f22ff7 100644 --- a/docs/csharp/misc/cs0238.md +++ b/docs/csharp/misc/cs0238.md @@ -12,7 +12,7 @@ ms.assetid: 9b50c6e2-2c3f-431d-bdb7-557b0ec51626 'member' cannot be sealed because it is not an override - [sealed](../language-reference/keywords/sealed.md) was used on a member that was not also marked with [override](../language-reference/keywords/override.md). For more information, see [Inheritance](../programming-guide/classes-and-structs/inheritance.md). + [sealed](../language-reference/keywords/sealed.md) was used on a member that was not also marked with [override](../language-reference/keywords/override.md). For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). The following sample generates CS0238: diff --git a/docs/csharp/misc/cs0255.md b/docs/csharp/misc/cs0255.md index 7b7c04fce1b09..a9d1024629370 100644 --- a/docs/csharp/misc/cs0255.md +++ b/docs/csharp/misc/cs0255.md @@ -12,7 +12,7 @@ ms.assetid: b45f5d5a-1923-4fe1-a858-e5ef5590a108 stackalloc may not be used in a catch or finally block -You cannot use the [stackalloc operator](../language-reference/operators/stackalloc.md) in a [catch](../language-reference/keywords/try-catch.md) or [finally](../language-reference/keywords/try-catch-finally.md) block. For more information, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). +You cannot use the [stackalloc operator](../language-reference/operators/stackalloc.md) in a [catch](../language-reference/keywords/try-catch.md) or [finally](../language-reference/keywords/try-catch-finally.md) block. For more information, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). The following sample generates CS0255: diff --git a/docs/csharp/misc/cs0426.md b/docs/csharp/misc/cs0426.md index f7f1f1eb1c98d..6aabe4e3c3efc 100644 --- a/docs/csharp/misc/cs0426.md +++ b/docs/csharp/misc/cs0426.md @@ -37,4 +37,4 @@ class D ## See also -- [Classes, structs, and records](../programming-guide/classes-and-structs/index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) diff --git a/docs/csharp/misc/cs0666.md b/docs/csharp/misc/cs0666.md index f772d809fe791..24b6ff6113da0 100644 --- a/docs/csharp/misc/cs0666.md +++ b/docs/csharp/misc/cs0666.md @@ -12,7 +12,7 @@ ms.assetid: 44ad4574-b4a2-487b-8d05-0116762231ab 'member' : new protected member declared in struct - A [struct](../language-reference/builtin-types/struct.md) cannot be [abstract](../language-reference/keywords/abstract.md) and is always implicitly [sealed](../language-reference/keywords/sealed.md). Because structs do not support inheritance, the concept of a [protected](../language-reference/keywords/protected.md) member in a struct makes no sense. For more information, see [Inheritance](../programming-guide/classes-and-structs/inheritance.md). + A [struct](../language-reference/builtin-types/struct.md) cannot be [abstract](../language-reference/keywords/abstract.md) and is always implicitly [sealed](../language-reference/keywords/sealed.md). Because structs do not support inheritance, the concept of a [protected](../language-reference/keywords/protected.md) member in a struct makes no sense. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). ## Example diff --git a/docs/csharp/misc/cs0736.md b/docs/csharp/misc/cs0736.md index 9d8e4af2e6dbd..654157ea68db6 100644 --- a/docs/csharp/misc/cs0736.md +++ b/docs/csharp/misc/cs0736.md @@ -48,4 +48,4 @@ namespace CS0736 ## See also -- [Interfaces](../programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) diff --git a/docs/csharp/misc/cs0737.md b/docs/csharp/misc/cs0737.md index ac25d0702fcf3..255de4b1a3719 100644 --- a/docs/csharp/misc/cs0737.md +++ b/docs/csharp/misc/cs0737.md @@ -51,4 +51,4 @@ public class Test ## See also -- [Interfaces](../programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) diff --git a/docs/csharp/misc/cs0738.md b/docs/csharp/misc/cs0738.md index 9bfaad0232763..2b1a128690c62 100644 --- a/docs/csharp/misc/cs0738.md +++ b/docs/csharp/misc/cs0738.md @@ -39,4 +39,4 @@ public class Test: ITest ## See also -- [Interfaces](../programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) diff --git a/docs/csharp/misc/cs0746.md b/docs/csharp/misc/cs0746.md index 1a65bbfabd87a..7ebcbf66fb3a7 100644 --- a/docs/csharp/misc/cs0746.md +++ b/docs/csharp/misc/cs0746.md @@ -43,4 +43,4 @@ public class C ## See also -- [Anonymous Types](../programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) diff --git a/docs/csharp/misc/cs0833.md b/docs/csharp/misc/cs0833.md index a4949be2d20c7..3008a4994fd8e 100644 --- a/docs/csharp/misc/cs0833.md +++ b/docs/csharp/misc/cs0833.md @@ -38,4 +38,4 @@ public class C ## See also -- [Anonymous Types](../programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) diff --git a/docs/csharp/misc/cs1015.md b/docs/csharp/misc/cs1015.md index ac4ebdff9cf06..58000aaf36c6f 100644 --- a/docs/csharp/misc/cs1015.md +++ b/docs/csharp/misc/cs1015.md @@ -12,7 +12,7 @@ ms.assetid: 53179feb-e8be-41e0-bb0b-f7879e9fa613 An object, string, or class type expected - An attempt was made to pass a predefined data type into a [catch](../language-reference/keywords/try-catch.md) block. Only data types that derive from can be passed into a `catch` block. For more information on exceptions, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). + An attempt was made to pass a predefined data type into a [catch](../language-reference/keywords/try-catch.md) block. Only data types that derive from can be passed into a `catch` block. For more information on exceptions, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). ## Example diff --git a/docs/csharp/misc/cs1017.md b/docs/csharp/misc/cs1017.md index a42bb3b0218c7..dfce8ba7cbebe 100644 --- a/docs/csharp/misc/cs1017.md +++ b/docs/csharp/misc/cs1017.md @@ -12,7 +12,7 @@ ms.assetid: e0902e8a-b042-4711-a8a6-83456a3f88cb Catch clauses cannot follow the general catch clause of a try statement - A `catch` block that does not take any parameters must be the last in a series of `catch` blocks. For more information on exceptions, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). + A `catch` block that does not take any parameters must be the last in a series of `catch` blocks. For more information on exceptions, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). ## Example diff --git a/docs/csharp/misc/cs1524.md b/docs/csharp/misc/cs1524.md index d1c87a88becad..764118f84881f 100644 --- a/docs/csharp/misc/cs1524.md +++ b/docs/csharp/misc/cs1524.md @@ -14,7 +14,7 @@ Expected catch or finally A `try` block must be followed by a `catch` or `finally` block. - For more information on exceptions, see [Exceptions and Exception Handling](../programming-guide/exceptions/index.md). + For more information on exceptions, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). ## Example diff --git a/docs/csharp/misc/cs1527.md b/docs/csharp/misc/cs1527.md index dcf487134a674..45311dc43ff10 100644 --- a/docs/csharp/misc/cs1527.md +++ b/docs/csharp/misc/cs1527.md @@ -39,7 +39,7 @@ private struct S1 {} ## See also -- [Namespaces](../programming-guide/namespaces/index.md) +- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) - [:: Operator](../language-reference/operators/namespace-alias-qualifier.md) - [Accessibility Domain](../language-reference/keywords/accessibility-domain.md) - [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md) diff --git a/docs/csharp/misc/cs1558.md b/docs/csharp/misc/cs1558.md index 41e2ede61f5c8..35423bda10737 100644 --- a/docs/csharp/misc/cs1558.md +++ b/docs/csharp/misc/cs1558.md @@ -12,7 +12,7 @@ ms.assetid: ee603d66-007e-4782-9285-7ff031975f0f 'class' does not have a suitable static Main method - The [**MainEntryPoint**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option specified a class in which to look for a **Main** method. However, the [Main](../programming-guide/main-and-command-args/index.md) method was not defined correctly. + The [**MainEntryPoint**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option specified a class in which to look for a **Main** method. However, the [Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method was not defined correctly. The following example generates CS1558 because of invalid return type. diff --git a/docs/csharp/misc/cs1559.md b/docs/csharp/misc/cs1559.md index 3995533b20f75..1367451406264 100644 --- a/docs/csharp/misc/cs1559.md +++ b/docs/csharp/misc/cs1559.md @@ -12,4 +12,4 @@ ms.assetid: 08e6c183-4e92-44a5-99f1-4d1eef3d0a67 Cannot use 'object' for Main method because it is imported - An invalid class was specified to the [**StartupObject**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option; the class cannot be used as a location for the [Main](../programming-guide/main-and-command-args/index.md) method. + An invalid class was specified to the [**StartupObject**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option; the class cannot be used as a location for the [Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. diff --git a/docs/csharp/misc/cs1913.md b/docs/csharp/misc/cs1913.md index 6fca3b23e9fb8..9d01a2d56e702 100644 --- a/docs/csharp/misc/cs1913.md +++ b/docs/csharp/misc/cs1913.md @@ -44,4 +44,4 @@ public class Test ## See also -- [Classes, structs, and records](../programming-guide/classes-and-structs/index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) diff --git a/docs/csharp/misc/cs5001.md b/docs/csharp/misc/cs5001.md index 222fe4ad13b3b..52345490625f9 100644 --- a/docs/csharp/misc/cs5001.md +++ b/docs/csharp/misc/cs5001.md @@ -12,7 +12,7 @@ ms.assetid: e1e26e75-84e0-47c7-be8a-3c4fd0d6f497 Program does not contain a static 'Main' method suitable for an entry point -This error occurs when no static `Main` method with a correct signature is found in the code that produces an executable file. It also occurs if the entry point function, `Main`, is defined with the wrong case, such as lower-case `main`. For information about the rules that apply to the `Main` method, see [Main() and Command-Line Arguments](../programming-guide/main-and-command-args/index.md). +This error occurs when no static `Main` method with a correct signature is found in the code that produces an executable file. It also occurs if the entry point function, `Main`, is defined with the wrong case, such as lower-case `main`. For information about the rules that apply to the `Main` method, see [Main() and Command-Line Arguments](/dotnet/csharp/fundamentals/program-structure/main-command-line). If the `Main` method has an `async` modifier, make sure that the [selected C# language version](../language-reference/configure-language-version.md) is 7.1 or higher. diff --git a/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md b/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md index de133d64caf74..e1fcbbe64a2e4 100644 --- a/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md +++ b/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md @@ -32,7 +32,7 @@ When you create an anonymous type that contains an array, the array must be impl - [C# Programming Guide](../index.md) - [Implicitly Typed Local Variables](../classes-and-structs/implicitly-typed-local-variables.md) - [Arrays](./index.md) -- [Anonymous Types](../classes-and-structs/anonymous-types.md) +- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) - [Object and Collection Initializers](../classes-and-structs/object-and-collection-initializers.md) - [var](../../language-reference/keywords/var.md) - [LINQ in C#](../../linq/index.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md b/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md index c77fa008014a2..6f32db75a9e01 100644 --- a/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md +++ b/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md @@ -48,8 +48,8 @@ The [abstract](../../language-reference/keywords/abstract.md) keyword enables yo ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) -- [Inheritance](./inheritance.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) - [Methods](./methods.md) - [Fields](./fields.md) - [How to define abstract properties](./how-to-define-abstract-properties.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md b/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md index c48989365ac37..55a4b22e65be2 100644 --- a/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md +++ b/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md @@ -65,8 +65,8 @@ Delegates behave like classes and structs. By default, they have `internal` acce ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) -- [Interfaces](../interfaces/index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) - [private](../../language-reference/keywords/private.md) - [public](../../language-reference/keywords/public.md) - [internal](../../language-reference/keywords/internal.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/constants.md b/docs/csharp/programming-guide/classes-and-structs/constants.md index f4abffb82c868..bc72e62cfe686 100644 --- a/docs/csharp/programming-guide/classes-and-structs/constants.md +++ b/docs/csharp/programming-guide/classes-and-structs/constants.md @@ -45,8 +45,8 @@ Constants are immutable values which are known at compile time and do not change ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Properties](./properties.md) -- [Types](../types/index.md) +- [Types](/dotnet/csharp/fundamentals/types) - [readonly](../../language-reference/keywords/readonly.md) - [Immutability in C# Part One: Kinds of Immutability](/archive/blogs/ericlippert/immutability-in-c-part-one-kinds-of-immutability) diff --git a/docs/csharp/programming-guide/classes-and-structs/constructors.md b/docs/csharp/programming-guide/classes-and-structs/constructors.md index 5edd2489bfcfb..3b2b6fd1c699e 100644 --- a/docs/csharp/programming-guide/classes-and-structs/constructors.md +++ b/docs/csharp/programming-guide/classes-and-structs/constructors.md @@ -55,7 +55,7 @@ For more information and examples, see [Static Constructors](./static-constructo ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Finalizers](./destructors.md) - [static](../../language-reference/keywords/static.md) - [Why Do Initializers Run In The Opposite Order As Constructors? Part One](/archive/blogs/ericlippert/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one) diff --git a/docs/csharp/programming-guide/classes-and-structs/extension-methods.md b/docs/csharp/programming-guide/classes-and-structs/extension-methods.md index 1cf34be4977f6..869598761c6f7 100644 --- a/docs/csharp/programming-guide/classes-and-structs/extension-methods.md +++ b/docs/csharp/programming-guide/classes-and-structs/extension-methods.md @@ -101,7 +101,7 @@ Extending predefined types can be difficult with `struct` types because they're While it's still considered preferable to add functionality by modifying an object's code or deriving a new type whenever it's reasonable and possible to do so, extension methods have become a crucial option for creating reusable functionality throughout the .NET ecosystem. For those occasions when the original source isn't under your control, when a derived object is inappropriate or impossible, or when the functionality shouldn't be exposed beyond its applicable scope, Extension methods are an excellent choice. -For more information on derived types, see [Inheritance](./inheritance.md). +For more information on derived types, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). When using an extension method to extend a type whose source code you aren't in control of, you run the risk that a change in the implementation of the type will cause your extension method to break. diff --git a/docs/csharp/programming-guide/classes-and-structs/fields.md b/docs/csharp/programming-guide/classes-and-structs/fields.md index 5fc505e98245a..242e882da9cdb 100644 --- a/docs/csharp/programming-guide/classes-and-structs/fields.md +++ b/docs/csharp/programming-guide/classes-and-structs/fields.md @@ -46,8 +46,8 @@ A field can be declared [readonly](../../language-reference/keywords/readonly.md ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Using Constructors](./using-constructors.md) -- [Inheritance](./inheritance.md) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) - [Access Modifiers](./access-modifiers.md) - [Abstract and Sealed Classes and Class Members](./abstract-and-sealed-classes-and-class-members.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-declare-and-use-read-write-properties.md b/docs/csharp/programming-guide/classes-and-structs/how-to-declare-and-use-read-write-properties.md index 1080b5be22ce1..e9a2046899474 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-declare-and-use-read-write-properties.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-declare-and-use-read-write-properties.md @@ -58,4 +58,4 @@ person.SetAge(person.GetAge() + 1); - [C# Programming Guide](../index.md) - [Properties](./properties.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md b/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md index 6f6ccc8dda2f4..c103220472de7 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md @@ -56,6 +56,6 @@ The following example shows how to define [abstract](../../language-reference/ke ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Abstract and Sealed Classes and Class Members](./abstract-and-sealed-classes-and-class-members.md) - [Properties](./properties.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-define-constants.md b/docs/csharp/programming-guide/classes-and-structs/how-to-define-constants.md index 734cc507edab9..b62b3816c334b 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-define-constants.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-define-constants.md @@ -28,4 +28,4 @@ Constants are fields whose values are set at compile time and can never be chang ## See also -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md b/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md index 350abed69424d..83fbde0b8973f 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md @@ -27,6 +27,6 @@ The following example demonstrates how passing a [struct](../../language-referen ## See also - [C# Programming Guide](../index.md) -- [Classes](./classes.md) +- [Classes](/dotnet/csharp/fundamentals/types/classes) - [Structure types](../../language-reference/builtin-types/struct.md) - [Passing Parameters](./passing-parameters.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method.md b/docs/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method.md index c7cb5c9de0c3c..ba0c196a35058 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method.md @@ -44,7 +44,7 @@ To override the `ToString` method in your class or struct: - - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Strings](../strings/index.md) - [string](../../language-reference/builtin-types/reference-types.md) - [override](../../language-reference/keywords/override.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md b/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md index 55817280824bc..513ce06f93d93 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md @@ -47,5 +47,5 @@ To run this code, copy and paste the class into a C# console application with a ## See also - [C# Programming Guide](../index.md) -- [Anonymous Types](./anonymous-types.md) +- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) - [LINQ in C#](../../linq/index.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md b/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md index 5b1e9b1625d5a..e478e7aef0f36 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md @@ -16,7 +16,7 @@ You can use implicitly typed local variables whenever you want the compiler to d ## Example - The following example shows a common scenario in which the `var` keyword is required: a query expression that produces a sequence of anonymous types. In this scenario, both the query variable and the iteration variable in the `foreach` statement must be implicitly typed by using `var` because you do not have access to a type name for the anonymous type. For more information about anonymous types, see [Anonymous Types](./anonymous-types.md). + The following example shows a common scenario in which the `var` keyword is required: a query expression that produces a sequence of anonymous types. In this scenario, both the query variable and the iteration variable in the `foreach` statement must be implicitly typed by using `var` because you do not have access to a type name for the anonymous type. For more information about anonymous types, see [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types). [!code-csharp[csProgGuideLINQ#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideLINQ/CS/csRef30LangFeatures_2.cs#32)] diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md b/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md index 1ab0cdbf8158e..2c8e3ce2358e1 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md @@ -11,7 +11,7 @@ ms.assetid: fba899b5-fc41-428e-a745-3ebdbf37990a --- # How to write a copy constructor (C# Programming Guide) -C# [records](records.md) provide a copy constructor for objects, but for classes you have to write one yourself. +C# [records](/dotnet/csharp/fundamentals/types/records) provide a copy constructor for objects, but for classes you have to write one yourself. ## Example @@ -22,8 +22,8 @@ C# [records](records.md) provide a copy constructor for objects, but for classes ## See also - -- [Records](records.md) +- [Records](/dotnet/csharp/fundamentals/types/records) - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Constructors](./constructors.md) - [Finalizers](./destructors.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md b/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md index 19f15c52969ec..e7b7afdf0e9a5 100644 --- a/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md +++ b/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md @@ -43,7 +43,7 @@ For more information, see [How to use implicitly typed local variables and array ## var and anonymous types -In many cases the use of `var` is optional and is just a syntactic convenience. However, when a variable is initialized with an anonymous type you must declare the variable as `var` if you need to access the properties of the object at a later point. This is a common scenario in LINQ query expressions. For more information, see [Anonymous Types](anonymous-types.md). +In many cases the use of `var` is optional and is just a syntactic convenience. However, when a variable is initialized with an anonymous type you must declare the variable as `var` if you need to access the properties of the object at a later point. This is a common scenario in LINQ query expressions. For more information, see [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types). From the perspective of your source code, an anonymous type has no name. Therefore, if a query variable has been initialized with `var`, then the only way to access the properties in the returned sequence of objects is to use `var` as the type of the iteration variable in the `foreach` statement. @@ -83,14 +83,14 @@ The `var` keyword can also be useful when the specific type of the variable is t [!code-csharp[cscsrefQueryKeywords#13](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsCsrefQueryKeywords/CS/Group.cs#13)] -The use of `var` helps simplify your code, but its use should be restricted to cases where it is required, or when it makes your code easier to read. For more information about when to use `var` properly, see the [Implicitly typed local variables](../inside-a-program/coding-conventions.md#implicitly-typed-local-variables) section on the C# Coding Guidelines article. +The use of `var` helps simplify your code, but its use should be restricted to cases where it is required, or when it makes your code easier to read. For more information about when to use `var` properly, see the [Implicitly typed local variables](/dotnet/csharp/fundamentals/coding-style/coding-conventions#implicitly-typed-local-variables) section on the C# Coding Guidelines article. ## See also - [C# Reference](../../language-reference/index.md) - [Implicitly Typed Arrays](../arrays/implicitly-typed-arrays.md) - [How to use implicitly typed local variables and arrays in a query expression](how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md) -- [Anonymous Types](anonymous-types.md) +- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) - [Object and Collection Initializers](object-and-collection-initializers.md) - [var](../../language-reference/keywords/var.md) - [LINQ in C#](../../linq/index.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/instance-constructors.md b/docs/csharp/programming-guide/classes-and-structs/instance-constructors.md index 18b02633cde3b..f4a646c2e13d0 100644 --- a/docs/csharp/programming-guide/classes-and-structs/instance-constructors.md +++ b/docs/csharp/programming-guide/classes-and-structs/instance-constructors.md @@ -67,7 +67,7 @@ class Circle : Shape ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Constructors](./constructors.md) - [Finalizers](./destructors.md) - [static](../../language-reference/keywords/static.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/interface-properties.md b/docs/csharp/programming-guide/classes-and-structs/interface-properties.md index 9dd9a2503df5f..498b5cae9ce5e 100644 --- a/docs/csharp/programming-guide/classes-and-structs/interface-properties.md +++ b/docs/csharp/programming-guide/classes-and-structs/interface-properties.md @@ -55,4 +55,4 @@ Employee name: Hazem Abolrous - [Using Properties](./using-properties.md) - [Comparison Between Properties and Indexers](../indexers/comparison-between-properties-and-indexers.md) - [Indexers](../indexers/index.md) -- [Interfaces](../interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) diff --git a/docs/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md b/docs/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md index 124713a13c532..07736b6cf39e3 100644 --- a/docs/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md +++ b/docs/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md @@ -540,7 +540,7 @@ namespace OverrideAndNew2 ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Versioning with the Override and New Keywords](./versioning-with-the-override-and-new-keywords.md) - [base](../../language-reference/keywords/base.md) - [abstract](../../language-reference/keywords/abstract.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/members.md b/docs/csharp/programming-guide/classes-and-structs/members.md index 1e8250bfcc878..0cbb17f541f3f 100644 --- a/docs/csharp/programming-guide/classes-and-structs/members.md +++ b/docs/csharp/programming-guide/classes-and-structs/members.md @@ -29,4 +29,4 @@ Classes and structs have members that represent their data and behavior. A class ## See also - [C# Programming Guide](../index.md) -- [Classes](./classes.md) +- [Classes](/dotnet/csharp/fundamentals/types/classes) diff --git a/docs/csharp/programming-guide/classes-and-structs/methods.md b/docs/csharp/programming-guide/classes-and-structs/methods.md index bd4c48ee2974b..1e94af36682f2 100644 --- a/docs/csharp/programming-guide/classes-and-structs/methods.md +++ b/docs/csharp/programming-guide/classes-and-structs/methods.md @@ -11,14 +11,14 @@ ms.assetid: cc738f07-e8cd-4683-9585-9f40c0667c37 A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. -The `Main` method is the entry point for every C# application and it's called by the common language runtime (CLR) when the program is started. In an application that uses [top-level statements](../main-and-command-args/top-level-statements.md), the `Main` method is generated by the compiler and contains all top-level statements. +The `Main` method is the entry point for every C# application and it's called by the common language runtime (CLR) when the program is started. In an application that uses [top-level statements](/dotnet/csharp/fundamentals/program-structure/top-level-statements), the `Main` method is generated by the compiler and contains all top-level statements. > [!NOTE] > This article discusses named methods. For information about anonymous functions, see [Anonymous Functions](../statements-expressions-operators/anonymous-functions.md). ## Method signatures -Methods are declared in a [class](../../language-reference/keywords/class.md), [struct](../../language-reference/builtin-types/struct.md), or [interface](../interfaces/index.md) by specifying the access level such as `public` or `private`, optional modifiers such as `abstract` or `sealed`, the return value, the name of the method, and any method parameters. These parts together are the signature of the method. +Methods are declared in a [class](../../language-reference/keywords/class.md), [struct](../../language-reference/builtin-types/struct.md), or [interface](/dotnet/csharp/fundamentals/types/interfaces) by specifying the access level such as `public` or `private`, optional modifiers such as `abstract` or `sealed`, the return value, the name of the method, and any method parameters. These parts together are the signature of the method. > [!IMPORTANT] > A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to. @@ -165,10 +165,10 @@ For more information, see [Iterators](../concepts/iterators.md). ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Access Modifiers](access-modifiers.md) - [Static Classes and Static Class Members](static-classes-and-static-class-members.md) -- [Inheritance](inheritance.md) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) - [Abstract and Sealed Classes and Class Members](abstract-and-sealed-classes-and-class-members.md) - [params](../../language-reference/keywords/params.md) - [return](../../language-reference/keywords/return.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/nested-types.md b/docs/csharp/programming-guide/classes-and-structs/nested-types.md index ecae377749921..dbbc09a24799d 100644 --- a/docs/csharp/programming-guide/classes-and-structs/nested-types.md +++ b/docs/csharp/programming-guide/classes-and-structs/nested-types.md @@ -41,7 +41,7 @@ In the previous declaration, the full name of class `Nested` is `Container.Neste ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Access Modifiers](./access-modifiers.md) - [Constructors](./constructors.md) - [CA1034 rule](../../../fundamentals/code-analysis/quality-rules/ca1034.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md b/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md index 130ee9ad9f11f..dfac4267d98f4 100644 --- a/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md +++ b/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md @@ -52,7 +52,7 @@ public string this[char c, int i] { set { ... }; } ## Object Initializers with anonymous types -Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of [anonymous types](./anonymous-types.md), which can only be initialized by using an object initializer, as shown in the following declaration. +Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types), which can only be initialized by using an object initializer, as shown in the following declaration. ```csharp var pet = new { Age = 10, Name = "Fluffy" }; @@ -121,4 +121,4 @@ The following example shows an object that implements - [Standard Query Operators Overview (C#)](./standard-query-operators-overview.md) -- [Anonymous Types](../../classes-and-structs/anonymous-types.md) +- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) - [Formulate Joins and Cross-Product Queries](../../../../framework/data/adonet/sql/linq/formulate-joins-and-cross-product-queries.md) - [join clause](../../../language-reference/keywords/join-clause.md) - [Join by using composite keys](../../../linq/join-by-using-composite-keys.md) diff --git a/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md b/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md index 4a0303bdb6cde..7827b7bbadffe 100644 --- a/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md +++ b/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md @@ -16,7 +16,7 @@ LINQ queries are based on generic types, which were introduced in version 2.0 of 2. is the interface that enables generic collection classes to be enumerated by using the `foreach` statement. Generic collection classes support just as non-generic collection classes such as support . - For more information about generics, see [Generics](../../generics/index.md). + For more information about generics, see [Generics](/dotnet/csharp/fundamentals/types/generics). ## IEnumerable variables in LINQ Queries @@ -36,4 +36,4 @@ LINQ queries are based on generic types, which were introduced in version 2.0 of ## See also -- [Generics](../../generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md b/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md index 8ff05548925ea..091ecebea779d 100644 --- a/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md +++ b/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md @@ -129,6 +129,6 @@ You could use it as shown in the following sample to create an enum and build a - - [C# Programming Guide](../index.md) -- [Introduction to Generics](./index.md) +- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) - [Generic Classes](./generic-classes.md) - [new Constraint](../../language-reference/keywords/new-constraint.md) diff --git a/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md b/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md index a064061f2638b..53e164f4bbe8b 100644 --- a/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md +++ b/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md @@ -31,5 +31,5 @@ C# Generics and C++ templates are both language features that provide support fo ## See also - [C# Programming Guide](../index.md) -- [Introduction to Generics](./index.md) +- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) - [Templates](/cpp/cpp/templates-cpp) diff --git a/docs/csharp/programming-guide/generics/generic-classes.md b/docs/csharp/programming-guide/generics/generic-classes.md index 1ceccbcd40c78..18754d4dda47f 100644 --- a/docs/csharp/programming-guide/generics/generic-classes.md +++ b/docs/csharp/programming-guide/generics/generic-classes.md @@ -31,7 +31,7 @@ Generic classes encapsulate operations that are not specific to a particular dat For example, if you are designing a class that will be used to create items in a generics-based collection, you may have to implement an interface such as where `T` is the type of your class. - For an example of a simple generic class, see [Introduction to Generics](./index.md). + For an example of a simple generic class, see [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics). The rules for type parameters and constraints have several implications for generic class behavior, especially regarding inheritance and member accessibility. Before proceeding, you should understand some terms. For a generic class `Node,` client code can reference the class either by specifying a type argument, to create a closed constructed type (`Node`). Alternatively, it can leave the type parameter unspecified, for example when you specify a generic base class, to create an open constructed type (`Node`). Generic classes can inherit from concrete, closed constructed, or open constructed base classes: @@ -65,6 +65,6 @@ Generic classes encapsulate operations that are not specific to a particular dat - - [C# Programming Guide](../index.md) -- [Generics](./index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) - [Saving the State of Enumerators](/archive/blogs/wesdyer/saving-the-state-of-enumerators) - [An Inheritance Puzzle, Part One](/archive/blogs/ericlippert/an-inheritance-puzzle-part-one) diff --git a/docs/csharp/programming-guide/generics/generic-delegates.md b/docs/csharp/programming-guide/generics/generic-delegates.md index 482747f216d51..17e1eff68a5e5 100644 --- a/docs/csharp/programming-guide/generics/generic-delegates.md +++ b/docs/csharp/programming-guide/generics/generic-delegates.md @@ -33,7 +33,7 @@ A [delegate](../../language-reference/builtin-types/reference-types.md) can defi - - [C# Programming Guide](../index.md) -- [Introduction to Generics](./index.md) +- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) - [Generic Methods](./generic-methods.md) - [Generic Classes](./generic-classes.md) - [Generic Interfaces](./generic-interfaces.md) diff --git a/docs/csharp/programming-guide/generics/generic-interfaces.md b/docs/csharp/programming-guide/generics/generic-interfaces.md index d0e045a6c7eaf..47061e215ca5e 100644 --- a/docs/csharp/programming-guide/generics/generic-interfaces.md +++ b/docs/csharp/programming-guide/generics/generic-interfaces.md @@ -11,7 +11,7 @@ ms.assetid: a8fa49a1-6e78-4a09-87e5-84a0b9f5ffbe It is often useful to define interfaces either for generic collection classes, or for the generic classes that represent items in the collection. The preference for generic classes is to use generic interfaces, such as rather than , in order to avoid boxing and unboxing operations on value types. The .NET class library defines several generic interfaces for use with the collection classes in the namespace. - When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used. The following code example shows a `SortedList` class that derives from the `GenericList` class. For more information, see [Introduction to Generics](./index.md). `SortedList` adds the constraint `where T : IComparable`. This enables the `BubbleSort` method in `SortedList` to use the generic method on list elements. In this example, list elements are a simple class, `Person`, that implements `IComparable`. + When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used. The following code example shows a `SortedList` class that derives from the `GenericList` class. For more information, see [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics). `SortedList` adds the constraint `where T : IComparable`. This enables the `BubbleSort` method in `SortedList` to use the generic method on list elements. In this example, list elements are a simple class, `Person`, that implements `IComparable`. [!code-csharp[csProgGuideGenerics#29](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics2.cs#29)] @@ -42,6 +42,6 @@ It is often useful to define interfaces either for generic collection classes, o ## See also - [C# Programming Guide](../index.md) -- [Introduction to Generics](./index.md) +- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) - [interface](../../language-reference/keywords/interface.md) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/generics/generic-methods.md b/docs/csharp/programming-guide/generics/generic-methods.md index a9f7c9c1ccaa9..1ce1695a39783 100644 --- a/docs/csharp/programming-guide/generics/generic-methods.md +++ b/docs/csharp/programming-guide/generics/generic-methods.md @@ -46,5 +46,5 @@ A generic method is a method that is declared with type parameters, as follows: - - [C# Programming Guide](../index.md) -- [Introduction to Generics](./index.md) +- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) - [Methods](../classes-and-structs/methods.md) diff --git a/docs/csharp/programming-guide/generics/generic-type-parameters.md b/docs/csharp/programming-guide/generics/generic-type-parameters.md index 37a6db06c0ecc..b182c484f300a 100644 --- a/docs/csharp/programming-guide/generics/generic-type-parameters.md +++ b/docs/csharp/programming-guide/generics/generic-type-parameters.md @@ -9,7 +9,7 @@ ms.assetid: a03b0ab2-0606-4b41-b7bf-e64d5bb4d18f --- # Generic type parameters (C# Programming Guide) -In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they create an instance of the generic type. A generic class, such as `GenericList` listed in [Introduction to Generics](./index.md), cannot be used as-is because it is not really a type; it is more like a blueprint for a type. To use `GenericList`, client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler. Any number of constructed type instances can be created, each one using a different type argument, as follows: +In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they create an instance of the generic type. A generic class, such as `GenericList` listed in [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics), cannot be used as-is because it is not really a type; it is more like a blueprint for a type. To use `GenericList`, client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler. Any number of constructed type instances can be created, each one using a different type argument, as follows: [!code-csharp[csProgGuideGenerics#7](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs#7)] @@ -37,5 +37,5 @@ The code analysis rule [CA1715](/visualstudio/code-quality/ca1715) can be used t - - [C# Programming Guide](../index.md) -- [Generics](./index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) - [Differences Between C++ Templates and C# Generics](./differences-between-cpp-templates-and-csharp-generics.md) diff --git a/docs/csharp/programming-guide/generics/generics-and-arrays.md b/docs/csharp/programming-guide/generics/generics-and-arrays.md index 2901c54dcef1c..5ae9f5a84173b 100644 --- a/docs/csharp/programming-guide/generics/generics-and-arrays.md +++ b/docs/csharp/programming-guide/generics/generics-and-arrays.md @@ -19,6 +19,6 @@ In C# 2.0 and later, single-dimensional arrays that have a lower bound of zero a - - [C# Programming Guide](../index.md) -- [Generics](./index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) - [Arrays](../arrays/index.md) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/generics/generics-and-attributes.md b/docs/csharp/programming-guide/generics/generics-and-attributes.md index 038da8f5a3b26..b41c05ef6d632 100644 --- a/docs/csharp/programming-guide/generics/generics-and-attributes.md +++ b/docs/csharp/programming-guide/generics/generics-and-attributes.md @@ -42,5 +42,5 @@ Attributes can be applied to generic types in the same way as non-generic types. ## See also - [C# Programming Guide](../index.md) -- [Generics](./index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) - [Attributes](../../../standard/attributes/index.md) diff --git a/docs/csharp/programming-guide/generics/generics-and-reflection.md b/docs/csharp/programming-guide/generics/generics-and-reflection.md index c5adc2543d52d..28c587e0d6d33 100644 --- a/docs/csharp/programming-guide/generics/generics-and-reflection.md +++ b/docs/csharp/programming-guide/generics/generics-and-reflection.md @@ -43,6 +43,6 @@ Because the Common Language Runtime (CLR) has access to generic type information ## See also - [C# Programming Guide](../index.md) -- [Generics](./index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) - [Reflection and Generic Types](../../../framework/reflection-and-codedom/reflection-and-generic-types.md) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/generics/generics-in-the-run-time.md b/docs/csharp/programming-guide/generics/generics-in-the-run-time.md index cf78284a3253f..3de147c785995 100644 --- a/docs/csharp/programming-guide/generics/generics-in-the-run-time.md +++ b/docs/csharp/programming-guide/generics/generics-in-the-run-time.md @@ -46,5 +46,5 @@ When a generic type or method is compiled into Microsoft intermediate language ( - - [C# Programming Guide](../index.md) -- [Introduction to Generics](./index.md) +- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/index.md b/docs/csharp/programming-guide/index.md index c8c001f2a4ba7..565e045c7cf27 100644 --- a/docs/csharp/programming-guide/index.md +++ b/docs/csharp/programming-guide/index.md @@ -21,19 +21,19 @@ This section provides detailed information on key C# language features and featu ## Program sections -[Inside a C# Program](./inside-a-program/index.md) +[Inside a C# Program](/dotnet/csharp/fundamentals/program-structure) -[Main() and Command-Line Arguments](./main-and-command-args/index.md) +[Main() and Command-Line Arguments](/dotnet/csharp/fundamentals/program-structure/main-command-line) ## Language Sections [Statements, Expressions, and Operators](./statements-expressions-operators/index.md) - [Types](./types/index.md) + [Types](/dotnet/csharp/fundamentals/types) - [Classes, structs, and records](./classes-and-structs/index.md) + [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Interfaces](./interfaces/index.md) + [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) [Delegates](./delegates/index.md) @@ -47,13 +47,13 @@ This section provides detailed information on key C# language features and featu [Events](./events/index.md) - [Generics](./generics/index.md) + [Generics](/dotnet/csharp/fundamentals/types/generics) [Iterators](./concepts/iterators.md) [LINQ Query Expressions](../linq/index.md) - [Namespaces](./namespaces/index.md) + [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) [Unsafe Code and Pointers](../language-reference/unsafe-code.md) @@ -69,7 +69,7 @@ This section provides detailed information on key C# language features and featu [Collections](./concepts/collections.md) - [Exceptions and Exception Handling](./exceptions/index.md) + [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions) [File System and the Registry (C# Programming Guide)](./file-system/index.md) diff --git a/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md b/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md index 8b9a2330c2119..1d73166e9350e 100644 --- a/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md +++ b/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md @@ -61,4 +61,4 @@ implements the indexer on the `ICitizen` interface. - [C# Programming Guide](../index.md) - [Indexers](./index.md) - [Properties](../classes-and-structs/properties.md) -- [Interfaces](../interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) diff --git a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md index 0e558c7d1fc56..fe69805e13d0a 100644 --- a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md +++ b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md @@ -43,6 +43,6 @@ Any class that implements the `IControl` interface can override the default `Pai ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](../classes-and-structs/index.md) -- [Interfaces](./index.md) -- [Inheritance](../classes-and-structs/inheritance.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) diff --git a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md index 8fda869e66c57..48c1e00832ad4 100644 --- a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md +++ b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md @@ -28,6 +28,6 @@ This example declares an [interface](../../language-reference/keywords/interface ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](../classes-and-structs/index.md) -- [Interfaces](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) - [How to explicitly implement members of two interfaces](./how-to-explicitly-implement-members-of-two-interfaces.md) diff --git a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md index 9a1394e051c64..203c97be85e63 100644 --- a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md +++ b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md @@ -29,6 +29,6 @@ Explicit [interface](../../language-reference/keywords/interface.md) implementat ## See also - [C# Programming Guide](../index.md) -- [Classes, structs, and records](../classes-and-structs/index.md) -- [Interfaces](./index.md) +- [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) - [How to explicitly implement interface members](./how-to-explicitly-implement-interface-members.md) diff --git a/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md b/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md index 95c071537bc7c..76f03a678db94 100644 --- a/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md +++ b/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md @@ -39,5 +39,5 @@ The namespace (`My` in Visual Basic) pro ## See also - [C# Programming Guide](../index.md) -- [Namespaces](./index.md) +- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) - [Using Namespaces](./using-namespaces.md) diff --git a/docs/csharp/programming-guide/namespaces/using-namespaces.md b/docs/csharp/programming-guide/namespaces/using-namespaces.md index 1b7efe942f6d9..ffd9d26b17f42 100644 --- a/docs/csharp/programming-guide/namespaces/using-namespaces.md +++ b/docs/csharp/programming-guide/namespaces/using-namespaces.md @@ -88,7 +88,7 @@ Namespaces are heavily used within C# programs in two ways. Firstly, the .NET cl ## See also - [C# Programming Guide](../index.md) -- [Namespaces](./index.md) +- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) - [Member access expression](../../language-reference/operators/member-access-operators.md#member-access-expression-) - [:: operator](../../language-reference/operators/namespace-alias-qualifier.md) - [extern alias](../../language-reference/keywords/extern-alias.md) diff --git a/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md b/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md index 3e3c50e3ef587..0400c8b1b16ba 100644 --- a/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md +++ b/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md @@ -50,8 +50,8 @@ However, there is no requirement that equivalence be based on all the fields in |[How to test for reference equality (Identity)](./how-to-test-for-reference-equality-identity.md)|Describes how to determine whether two variables have reference equality.| |[How to define value equality for a type](./how-to-define-value-equality-for-a-type.md)|Describes how to provide a custom definition of value equality for a type.| |[C# Programming Guide](../index.md)|Provides links to detailed information about important C# language features and features that are available to C# through .NET.| -|[Types](../types/index.md)|Provides information about the C# type system and links to additional information.| -|[Records](../classes-and-structs/records.md)|Provides information about record types, which test for value equality by default.| +|[Types](/dotnet/csharp/fundamentals/types)|Provides information about the C# type system and links to additional information.| +|[Records](/dotnet/csharp/fundamentals/types/records)|Provides information about record types, which test for value equality by default.| ## See also diff --git a/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md b/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md index e04861dd307b2..9bbb89c9a80e6 100644 --- a/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md +++ b/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md @@ -13,7 +13,7 @@ ms.assetid: 4084581e-b931-498b-9534-cf7ef5b68690 --- # How to define value equality for a class or struct (C# Programming Guide) -[Records](../classes-and-structs/records.md) automatically implement value equality. Consider defining a `record` instead of a `class` when your type models data and should implement value equality. +[Records](/dotnet/csharp/fundamentals/types/records) automatically implement value equality. Consider defining a `record` instead of a `class` when your type models data and should implement value equality. When you define a class or struct, you decide whether it makes sense to create a custom definition of value equality (or equivalence) for the type. Typically, you implement value equality when you expect to add objects of the type to a collection, or when their primary purpose is to store a set of fields or properties. You can base your definition of value equality on a comparison of all the fields and properties in the type, or you can base the definition on a subset. diff --git a/docs/csharp/programming-guide/types/casting-and-type-conversions.md b/docs/csharp/programming-guide/types/casting-and-type-conversions.md index b7457074e5d2e..d97a34ac2cbc3 100644 --- a/docs/csharp/programming-guide/types/casting-and-type-conversions.md +++ b/docs/csharp/programming-guide/types/casting-and-type-conversions.md @@ -74,7 +74,7 @@ Animal a = g; Giraffe g2 = (Giraffe)a; ``` -A cast operation between reference types does not change the run-time type of the underlying object; it only changes the type of the value that is being used as a reference to that object. For more information, see [Polymorphism](../classes-and-structs/polymorphism.md). +A cast operation between reference types does not change the run-time type of the underlying object; it only changes the type of the value that is being used as a reference to that object. For more information, see [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism). ## Type conversion exceptions at run time @@ -82,7 +82,7 @@ In some reference type conversions, the compiler cannot determine whether a cast [!code-csharp[csProgGuideTypes#41](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#41)] -The `Test` method has an `Animal` parameter, thus explicitly casting the argument `a` to a `Reptile` makes a dangerous assumption. It is safer to not make assumptions, but rather check the type. C# provides the [is](../../language-reference/operators/type-testing-and-cast.md#is-operator) operator to enable you to test for compatibility before actually performing a cast. For more information, see [How to safely cast using pattern matching and the as and is operators](../../how-to/safely-cast-using-pattern-matching-is-and-as-operators.md). +The `Test` method has an `Animal` parameter, thus explicitly casting the argument `a` to a `Reptile` makes a dangerous assumption. It is safer to not make assumptions, but rather check the type. C# provides the [is](../../language-reference/operators/type-testing-and-cast.md#is-operator) operator to enable you to test for compatibility before actually performing a cast. For more information, see [How to safely cast using pattern matching and the as and is operators](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). ## C# language specification @@ -91,7 +91,7 @@ For more information, see the [Conversions](~/_csharplang/spec/conversions.md) s ## See also - [C# Programming Guide](../index.md) -- [Types](./index.md) +- [Types](/dotnet/csharp/fundamentals/types) - [Cast expression](../../language-reference/operators/type-testing-and-cast.md#cast-expression) - [User-defined conversion operators](../../language-reference/operators/user-defined-conversion-operators.md) - [Generalized Type Conversion](/previous-versions/visualstudio/visual-studio-2013/yy580hbd(v=vs.120)) diff --git a/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md b/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md index 0547a99ebffde..d972de80dcb22 100644 --- a/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md +++ b/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md @@ -48,4 +48,4 @@ In this example, the metho - - -- [Types](./index.md) +- [Types](/dotnet/csharp/fundamentals/types) diff --git a/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md b/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md index 1e99fcb8b9947..fb44f4717aeee 100644 --- a/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md +++ b/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md @@ -64,5 +64,5 @@ These examples show you how to perform the following tasks: ## See also - [Standard Numeric Format Strings](../../../standard/base-types/standard-numeric-format-strings.md) -- [Types](./index.md) +- [Types](/dotnet/csharp/fundamentals/types) - [How to determine whether a string represents a numeric value](../strings/how-to-determine-whether-a-string-represents-a-numeric-value.md) diff --git a/docs/csharp/programming-guide/xmldoc/exception.md b/docs/csharp/programming-guide/xmldoc/exception.md index 8e74f64af69d1..518cf70387c88 100644 --- a/docs/csharp/programming-guide/xmldoc/exception.md +++ b/docs/csharp/programming-guide/xmldoc/exception.md @@ -36,7 +36,7 @@ The `` tag lets you specify which exceptions can be thrown. This tag Compile with [**DocumentationFile**](../../language-reference/compiler-options/output.md#documentationfile) to process documentation comments to a file. -For more information about exception handling, see [Exceptions and Exception Handling](../exceptions/index.md). +For more information about exception handling, see [Exceptions and Exception Handling](/dotnet/csharp/fundamentals/exceptions). ## Example diff --git a/docs/csharp/programming-guide/xmldoc/typeparam.md b/docs/csharp/programming-guide/xmldoc/typeparam.md index c33f4d325ea58..40ee68a3c037d 100644 --- a/docs/csharp/programming-guide/xmldoc/typeparam.md +++ b/docs/csharp/programming-guide/xmldoc/typeparam.md @@ -31,7 +31,7 @@ ms.assetid: 9b99d400-e911-4e55-99c6-64367c96aa4f The `` tag should be used in the comment for a generic type or method declaration to describe a type parameter. Add a tag for each type parameter of the generic type or method. -For more information, see [Generics](../generics/index.md). +For more information, see [Generics](/dotnet/csharp/fundamentals/types/generics). The text for the `` tag will be displayed in IntelliSense, the [Object Browser Window](/visualstudio/ide/viewing-the-structure-of-code#BKMK_ObjectBrowser) code comment web report. diff --git a/docs/csharp/programming-guide/xmldoc/typeparamref.md b/docs/csharp/programming-guide/xmldoc/typeparamref.md index 467c7029922c1..1add68d4ee1ce 100644 --- a/docs/csharp/programming-guide/xmldoc/typeparamref.md +++ b/docs/csharp/programming-guide/xmldoc/typeparamref.md @@ -25,7 +25,7 @@ ms.assetid: 6d8ffc58-12c5-4688-8db6-833a7ded5886 ## Remarks -For more information on type parameters in generic types and methods, see [Generics](../generics/index.md). +For more information on type parameters in generic types and methods, see [Generics](/dotnet/csharp/fundamentals/types/generics). Use this tag to enable consumers of the documentation file to format the word in some distinct way, for example in italics. diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 5b203e2c15bce..8bfe3f5333a6c 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -209,7 +209,7 @@ items: - name: Choose a strategy for enabling nullable reference types href: nullable-migration-strategies.md - name: Deconstructing tuples and other types - href: deconstruct.md + href: /dotnet/csharp/fundamentals/functional/deconstruct - name: Methods href: methods.md - name: Properties @@ -217,7 +217,7 @@ items: - name: Indexers href: indexers.md - name: Discards - href: discards.md + href: /dotnet/csharp/fundamentals/functional/discards - name: Iterators href: iterators.md - name: Delegates & events @@ -281,7 +281,7 @@ items: - name: Handle exceptions in query expressions href: linq/handle-exceptions-in-query-expressions.md - name: Pattern Matching - href: pattern-matching.md + href: /dotnet/csharp/fundamentals/functiona/pattern-matching - name: Write safe, efficient code href: write-safe-efficient-code.md - name: Expression trees @@ -324,7 +324,7 @@ items: - name: Compare strings href: how-to/compare-strings.md - name: Safely cast using pattern matching and is/as operators - href: how-to/safely-cast-using-pattern-matching-is-and-as-operators.md + href: /dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md - name: "How to catch a non-CLS exception" href: how-to/how-to-catch-a-non-cls-exception.md - name: The .NET Compiler Platform SDK (Roslyn APIs) diff --git a/docs/csharp/tour-of-csharp/features.md b/docs/csharp/tour-of-csharp/features.md index a8a1181e52d57..b32c52b0d45f4 100644 --- a/docs/csharp/tour-of-csharp/features.md +++ b/docs/csharp/tour-of-csharp/features.md @@ -58,7 +58,7 @@ An interpolated string is declared using the `$` token. String interpolation eva ## Pattern matching -The C# language provides [***pattern matching***](../pattern-matching.md) expressions to query the state of an object and execute code based on that state. You can inspect types and the values of properties and fields to determine which action to take. The `switch` expression is the primary expression for pattern matching. +The C# language provides [***pattern matching***](/dotnet/csharp/fundamentals/functiona/pattern-matching) expressions to query the state of an object and execute code based on that state. You can inspect types and the values of properties and fields to determine which action to take. The `switch` expression is the primary expression for pattern matching. ## Delegates and lambda expressions @@ -112,7 +112,7 @@ The following code sample demonstrates how to get the `HelpAttribute` instances ## Learn more -You can explore more about C# by trying one of our [tutorials](../tutorials/intro-to-csharp/introduction-to-classes.md). +You can explore more about C# by trying one of our [tutorials](/dotnet/csharp/fundamentals/object-oriented/tutorials/classes). >[!div class="step-by-step"] >[Previous](program-building-blocks.md) diff --git a/docs/csharp/tour-of-csharp/index.md b/docs/csharp/tour-of-csharp/index.md index 40f56da72bf38..1128da1b7f842 100644 --- a/docs/csharp/tour-of-csharp/index.md +++ b/docs/csharp/tour-of-csharp/index.md @@ -10,7 +10,7 @@ C# (pronounced "See Sharp") is a modern, object-oriented, and type-safe programm C# is an object-oriented, ***component-oriented*** programming language. C# provides language constructs to directly support these concepts, making C# a natural language in which to create and use software components. Since its origin, C# has added features to support new workloads and emerging software design practices. -Several C# features help create robust and durable applications. [***Garbage collection***](../../standard/garbage-collection/index.md) automatically reclaims memory occupied by unreachable unused objects. [***Nullable types***](../nullable-references.md) guard against variables that don't refer to allocated objects. [***Exception handling***](../programming-guide/exceptions/index.md) provides a structured and extensible approach to error detection and recovery. [***Lambda expressions***](../language-reference/operators/lambda-expressions.md) support functional programming techniques. [***Language Integrated Query (LINQ)***](../linq/index.md) syntax creates a common pattern for working with data from any source. Language support for [***asynchronous operations***](../programming-guide/concepts/async/index.md) provides syntax for building distributed systems. C# has a [***unified type system***](../programming-guide/types/index.md). All C# types, including primitive types such as `int` and `double`, inherit from a single root `object` type. All types share a set of common operations. Values of any type can be stored, transported, and operated upon in a consistent manner. Furthermore, C# supports both user-defined [reference types](../language-reference/builtin-types/reference-types.md) and [value types](../language-reference/builtin-types/value-types.md). C# allows dynamic allocation of objects and in-line storage of lightweight structures. C# supports generic methods and types, which provide increased type safety and performance. C# provides iterators, which enable implementers of collection classes to define custom behaviors for client code. +Several C# features help create robust and durable applications. [***Garbage collection***](../../standard/garbage-collection/index.md) automatically reclaims memory occupied by unreachable unused objects. [***Nullable types***](../nullable-references.md) guard against variables that don't refer to allocated objects. [***Exception handling***](/dotnet/csharp/fundamentals/exceptions) provides a structured and extensible approach to error detection and recovery. [***Lambda expressions***](../language-reference/operators/lambda-expressions.md) support functional programming techniques. [***Language Integrated Query (LINQ)***](../linq/index.md) syntax creates a common pattern for working with data from any source. Language support for [***asynchronous operations***](../programming-guide/concepts/async/index.md) provides syntax for building distributed systems. C# has a [***unified type system***](/dotnet/csharp/fundamentals/types). All C# types, including primitive types such as `int` and `double`, inherit from a single root `object` type. All types share a set of common operations. Values of any type can be stored, transported, and operated upon in a consistent manner. Furthermore, C# supports both user-defined [reference types](../language-reference/builtin-types/reference-types.md) and [value types](../language-reference/builtin-types/value-types.md). C# allows dynamic allocation of objects and in-line storage of lightweight structures. C# supports generic methods and types, which provide increased type safety and performance. C# provides iterators, which enable implementers of collection classes to define custom behaviors for client code. C# emphasizes ***versioning*** to ensure programs and libraries can evolve over time in a compatible manner. Aspects of C#'s design that were directly influenced by versioning considerations include the separate `virtual` and `override` modifiers, the rules for method overload resolution, and support for explicit interface member declarations. @@ -118,7 +118,7 @@ There are several kinds of *variables* in C#, including fields, array elements, ## Program structure -The key organizational concepts in C# are [***programs***](../programming-guide/inside-a-program/index.md), [***namespaces***](../programming-guide/namespaces/index.md), [***types***](../programming-guide/types/index.md), [***members***](../programming-guide/classes-and-structs/members.md), and [***assemblies***](../../standard/assembly/index.md). Programs declare types, which contain members and can be organized into namespaces. Classes, structs, and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they're physically packaged into assemblies. Assemblies typically have the file extension `.exe` or `.dll`, depending on whether they implement ***applications*** or ***libraries***, respectively. +The key organizational concepts in C# are [***programs***](/dotnet/csharp/fundamentals/program-structure), [***namespaces***](/dotnet/csharp/fundamentals/types/namespaces), [***types***](/dotnet/csharp/fundamentals/types), [***members***](../programming-guide/classes-and-structs/members.md), and [***assemblies***](../../standard/assembly/index.md). Programs declare types, which contain members and can be organized into namespaces. Classes, structs, and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they're physically packaged into assemblies. Assemblies typically have the file extension `.exe` or `.dll`, depending on whether they implement ***applications*** or ***libraries***, respectively. As a small example, consider an assembly that contains the following code: diff --git a/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md b/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md index 27b65e958eada..1f78cedb78a64 100644 --- a/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md +++ b/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md @@ -205,6 +205,6 @@ You can see an example solution by [looking at the finished sample code on GitHu With each iteration of the loop, you're taking the last two integers in the list, summing them, and adding that value to the list. The loop repeats until you've added 20 items to the list. -Congratulations, you've completed the list tutorial. You can continue with [additional](../../tutorials/intro-to-csharp/introduction-to-classes.md) tutorials in your own development environment. +Congratulations, you've completed the list tutorial. You can continue with [additional](/dotnet/csharp/fundamentals/object-oriented/tutorials/classes) tutorials in your own development environment. You can learn more about working with the `List` type in the .NET fundamentals article on [collections](../../../standard/collections/index.md). You'll also learn about many other collection types. diff --git a/docs/csharp/tour-of-csharp/types.md b/docs/csharp/tour-of-csharp/types.md index 9ec8aee666709..4592a9a053214 100644 --- a/docs/csharp/tour-of-csharp/types.md +++ b/docs/csharp/tour-of-csharp/types.md @@ -32,7 +32,7 @@ The memory occupied by an object is automatically reclaimed when the object is n ### Type parameters -Generic classes define [***type parameters***](../programming-guide/generics/index.md). Type parameters are a list of type parameter names enclosed in angle brackets. Type parameters follow the class name. The type parameters can then be used in the body of the class declarations to define the members of the class. In the following example, the type parameters of `Pair` are `TFirst` and `TSecond`: +Generic classes define [***type parameters***](/dotnet/csharp/fundamentals/types/generics). Type parameters are a list of type parameter names enclosed in angle brackets. Type parameters follow the class name. The type parameters can then be used in the body of the class declarations to define the members of the class. In the following example, the type parameters of `Pair` are `TFirst` and `TSecond`: :::code language="csharp" source="./snippets/shared/Types.cs" ID="DefinePairClass"::: @@ -63,7 +63,7 @@ Classes define types that support inheritance and polymorphism. They enable you ## Interfaces -An [***interface***](../programming-guide/interfaces/index.md) defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers. An interface typically doesn't provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface. +An [***interface***](/dotnet/csharp/fundamentals/types/interfaces) defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers. An interface typically doesn't provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface. Interfaces may employ ***multiple inheritance***. In the following example, the interface `IComboBox` inherits from both `ITextBox` and `IListBox`. diff --git a/docs/csharp/tutorials/console-webapiclient.md b/docs/csharp/tutorials/console-webapiclient.md index b27492c944bc2..4dbf6ebaca079 100644 --- a/docs/csharp/tutorials/console-webapiclient.md +++ b/docs/csharp/tutorials/console-webapiclient.md @@ -182,7 +182,7 @@ The following steps convert the JSON response into C# objects. You use the is an `await` expression. `await` expressions can appear almost anywhere in your code, even though up to now, you've only seen them as part of an assignment statement. The other two parameters, `JsonSerializerOptions` and `CancellationToken`, are optional and are omitted in the code snippet. - The `DeserializeAsync` method is [*generic*](../programming-guide/generics/index.md), which means you supply type arguments for what kind of objects should be created from the JSON text. In this example, you're deserializing to a `List`, which is another generic object, a . The `List` class stores a collection of objects. The type argument declares the type of objects stored in the `List`. The type argument is your `Repository` class, because the JSON text represents a collection of repository objects. + The `DeserializeAsync` method is [*generic*](/dotnet/csharp/fundamentals/types/generics), which means you supply type arguments for what kind of objects should be created from the JSON text. In this example, you're deserializing to a `List`, which is another generic object, a . The `List` class stores a collection of objects. The type argument declares the type of objects stored in the `List`. The type argument is your `Repository` class, because the JSON text represents a collection of repository objects. 1. Add code to display the name of each repository. Replace the lines that read: diff --git a/docs/csharp/tutorials/exploration/interpolated-strings-local.md b/docs/csharp/tutorials/exploration/interpolated-strings-local.md index 5aeb7a1290f73..af17777b7306b 100644 --- a/docs/csharp/tutorials/exploration/interpolated-strings-local.md +++ b/docs/csharp/tutorials/exploration/interpolated-strings-local.md @@ -40,7 +40,7 @@ Let's try a few more string interpolation examples with some other data types. In the previous section, you used string interpolation to insert one string inside of another. The result of an interpolation expression can be of any data type, though. Let's include values of various data types in an interpolated string. -In the following example, we first define a [class](../../programming-guide/classes-and-structs/classes.md) data type `Vegetable` that has a `Name` [property](../../properties.md) and a `ToString` [method](../../methods.md), which [overrides](../../language-reference/keywords/override.md) the behavior of the method. The [`public` access modifier](../../language-reference/keywords/public.md) makes that method available to any client code to get the string representation of a `Vegetable` instance. In the example the `Vegetable.ToString` method returns the value of the `Name` property that is initialized at the `Vegetable` [constructor](../../programming-guide/classes-and-structs/constructors.md): +In the following example, we first define a [class](/dotnet/csharp/fundamentals/types/classes) data type `Vegetable` that has a `Name` [property](../../properties.md) and a `ToString` [method](../../methods.md), which [overrides](../../language-reference/keywords/override.md) the behavior of the method. The [`public` access modifier](../../language-reference/keywords/public.md) makes that method available to any client code to get the string representation of a `Vegetable` instance. In the example the `Vegetable.ToString` method returns the value of the `Name` property that is initialized at the `Vegetable` [constructor](../../programming-guide/classes-and-structs/constructors.md): ```csharp public Vegetable(string name) => Name = name; diff --git a/docs/csharp/whats-new/csharp-7.md b/docs/csharp/whats-new/csharp-7.md index d07e5e2dbe499..30a987d34fc74 100644 --- a/docs/csharp/whats-new/csharp-7.md +++ b/docs/csharp/whats-new/csharp-7.md @@ -131,7 +131,7 @@ public class Example } ``` -For more information, see [Discards](../discards.md). +For more information, see [Discards](/dotnet/csharp/fundamentals/functional/discards). ## Pattern matching @@ -194,7 +194,7 @@ public static int SumPositiveNumbers(IEnumerable sequence) Beginning with C# 7.1, the pattern expression for `is` and the `switch` type pattern may have the type of a generic type parameter. This can be most useful when checking types that may be either `struct` or `class` types, and you want to avoid boxing. -You can learn more about pattern matching in [Pattern Matching in C#](../pattern-matching.md). +You can learn more about pattern matching in [Pattern Matching in C#](/dotnet/csharp/fundamentals/functiona/pattern-matching). ## Async main @@ -228,7 +228,7 @@ static async Task Main() } ``` -You can read more about the details in the [async main](../programming-guide/main-and-command-args/index.md) article in the programming guide. +You can read more about the details in the [async main](/dotnet/csharp/fundamentals/program-structure/main-command-line) article in the programming guide. ## Local functions diff --git a/docs/csharp/whats-new/csharp-8.md b/docs/csharp/whats-new/csharp-8.md index 24042b996972f..fb31f4cc0d7c8 100644 --- a/docs/csharp/whats-new/csharp-8.md +++ b/docs/csharp/whats-new/csharp-8.md @@ -259,7 +259,7 @@ static Quadrant GetQuadrant(Point point) => point switch The discard pattern in the preceding switch matches when either `x` or `y` is 0, but not both. A switch expression must either produce a value or throw an exception. If none of the cases match, the switch expression throws an exception. The compiler generates a warning for you if you don't cover all possible cases in your switch expression. -You can explore pattern matching techniques in this [advanced tutorial on pattern matching](../tutorials/pattern-matching.md). For more information about a positional pattern, see the [Positional pattern](../language-reference/operators/patterns.md#positional-pattern) section of the [Patterns](../language-reference/operators/patterns.md) article. +You can explore pattern matching techniques in this [advanced tutorial on pattern matching](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching). For more information about a positional pattern, see the [Positional pattern](../language-reference/operators/patterns.md#positional-pattern) section of the [Patterns](../language-reference/operators/patterns.md) article. ## Using declarations diff --git a/docs/csharp/whats-new/csharp-9.md b/docs/csharp/whats-new/csharp-9.md index 9ed57b3feb9b5..e44b09e18fca7 100644 --- a/docs/csharp/whats-new/csharp-9.md +++ b/docs/csharp/whats-new/csharp-9.md @@ -191,7 +191,7 @@ One of the most common uses for this feature is creating teaching materials. Beg Most importantly, top-level statements don't limit your application's scope or complexity. Those statements can access or use any .NET class. They also don't limit your use of command-line arguments or return values. Top-level statements can access an array of strings named `args`. If the top-level statements return an integer value, that value becomes the integer return code from a synthesized `Main` method. The top-level statements may contain async expressions. In that case, the synthesized entry point returns a `Task`, or `Task`. -For more information, see [Top-level statements](../programming-guide/main-and-command-args/top-level-statements.md) in the C# Programming Guide. +For more information, see [Top-level statements](/dotnet/csharp/fundamentals/program-structure/top-level-statements) in the C# Programming Guide. ## Pattern matching enhancements diff --git a/docs/csharp/whats-new/csharp-version-history.md b/docs/csharp/whats-new/csharp-version-history.md index dc88947fba697..ee53fcc630329 100644 --- a/docs/csharp/whats-new/csharp-version-history.md +++ b/docs/csharp/whats-new/csharp-version-history.md @@ -31,9 +31,9 @@ C# version 1.0 looked stripped of features, compared to today. You'd find yourse The major features of C# 1.0 included: -- [Classes](../programming-guide/classes-and-structs/classes.md) +- [Classes](/dotnet/csharp/fundamentals/types/classes) - [Structs](../language-reference/builtin-types/struct.md) -- [Interfaces](../programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) - [Events](../events-overview.md) - [Properties](../properties.md) - [Delegates](../delegates-overview.md) @@ -49,7 +49,7 @@ C# version 1.2 shipped with Visual Studio .NET 2003. It contained a few small en Now things start to get interesting. Let's take a look at some major features of C# 2.0, released in 2005, along with Visual Studio 2005: -- [Generics](../programming-guide/generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) - [Partial types](../programming-guide/classes-and-structs/partial-classes-and-methods.md#partial-classes) - [Anonymous methods](../language-reference/operators/delegate-operator.md) - [Nullable value types](../language-reference/builtin-types/nullable-value-types.md) @@ -76,7 +76,7 @@ And yet, C# continued to play a bit of catch-up with Java. Java had already rele C# version 3.0 came in late 2007, along with Visual Studio 2008, though the full boat of language features would actually come with .NET Framework version 3.5. This version marked a major change in the growth of C#. It established C# as a truly formidable programming language. Let's take a look at some major features in this version: - [Auto-implemented properties](../programming-guide/classes-and-structs/auto-implemented-properties.md) -- [Anonymous types](../programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) - [Query expressions](../linq/query-expression-basics.md) - [Lambda expressions](../language-reference/operators/lambda-expressions.md) - [Expression trees](../expression-trees.md) diff --git a/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md b/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md index c95fbe7313caa..c13b9baff8e07 100644 --- a/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md +++ b/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md @@ -97,7 +97,7 @@ The `HalogenLight` you created earlier doesn't support blinking. So, don't add t ## Detect the light types using pattern matching -Next, let's write some test code. You can make use of C#'s [pattern matching](../../pattern-matching.md) feature to determine a light's capabilities by examining which interfaces it supports. The following method exercises the supported capabilities of each light: +Next, let's write some test code. You can make use of C#'s [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) feature to determine a light's capabilities by examining which interfaces it supports. The following method exercises the supported capabilities of each light: [!code-csharp[Test a light's capabilities](./snippets/mixins-with-default-interface-methods/Program.cs?name=SnippetTestLightFunctions)] diff --git a/docs/csharp/whats-new/tutorials/top-level-statements.md b/docs/csharp/whats-new/tutorials/top-level-statements.md index 915196d7a1dc8..639c5df2513c7 100644 --- a/docs/csharp/whats-new/tutorials/top-level-statements.md +++ b/docs/csharp/whats-new/tutorials/top-level-statements.md @@ -216,4 +216,4 @@ This adds the call to `Utilities.ShowConsoleAnimation`, and adds an additional ` Top-level statements make it easier to create simple programs for use to explore new algorithms. You can experiment with algorithms by trying different snippets of code. Once you've learned what works, you can refactor the code to be more maintainable. -Top-level statements simplify programs that are based on console applications. These include Azure functions, GitHub actions, and other small utilities. For more information, see [Top-level statements (C# Programming Guide)](../../programming-guide/main-and-command-args/top-level-statements.md). +Top-level statements simplify programs that are based on console applications. These include Azure functions, GitHub actions, and other small utilities. For more information, see [Top-level statements (C# Programming Guide)](/dotnet/csharp/fundamentals/program-structure/top-level-statements). diff --git a/docs/csharp/write-safe-efficient-code.md b/docs/csharp/write-safe-efficient-code.md index 87bb3c26be269..da3d51a542c8d 100644 --- a/docs/csharp/write-safe-efficient-code.md +++ b/docs/csharp/write-safe-efficient-code.md @@ -23,7 +23,7 @@ This article focuses on the following resource management techniques: - Never pass a `struct` as an `in` parameter unless it's declared with the `readonly` modifier or the method calls only `readonly` members of the struct. Violating this guidance may negatively affect performance and could lead to an obscure behavior. - Use a [`ref struct`](language-reference/builtin-types/struct.md#ref-struct), or a `readonly ref struct` such as or to work with memory as a sequence of bytes. -These techniques force you to balance two competing goals with regard to **references** and **values**. Variables that are [reference types](programming-guide/types/index.md#reference-types) hold a reference to the location in memory. Variables that are [value types](programming-guide/types/index.md#value-types) directly contain their value. These differences highlight the key differences that are important for managing memory resources. **Value types** are typically copied when passed to a method or returned from a method. This behavior includes copying the value of `this` when calling members of a value type. The cost of the copy is related to the size of the type. **Reference types** are allocated on the managed heap. Each new object requires a new allocation, and subsequently must be reclaimed. Both these operations take time. The reference is copied when a reference type is passed as an argument to a method or returned from a method. +These techniques force you to balance two competing goals with regard to **references** and **values**. Variables that are [reference types](/dotnet/csharp/fundamentals/types#reference-types) hold a reference to the location in memory. Variables that are [value types](/dotnet/csharp/fundamentals/types#value-types) directly contain their value. These differences highlight the key differences that are important for managing memory resources. **Value types** are typically copied when passed to a method or returned from a method. This behavior includes copying the value of `this` when calling members of a value type. The cost of the copy is related to the size of the type. **Reference types** are allocated on the managed heap. Each new object requires a new allocation, and subsequently must be reclaimed. Both these operations take time. The reference is copied when a reference type is passed as an argument to a method or returned from a method. This article uses the following example concept of the 3D-point structure to explain these recommendations: diff --git a/docs/fsharp/language-reference/anonymous-records.md b/docs/fsharp/language-reference/anonymous-records.md index 7fd3d03442254..f795b320e7e23 100644 --- a/docs/fsharp/language-reference/anonymous-records.md +++ b/docs/fsharp/language-reference/anonymous-records.md @@ -234,7 +234,7 @@ Anonymous records are useful for sending lightweight data over a network without ### Anonymous records interoperate with C# anonymous types -It is possible to use a .NET API that requires the use of [C# anonymous types](../../csharp/programming-guide/classes-and-structs/anonymous-types.md). C# anonymous types are trivial to interoperate with by using anonymous records. The following example shows how to use anonymous records to call a [LINQ](../../csharp/programming-guide/concepts/linq/index.md) overload that requires an anonymous type: +It is possible to use a .NET API that requires the use of [C# anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types). C# anonymous types are trivial to interoperate with by using anonymous records. The following example shows how to use anonymous records to call a [LINQ](../../csharp/programming-guide/concepts/linq/index.md) overload that requires an anonymous type: ```fsharp open System.Linq diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1000.md b/docs/fundamentals/code-analysis/quality-rules/ca1000.md index e843d34506ed4..da444fdacbaef 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1000.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1000.md @@ -80,4 +80,4 @@ You can configure this option for just this rule, for all rules, or for all rule ## See also -- [Generics](../../../csharp/programming-guide/generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1002.md b/docs/fundamentals/code-analysis/quality-rules/ca1002.md index 6b606161a7e0d..fd73055c23f5c 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1002.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1002.md @@ -72,4 +72,4 @@ You can configure this option for just this rule, for all rules, or for all rule ## See also -[Generics](../../../csharp/programming-guide/generics/index.md) +[Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1003.md b/docs/fundamentals/code-analysis/quality-rules/ca1003.md index 91dd2eb597b7f..d577d781d3ab3 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1003.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1003.md @@ -74,4 +74,4 @@ The following code snippet removes the delegate declaration from the previous ex ## See also -- [Generics](../../../csharp/programming-guide/generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1005.md b/docs/fundamentals/code-analysis/quality-rules/ca1005.md index 6bae9f072ef5e..2f936eed8e6bf 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1005.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1005.md @@ -62,4 +62,4 @@ You can configure this option for just this rule, for all rules, or for all rule ## See also -- [Generics](../../../csharp/programming-guide/generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1010.md b/docs/fundamentals/code-analysis/quality-rules/ca1010.md index b7db22014204e..a716941648cf9 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1010.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1010.md @@ -111,4 +111,4 @@ Changing the base class of an already released class is considered a breaking ch ## See also -- [Generics](../../../csharp/programming-guide/generics/index.md) +- [Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1033.md b/docs/fundamentals/code-analysis/quality-rules/ca1033.md index 886ca10b93812..8222e39038f54 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1033.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1033.md @@ -48,4 +48,4 @@ The following example shows a type, `ViolatingBase`, that violates the rule and ## See also -- [Interfaces](../../../csharp/programming-guide/interfaces/index.md) +- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1801.md b/docs/fundamentals/code-analysis/quality-rules/ca1801.md index 894cf9bcc4571..990374fc63b84 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1801.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1801.md @@ -43,7 +43,7 @@ This rule does not examine the following kinds of methods: - Methods declared with the `extern` (`Declare` statement in Visual Basic) modifier. -This rule does not flag parameters that are named with the [discard](../../../csharp/discards.md) symbol, for example, `_`, `_1`, and `_2`. This reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. +This rule does not flag parameters that are named with the [discard](/dotnet/csharp/fundamentals/functional/discards) symbol, for example, `_`, `_1`, and `_2`. This reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. > [!NOTE] > This rule has been deprecated in favor of [IDE0060](../style-rules/ide0060.md). For information about how to enforce the IDE0060 analyzer at build, see [code-style analysis](../overview.md#code-style-analysis). diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2119.md b/docs/fundamentals/code-analysis/quality-rules/ca2119.md index 9057d333de38d..77abe60daaa1a 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca2119.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca2119.md @@ -67,5 +67,5 @@ The following example exploits the virtual method implementation of the previous ## See also -- [Interfaces (C#)](../../../csharp/programming-guide/interfaces/index.md) +- [Interfaces (C#)](/dotnet/csharp/fundamentals/types/interfaces) - [Interfaces (Visual Basic)](../../../visual-basic/programming-guide/language-features/interfaces/index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0019.md b/docs/fundamentals/code-analysis/style-rules/ide0019.md index 94ad05445eb33..f92a403bbec89 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0019.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0019.md @@ -26,7 +26,7 @@ dev_langs: ## Overview -This style rule concerns the use of C# [pattern matching](../../../csharp/pattern-matching.md) over an `as` expression followed by a `null` check. +This style rule concerns the use of C# [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) over an `as` expression followed by a `null` check. ## csharp_style_pattern_matching_over_as_with_null_check @@ -49,7 +49,7 @@ if (s != null) {...} ## See also -- [Pattern matching in C#](../../../csharp/pattern-matching.md) +- [Pattern matching in C#](/dotnet/csharp/fundamentals/functiona/pattern-matching) - [Pattern matching preferences](pattern-matching-preferences.md) - [Code style language rules](language-rules.md) - [Code style rules reference](index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md b/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md index 386755c9027aa..ca01b38631ba7 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md @@ -28,7 +28,7 @@ dev_langs: ## Overview -This style rule concerns the use of C# [pattern matching](../../../csharp/pattern-matching.md) over an `is` check followed by a cast. For example, recommending use of `o is int i` instead of `if (o is int) { ... (int)o ...`. The option value determines if pattern matching is preferred or `is` check followed by a cast is preferred. Different rule IDs are used based on whether or not the cast expression is saved into a separate local variable in original code: +This style rule concerns the use of C# [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) over an `is` check followed by a cast. For example, recommending use of `o is int i` instead of `if (o is int) { ... (int)o ...`. The option value determines if pattern matching is preferred or `is` check followed by a cast is preferred. Different rule IDs are used based on whether or not the cast expression is saved into a separate local variable in original code: - IDE0020: Cast expression _is_ saved into a local variable. For example, original code is `if (o is int) { var i = (int)o; }`, which saves the result of `(int)o` in a local variable. - IDE0038: Cast expression _is not_ saved into a local variable. For example, original code is `if (o is int) { if ((int)o == 1) { ... } }`, which does not save the result of `(int)o` into a local variable. @@ -53,7 +53,7 @@ if (o is int) {var i = (int)o; ... } ## See also -- [Pattern matching in C#](../../../csharp/pattern-matching.md) +- [Pattern matching in C#](/dotnet/csharp/fundamentals/functiona/pattern-matching) - [Pattern matching preferences](pattern-matching-preferences.md) - [Code style language rules](language-rules.md) - [Code style rules reference](index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0050.md b/docs/fundamentals/code-analysis/style-rules/ide0050.md index f8431577f21d5..181c9f3ad3643 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0050.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0050.md @@ -25,7 +25,7 @@ dev_langs: ## Overview -This rule recommends use of [tuples](../../../csharp/language-reference/builtin-types/value-tuples.md) over [anonymous types](../../../csharp/programming-guide/classes-and-structs/anonymous-types.md), when the anonymous type has two or more fields. This rule has no associated code style option. +This rule recommends use of [tuples](../../../csharp/language-reference/builtin-types/value-tuples.md) over [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types), when the anonymous type has two or more fields. This rule has no associated code style option. ## Example @@ -48,7 +48,7 @@ Dim t1 = (a:=1, b:=2) ## See also - [Tuples](../../../csharp/language-reference/builtin-types/value-tuples.md) -- [Anonymous types](../../../csharp/programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) - [Expression-level preferences](expression-level-preferences.md) - [Code style language rules](language-rules.md) - [Code style rules reference](index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0058.md b/docs/fundamentals/code-analysis/style-rules/ide0058.md index 52eefea228b18..291c876d5e6a3 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0058.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0058.md @@ -44,7 +44,7 @@ Users can take one of the following actions to fix this violation: - If the expression has no side effects, remove the entire statement. This improves performance by avoiding unnecessary computation. -- If the expression has side effects, replace the left side of the assignment with a [discard](../../../csharp/discards.md) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. +- If the expression has side effects, replace the left side of the assignment with a [discard](/dotnet/csharp/fundamentals/functional/discards) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. ```csharp _ = Compute(); @@ -56,7 +56,7 @@ Users can take one of the following actions to fix this violation: |-|-| | **Option name** | csharp_style_unused_value_expression_statement_preference | **Applicable languages** | C# | -| **Option values** | `discard_variable` - Prefer to assign an unused expression to a [discard](../../../csharp/discards.md)

`unused_local_variable` - Prefer to assign an unused expression to a local variable that is never used | +| **Option values** | `discard_variable` - Prefer to assign an unused expression to a [discard](/dotnet/csharp/fundamentals/functional/discards)

`unused_local_variable` - Prefer to assign an unused expression to a local variable that is never used | | **Default option value** | `discard_variable` | ### Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0059.md b/docs/fundamentals/code-analysis/style-rules/ide0059.md index 9377d198116d2..04f6dc7a7fb6e 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0059.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0059.md @@ -44,7 +44,7 @@ Users can take one of the following actions to fix this violation: int v = Compute2(); ``` -- If the expression on the right side of the assignment has side effects, replace the left side of the assignment with a [discard](../../../csharp/discards.md) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. +- If the expression on the right side of the assignment has side effects, replace the left side of the assignment with a [discard](/dotnet/csharp/fundamentals/functional/discards) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. ```csharp _ = Compute(); @@ -57,7 +57,7 @@ Users can take one of the following actions to fix this violation: |-|-| | **Option name** | csharp_style_unused_value_assignment_preference | **Applicable languages** | C# | -| **Option values** | `discard_variable` - Prefer to use a [discard](../../../csharp/discards.md) when assigning a value that's not used

`unused_local_variable` - Prefer to use a local variable when assigning a value that's not used | +| **Option values** | `discard_variable` - Prefer to use a [discard](/dotnet/csharp/fundamentals/functional/discards) when assigning a value that's not used

`unused_local_variable` - Prefer to use a local variable when assigning a value that's not used | | **Default option value** | `discard_variable` | ### Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0060.md b/docs/fundamentals/code-analysis/style-rules/ide0060.md index 3ee79c94ca559..a3a6d2fe4cfaa 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0060.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0060.md @@ -29,7 +29,7 @@ dev_langs: This rule flags unused parameters. The option value determines if unused parameters should be flagged only for non-public methods or for both public and non-public methods. -This rule does not flag parameters that are named with the [discard](../../../csharp/discards.md) symbol `_`. In addition, the rule ignores parameters that are named with the discard symbol followed by an integer, for example, `_1`. This behavior reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. +This rule does not flag parameters that are named with the [discard](/dotnet/csharp/fundamentals/functional/discards) symbol `_`. In addition, the rule ignores parameters that are named with the discard symbol followed by an integer, for example, `_1`. This behavior reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. ## dotnet_code_quality_unused_parameters diff --git a/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md b/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md index 6dcc1c580ed30..7a12407409c00 100644 --- a/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md +++ b/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md @@ -12,7 +12,7 @@ dev_langs: ## C# preferences -The style rules in this section concern the use of [pattern matching](../../../csharp/pattern-matching.md) in C#. +The style rules in this section concern the use of [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) in C#. - [Use pattern matching to avoid `as` followed by a `null` check (IDE0019](ide0019.md) - [Use pattern matching to avoid `is` check followed by a cast (IDE0020 and IDE0038)](ide0020-ide0038.md) diff --git a/docs/standard/base-types/choosing-between-anonymous-and-tuple.md b/docs/standard/base-types/choosing-between-anonymous-and-tuple.md index 6c248125d7980..a9ab65f496c17 100644 --- a/docs/standard/base-types/choosing-between-anonymous-and-tuple.md +++ b/docs/standard/base-types/choosing-between-anonymous-and-tuple.md @@ -48,7 +48,7 @@ internal sealed class f__AnonymousType0 } ``` -For more information, see [anonymous types](../../csharp/programming-guide/classes-and-structs/anonymous-types.md). The same functionality exists with tuples when projecting into LINQ queries, you can select properties into tuples. These tuples flow through the query, just as anonymous types would. Now consider the following example using the `System.Tuple`. +For more information, see [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types). The same functionality exists with tuples when projecting into LINQ queries, you can select properties into tuples. These tuples flow through the query, just as anonymous types would. Now consider the following example using the `System.Tuple`. ```csharp-interactive var dates = new[] @@ -114,7 +114,7 @@ As a developer choosing between tuples and anonymous types, there are several fa ## See also -- [Anonymous types](../../csharp/programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) - [Expression trees](../../csharp/expression-trees.md) - [Tuple types (C# reference)](../../csharp/language-reference/builtin-types/value-tuples.md) - [Tuples (Visual Basic)](../../visual-basic/programming-guide/language-features/data-types/tuples.md) diff --git a/docs/standard/generics.md b/docs/standard/generics.md index d79041195d720..f6a9cbe228be7 100644 --- a/docs/standard/generics.md +++ b/docs/standard/generics.md @@ -62,4 +62,4 @@ An additional advantage of the runtime knowing the type of your generic is a bet ## See also -- [C# Programming Guide - Generics](../csharp/programming-guide/generics/index.md) +- [C# Programming Guide - Generics](/dotnet/csharp/fundamentals/types/generics) diff --git a/docs/standard/generics/covariance-and-contravariance.md b/docs/standard/generics/covariance-and-contravariance.md index 5a076643c9dc7..327b8002d9907 100644 --- a/docs/standard/generics/covariance-and-contravariance.md +++ b/docs/standard/generics/covariance-and-contravariance.md @@ -36,7 +36,7 @@ When you're referring to a type system, covariance, contravariance, and invarian You cannot assign an instance of `List` to a variable of type `List` or vice versa. - Covariant type parameters enable you to make assignments that look much like ordinary [Polymorphism](../../csharp/programming-guide/classes-and-structs/polymorphism.md), as shown in the following code. + Covariant type parameters enable you to make assignments that look much like ordinary [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism), as shown in the following code. [!code-csharp[CoContraSimpleIEnum#1](../../../samples/snippets/csharp/VS_Snippets_CLR/cocontrasimpleienum/cs/example.cs#1)] [!code-vb[CoContraSimpleIEnum#1](../../../samples/snippets/visualbasic/VS_Snippets_CLR/cocontrasimpleienum/vb/example.vb#1)] diff --git a/docs/standard/generics/index.md b/docs/standard/generics/index.md index 80f3723902c77..2953f2c4d40b5 100644 --- a/docs/standard/generics/index.md +++ b/docs/standard/generics/index.md @@ -122,7 +122,7 @@ Generics let you tailor a method, class, structure, or interface to the precise The common language runtime provides new opcodes and prefixes to support generic types in Microsoft intermediate language (MSIL), including , , , , and . - Visual C++, C#, and Visual Basic all provide full support for defining and using generics. For more information about language support, see [Generic Types in Visual Basic](../../visual-basic/programming-guide/language-features/data-types/generic-types.md), [Introduction to Generics](../../csharp/programming-guide/generics/index.md), and [Overview of Generics in Visual C++](/cpp/windows/overview-of-generics-in-visual-cpp). + Visual C++, C#, and Visual Basic all provide full support for defining and using generics. For more information about language support, see [Generic Types in Visual Basic](../../visual-basic/programming-guide/language-features/data-types/generic-types.md), [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics), and [Overview of Generics in Visual C++](/cpp/windows/overview-of-generics-in-visual-cpp). ## Nested Types and Generics @@ -140,7 +140,7 @@ Generics let you tailor a method, class, structure, or interface to the precise |[When to Use Generic Collections](../collections/when-to-use-generic-collections.md)|Describes general rules for determining when to use generic collection types.| |[How to: Define a Generic Type with Reflection Emit](../../framework/reflection-and-codedom/how-to-define-a-generic-type-with-reflection-emit.md)|Explains how to generate dynamic assemblies that include generic types and methods.| |[Generic Types in Visual Basic](../../visual-basic/programming-guide/language-features/data-types/generic-types.md)|Describes the generics feature for Visual Basic users, including how-to topics for using and defining generic types.| -|[Introduction to Generics](../../csharp/programming-guide/generics/index.md)|Provides an overview of defining and using generic types for C# users.| +|[Introduction to Generics](/dotnet/csharp/fundamentals/types/generics)|Provides an overview of defining and using generic types for C# users.| |[Overview of Generics in Visual C++](/cpp/windows/overview-of-generics-in-visual-cpp)|Describes the generics feature for C++ users, including the differences between generics and templates.| ## Reference diff --git a/docs/standard/linq/concepts-terminology-functional-transformation.md b/docs/standard/linq/concepts-terminology-functional-transformation.md index d59cae4b782fa..04833292ff212 100644 --- a/docs/standard/linq/concepts-terminology-functional-transformation.md +++ b/docs/standard/linq/concepts-terminology-functional-transformation.md @@ -57,7 +57,7 @@ A structured set of data, usually of a uniform type. To be compatible with LINQ, tuple (anonymous types) \ A mathematical concept, a tuple is a finite sequence of objects, each of a specific type. A tuple is also known as an ordered list. Anonymous types are a language implementation of this concept, which enable an unnamed class type to be declared and an object of that type to be instantiated at the same time. -For more information, see [Anonymous Types (C# Programming Guide)](../../csharp/programming-guide/classes-and-structs/anonymous-types.md) and [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md). +For more information, see [Anonymous Types (C# Programming Guide)](/dotnet/csharp/fundamentals/types/anonymous-types) and [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md). type inference (implicit typing) \ The ability of a compiler to determine the type of a variable in the absence of an explicit type declaration. diff --git a/docs/standard/linq/project-anonymous-type.md b/docs/standard/linq/project-anonymous-type.md index 0d33419d59bb3..d3dd27596ba6f 100644 --- a/docs/standard/linq/project-anonymous-type.md +++ b/docs/standard/linq/project-anonymous-type.md @@ -25,7 +25,7 @@ The type of each property is inferred by the compiler. The type name is generate For more information about anonymous types, see: -- [Anonymous Types (C# Programming Guide)](../../csharp/programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous Types (C# Programming Guide)](/dotnet/csharp/fundamentals/types/anonymous-types) - [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md) ## Example: Project an anonymous type by creating objects in the `select` clause @@ -72,5 +72,5 @@ LETSS:Let's Stop N Shop:Jaime Yorres ## See also -- [Anonymous Types (C# Programming Guide)](../../csharp/programming-guide/classes-and-structs/anonymous-types.md) +- [Anonymous Types (C# Programming Guide)](/dotnet/csharp/fundamentals/types/anonymous-types) - [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md) diff --git a/docs/whats-new/2021-03.md b/docs/whats-new/2021-03.md index 3888520479d54..e3ed3da9124c9 100644 --- a/docs/whats-new/2021-03.md +++ b/docs/whats-new/2021-03.md @@ -38,10 +38,10 @@ Welcome to what's new in the .NET docs from March 1, 2021 through April 1, 2021. - [C# preprocessor directives](../csharp/language-reference/preprocessor-directives.md) - [`nint` and `nuint` types (C# reference)](../csharp/language-reference/builtin-types/nint-nuint.md) -- [Top-level statements (C# Programming Guide)](../csharp/programming-guide/main-and-command-args/top-level-statements.md) +- [Top-level statements (C# Programming Guide)](/dotnet/csharp/fundamentals/program-structure/top-level-statements) - [init (C# Reference)](../csharp/language-reference/keywords/init.md) - [Records (C# reference)](../csharp/language-reference/builtin-types/record.md) -- [Records (C# Programming Guide)](../csharp/programming-guide/classes-and-structs/records.md) +- [Records (C# Programming Guide)](/dotnet/csharp/fundamentals/types/records) ### Updated articles From 22b939c7e098c12e66018c19a89e85b3f29283e3 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 10:52:42 -0400 Subject: [PATCH 43/60] replace relative links dleanrepo --relative-links. --- docs/core/whats-new/dotnet-core-3-0.md | 2 +- docs/csharp/delegate-class.md | 2 +- docs/csharp/delegates-strongly-typed.md | 2 +- docs/csharp/fundamentals/object-oriented/objects.md | 2 +- docs/csharp/fundamentals/types/classes.md | 4 ++-- docs/csharp/fundamentals/types/index.md | 4 ++-- docs/csharp/fundamentals/types/interfaces.md | 4 ++-- docs/csharp/fundamentals/types/namespaces.md | 2 +- docs/csharp/how-to/index.md | 10 +++++----- docs/csharp/iterators.md | 2 +- .../builtin-types/reference-types.md | 2 +- .../language-reference/builtin-types/value-tuples.md | 4 ++-- .../language-reference/compiler-messages/cs0106.md | 2 +- .../language-reference/compiler-messages/cs0116.md | 2 +- .../language-reference/compiler-messages/cs0229.md | 2 +- .../language-reference/compiler-messages/cs1058.md | 2 +- .../language-reference/compiler-messages/cs1519.md | 4 ++-- .../language-reference/compiler-messages/cs1540.md | 4 ++-- .../language-reference/compiler-messages/cs1721.md | 4 ++-- .../language-reference/compiler-messages/cs1729.md | 2 +- .../language-reference/compiler-options/advanced.md | 4 ++-- .../language-reference/compiler-options/output.md | 10 +++++----- docs/csharp/language-reference/keywords/class.md | 4 ++-- docs/csharp/language-reference/keywords/event.md | 4 ++-- docs/csharp/language-reference/keywords/index.md | 2 +- docs/csharp/language-reference/keywords/interface.md | 4 ++-- docs/csharp/language-reference/keywords/namespace.md | 2 +- .../language-reference/keywords/new-constraint.md | 2 +- docs/csharp/language-reference/keywords/override.md | 6 +++--- .../csharp/language-reference/keywords/partial-type.md | 2 +- .../restrictions-on-using-accessibility-levels.md | 4 ++-- docs/csharp/language-reference/keywords/sealed.md | 2 +- .../language-reference/keywords/select-clause.md | 2 +- .../language-reference/keywords/using-directive.md | 2 +- .../csharp/language-reference/keywords/using-static.md | 2 +- docs/csharp/language-reference/keywords/using.md | 2 +- docs/csharp/language-reference/keywords/virtual.md | 2 +- .../keywords/where-generic-type-constraint.md | 2 +- docs/csharp/language-reference/operators/await.md | 4 ++-- .../language-reference/operators/delegate-operator.md | 2 +- docs/csharp/language-reference/operators/is.md | 2 +- .../language-reference/operators/lambda-expressions.md | 2 +- .../language-reference/operators/new-operator.md | 2 +- docs/csharp/language-reference/operators/patterns.md | 6 +++--- .../language-reference/operators/switch-expression.md | 2 +- .../operators/type-testing-and-cast.md | 2 +- docs/csharp/linq/group-query-results.md | 2 +- docs/csharp/linq/perform-grouped-joins.md | 2 +- docs/csharp/linq/perform-inner-joins.md | 2 +- docs/csharp/linq/perform-left-outer-joins.md | 2 +- docs/csharp/methods.md | 2 +- docs/csharp/misc/cs0017.md | 2 +- docs/csharp/misc/cs0026.md | 2 +- docs/csharp/misc/cs0028.md | 2 +- docs/csharp/misc/cs0061.md | 2 +- docs/csharp/misc/cs0068.md | 2 +- docs/csharp/misc/cs0069.md | 2 +- docs/csharp/misc/cs0080.md | 2 +- docs/csharp/misc/cs0081.md | 2 +- docs/csharp/misc/cs0101.md | 2 +- docs/csharp/misc/cs0138.md | 2 +- docs/csharp/misc/cs0144.md | 2 +- docs/csharp/misc/cs0146.md | 2 +- docs/csharp/misc/cs0238.md | 2 +- docs/csharp/misc/cs0666.md | 2 +- docs/csharp/misc/cs0736.md | 2 +- docs/csharp/misc/cs0737.md | 2 +- docs/csharp/misc/cs0738.md | 2 +- docs/csharp/misc/cs0746.md | 2 +- docs/csharp/misc/cs0833.md | 2 +- docs/csharp/misc/cs1527.md | 2 +- docs/csharp/misc/cs1558.md | 2 +- docs/csharp/misc/cs1559.md | 2 +- docs/csharp/misc/cs5001.md | 2 +- .../arrays/implicitly-typed-arrays.md | 2 +- .../abstract-and-sealed-classes-and-class-members.md | 2 +- .../classes-and-structs/access-modifiers.md | 2 +- .../classes-and-structs/extension-methods.md | 2 +- .../programming-guide/classes-and-structs/fields.md | 2 +- ...passing-a-struct-and-passing-a-class-to-a-method.md | 2 +- ...-return-subsets-of-element-properties-in-a-query.md | 2 +- ...local-variables-and-arrays-in-a-query-expression.md | 2 +- .../how-to-write-a-copy-constructor.md | 4 ++-- .../implicitly-typed-local-variables.md | 6 +++--- .../classes-and-structs/interface-properties.md | 2 +- .../programming-guide/classes-and-structs/members.md | 2 +- .../programming-guide/classes-and-structs/methods.md | 6 +++--- .../object-and-collection-initializers.md | 4 ++-- .../classes-and-structs/partial-classes-and-methods.md | 4 ++-- .../static-classes-and-static-class-members.md | 2 +- .../classes-and-structs/using-properties.md | 2 +- .../versioning-with-the-override-and-new-keywords.md | 2 +- docs/csharp/programming-guide/concepts/iterators.md | 2 +- .../concepts/linq/basic-linq-query-operations.md | 2 +- .../concepts/linq/data-transformations-with-linq.md | 2 +- .../concepts/linq/features-that-support-linq.md | 2 +- ...te-object-collections-from-multiple-sources-linq.md | 2 +- .../programming-guide/concepts/linq/join-operations.md | 2 +- .../concepts/linq/linq-and-generic-types.md | 4 ++-- .../generics/constraints-on-type-parameters.md | 2 +- ...rences-between-cpp-templates-and-csharp-generics.md | 2 +- .../programming-guide/generics/generic-classes.md | 4 ++-- .../programming-guide/generics/generic-delegates.md | 2 +- .../programming-guide/generics/generic-interfaces.md | 4 ++-- .../programming-guide/generics/generic-methods.md | 2 +- .../generics/generic-type-parameters.md | 4 ++-- .../programming-guide/generics/generics-and-arrays.md | 2 +- .../generics/generics-and-attributes.md | 2 +- .../generics/generics-and-reflection.md | 2 +- .../generics/generics-in-the-run-time.md | 2 +- docs/csharp/programming-guide/index.md | 8 ++++---- .../indexers/indexers-in-interfaces.md | 2 +- .../interfaces/explicit-interface-implementation.md | 4 ++-- .../how-to-explicitly-implement-interface-members.md | 2 +- ...o-explicitly-implement-members-of-two-interfaces.md | 2 +- .../namespaces/how-to-use-the-my-namespace.md | 2 +- .../programming-guide/namespaces/using-namespaces.md | 2 +- .../equality-comparisons.md | 2 +- .../how-to-define-value-equality-for-a-type.md | 2 +- .../types/casting-and-type-conversions.md | 4 ++-- docs/csharp/programming-guide/xmldoc/typeparam.md | 2 +- docs/csharp/programming-guide/xmldoc/typeparamref.md | 2 +- docs/csharp/tour-of-csharp/features.md | 2 +- docs/csharp/tour-of-csharp/index.md | 2 +- .../tour-of-csharp/tutorials/arrays-and-collections.md | 2 +- docs/csharp/tour-of-csharp/types.md | 4 ++-- docs/csharp/tutorials/console-webapiclient.md | 2 +- .../exploration/interpolated-strings-local.md | 2 +- docs/csharp/whats-new/csharp-7.md | 4 ++-- docs/csharp/whats-new/csharp-8.md | 2 +- docs/csharp/whats-new/csharp-9.md | 2 +- docs/csharp/whats-new/csharp-version-history.md | 8 ++++---- .../csharp/whats-new/tutorials/top-level-statements.md | 2 +- docs/fsharp/language-reference/anonymous-records.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca1000.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca1002.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca1003.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca1005.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca1010.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca1033.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca1801.md | 2 +- .../fundamentals/code-analysis/quality-rules/ca2119.md | 2 +- docs/fundamentals/code-analysis/style-rules/ide0050.md | 4 ++-- docs/fundamentals/code-analysis/style-rules/ide0058.md | 4 ++-- docs/fundamentals/code-analysis/style-rules/ide0059.md | 4 ++-- docs/fundamentals/code-analysis/style-rules/ide0060.md | 2 +- .../base-types/choosing-between-anonymous-and-tuple.md | 4 ++-- docs/standard/generics.md | 2 +- .../standard/generics/covariance-and-contravariance.md | 2 +- docs/standard/generics/index.md | 4 ++-- .../concepts-terminology-functional-transformation.md | 2 +- docs/standard/linq/project-anonymous-type.md | 4 ++-- docs/whats-new/2021-03.md | 4 ++-- 153 files changed, 206 insertions(+), 206 deletions(-) diff --git a/docs/core/whats-new/dotnet-core-3-0.md b/docs/core/whats-new/dotnet-core-3-0.md index 32ff3406b7bc3..068d5e71f95df 100644 --- a/docs/core/whats-new/dotnet-core-3-0.md +++ b/docs/core/whats-new/dotnet-core-3-0.md @@ -28,7 +28,7 @@ Tutorials related to C# 8.0 language features: - [Tutorial: Express your design intent more clearly with nullable and non-nullable reference types](../../csharp/whats-new/tutorials/nullable-reference-types.md) - [Tutorial: Generate and consume async streams using C# 8.0 and .NET Core 3.0](../../csharp/whats-new/tutorials/generate-consume-asynchronous-stream.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../csharp/fundamentals/functional/tutorials/pattern-matching.md) Language enhancements were added to support the following API features detailed below: diff --git a/docs/csharp/delegate-class.md b/docs/csharp/delegate-class.md index 74059db4e6646..61a5a78b4bc90 100644 --- a/docs/csharp/delegate-class.md +++ b/docs/csharp/delegate-class.md @@ -38,7 +38,7 @@ The compiler generates a class, derived from `System.Delegate` that matches the signature used (in this case, a method that returns an integer, and has two arguments). The type of that delegate is `Comparison`. The `Comparison` delegate -type is a generic type. For details on generics see [here](/dotnet/csharp/fundamentals/types/generics). +type is a generic type. For details on generics see [here](fundamentals/types/generics.md). Notice that the syntax may appear as though it is declaring a variable, but it is actually declaring a *type*. You can diff --git a/docs/csharp/delegates-strongly-typed.md b/docs/csharp/delegates-strongly-typed.md index c2fb1cb429b5d..cff7adb3bd5e0 100644 --- a/docs/csharp/delegates-strongly-typed.md +++ b/docs/csharp/delegates-strongly-typed.md @@ -25,7 +25,7 @@ after a time. Every new feature requires new delegate types. Thankfully, this isn't necessary. The .NET Core framework contains several types that you can reuse whenever you need delegate types. These are -[generic](/dotnet/csharp/fundamentals/types/generics) definitions so you can declare customizations +[generic](fundamentals/types/generics.md) definitions so you can declare customizations when you need new method declarations. The first of these types is the type, and several variations: diff --git a/docs/csharp/fundamentals/object-oriented/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md index c02b9fe4522a6..a962759177478 100644 --- a/docs/csharp/fundamentals/object-oriented/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -47,7 +47,7 @@ When you compare two objects for equality, you must first distinguish whether yo For more information: -- [Classes](/dotnet/csharp/fundamentals/types/classes) +- [Classes](../types/classes.md) - [Constructors](../../programming-guide/classes-and-structs/constructors.md) - [Finalizers](../../programming-guide/classes-and-structs/destructors.md) - [Events](../../programming-guide/events/index.md) diff --git a/docs/csharp/fundamentals/types/classes.md b/docs/csharp/fundamentals/types/classes.md index c7341a7390111..8f3a7739bea7f 100644 --- a/docs/csharp/fundamentals/types/classes.md +++ b/docs/csharp/fundamentals/types/classes.md @@ -34,7 +34,7 @@ When the object is created, enough memory is allocated on the managed heap for t } ``` - The `class` keyword is preceded by the access level. Because [`public`](../../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 name of the class must be a valid C# [identifier name](/dotnet/csharp/fundamentals/coding-style/identifier-names). 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*. + The `class` keyword is preceded by the access level. Because [`public`](../../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 name of the class must be a valid C# [identifier name](../coding-style/identifier-names.md). 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 @@ -75,7 +75,7 @@ Inheritance is accomplished by using a *derivation*, which means a class is decl } ``` -When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). +When a class declares a base class, it inherits all the members of the base class except the constructors. For more information, see [Inheritance](../object-oriented/inheritance.md). 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 one or more interfaces. For more information, see [Interfaces](interfaces.md). diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 1e01113aed133..978408c00982f 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -60,7 +60,7 @@ You use the [`struct`](../../language-reference/builtin-types/struct.md), [`clas It's important to understand two fundamental points about the type system in .NET: -- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: [`object`](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). +- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as (C# keyword: `int`), derive ultimately from a single base type, which is (C# keyword: [`object`](../../language-reference/builtin-types/reference-types.md)). This unified type hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](../object-oriented/inheritance.md). - Each type in the CTS is defined as either a *value type* or a *reference type*. These types include all custom types in the .NET class library and also your own user-defined types. Types that you define by using the `struct` keyword are value types; all the built-in numeric types are `structs`. Types that you define by using the `class` or `record` keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior. The following illustration shows the relationship between value types and reference types in the CTS. @@ -116,7 +116,7 @@ All arrays are reference types, even if their elements are value types. Arrays i :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="ArrayDeclaration"::: -Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented). For more information about inheritance and virtual methods, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). +Reference types fully support inheritance. When you create a class, you can inherit from any other interface or class that isn't defined as [sealed](../../language-reference/keywords/sealed.md), and other classes can inherit from your class and override your virtual methods. For more information about how to create your own classes, see [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented). For more information about inheritance and virtual methods, see [Inheritance](../object-oriented/inheritance.md). ## Types of literal values diff --git a/docs/csharp/fundamentals/types/interfaces.md b/docs/csharp/fundamentals/types/interfaces.md index 0d6e52fe5922e..9d7ee51b0bc78 100644 --- a/docs/csharp/fundamentals/types/interfaces.md +++ b/docs/csharp/fundamentals/types/interfaces.md @@ -16,7 +16,7 @@ You define an interface by using the [`interface`](../../language-reference/keyw :::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" ID="Equatable"::: -The name of an interface must be a valid C# [identifier name](/dotnet/csharp/fundamentals/coding-style/identifier-names). By convention, interface names begin with a capital `I`. +The name of an interface must be a valid C# [identifier name](../coding-style/identifier-names.md). By convention, interface names begin with a capital `I`. Any class or struct that implements the interface must contain a definition for an method that matches the signature that the interface specifies. As a result, you can count on a class that implements `IEquatable` to contain an `Equals` method with which an instance of the class can determine whether it's equal to another instance of the same class. @@ -38,7 +38,7 @@ Properties and indexers of a class can define extra accessors for a property or Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces. That class may be implicitly converted to the derived interface or any of its base interfaces. A class might include an interface multiple times through base classes that it inherits or through interfaces that other interfaces inherit. However, the class can provide an implementation of an interface only one time and only if the class declares the interface as part of the definition of the class (`class ClassName : InterfaceName`). If the interface is inherited because you inherited a base class that implements the interface, the base class provides the implementation of the members of the interface. However, the derived class can reimplement any virtual interface members instead of using the inherited implementation. When interfaces declare a default implementation of a method, any class implementing that interface inherits that implementation. Implementations defined in interfaces are virtual and the implementing class may override that implementation. -A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members. For more information about virtual members, see [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism). +A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members. For more information about virtual members, see [Polymorphism](../object-oriented/polymorphism.md). ## Interfaces summary diff --git a/docs/csharp/fundamentals/types/namespaces.md b/docs/csharp/fundamentals/types/namespaces.md index ad240ee5e2ef6..325818dd47703 100644 --- a/docs/csharp/fundamentals/types/namespaces.md +++ b/docs/csharp/fundamentals/types/namespaces.md @@ -24,7 +24,7 @@ Second, declaring your own namespaces can help you control the scope of class an [!code-csharp[csProgGuideNamespaces#6](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs#6)] -The name of the namespace must be a valid C# [identifier name](/dotnet/csharp/fundamentals/coding-style/identifier-names). +The name of the namespace must be a valid C# [identifier name](../coding-style/identifier-names.md). ## Namespaces overview diff --git a/docs/csharp/how-to/index.md b/docs/csharp/how-to/index.md index d896452c1f8ce..638c413257927 100644 --- a/docs/csharp/how-to/index.md +++ b/docs/csharp/how-to/index.md @@ -59,7 +59,7 @@ You may need to convert an object to a different type. - [Convert a string to a `DateTime`](../../standard/base-types/parsing-datetime.md). - [Convert a byte array to an int](../programming-guide/types/how-to-convert-a-byte-array-to-an-int.md). - [Convert a string to a number](../programming-guide/types/how-to-convert-a-string-to-a-number.md). -- [Use pattern matching, the `as` and `is` operators to safely cast to a different type](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). +- [Use pattern matching, the `as` and `is` operators to safely cast to a different type](../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). - [Define custom type conversions](../language-reference/operators/user-defined-conversion-operators.md). - [Determine if a type is a nullable value type](../language-reference/builtin-types/nullable-value-types.md#how-to-identify-a-nullable-value-type). - [Convert between nullable and non-nullable value types](../language-reference/builtin-types/nullable-value-types.md#conversion-from-a-nullable-value-type-to-an-underlying-type). @@ -78,9 +78,9 @@ natural ordering among objects of that type. successfully complete their work by throwing exceptions. In these articles you'll learn to work with exceptions. -- [Handle exceptions using `try` and `catch`](/dotnet/csharp/fundamentals/exceptions/how-to-handle-an-exception-using-try-catch). -- [Cleanup resources using `finally` clauses](/dotnet/csharp/fundamentals/exceptions/how-to-execute-cleanup-code-using-finally). -- [Recover from non-CLS (Common Language Specification) exceptions](/dotnet/csharp/how-to/how-to-catch-a-non-cls-exception). +- [Handle exceptions using `try` and `catch`](../fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md). +- [Cleanup resources using `finally` clauses](../fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md). +- [Recover from non-CLS (Common Language Specification) exceptions](how-to-catch-a-non-cls-exception.md). ## Delegates and events @@ -133,4 +133,4 @@ to use these techniques. Typically, C# programs have command line arguments. These articles teach you to access and process those command line arguments. -- [Retrieve all command line arguments with `for`](/dotnet/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments). +- [Retrieve all command line arguments with `for`](../fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md). diff --git a/docs/csharp/iterators.md b/docs/csharp/iterators.md index bb010aba45f62..1e906c3e78a7f 100644 --- a/docs/csharp/iterators.md +++ b/docs/csharp/iterators.md @@ -47,7 +47,7 @@ to generate the code necessary to iterate a collection: `IEnumerable` and `IEnumerator`. This mechanism is explained in more detail below. Both of these interfaces also have non-generic counterparts: `IEnumerable` and -`IEnumerator`. The [generic](/dotnet/csharp/fundamentals/types/generics) versions are preferred for modern code. +`IEnumerator`. The [generic](fundamentals/types/generics.md) versions are preferred for modern code. ## Enumeration sources with iterator methods diff --git a/docs/csharp/language-reference/builtin-types/reference-types.md b/docs/csharp/language-reference/builtin-types/reference-types.md index d4b81ea4ae1c7..60710b8adc2ec 100644 --- a/docs/csharp/language-reference/builtin-types/reference-types.md +++ b/docs/csharp/language-reference/builtin-types/reference-types.md @@ -172,7 +172,7 @@ The following example uses `dynamic` in several declarations. The `Main` method - [Basic String Operations](../../../standard/base-types/basic-string-operations.md) - [Creating New Strings](../../../standard/base-types/creating-new.md) - [Type-testing and cast operators](../operators/type-testing-and-cast.md) -- [How to safely cast using pattern matching and the as and is operators](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) +- [How to safely cast using pattern matching and the as and is operators](../../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) - [Walkthrough: creating and using dynamic objects](../../programming-guide/types/walkthrough-creating-and-using-dynamic-objects.md) - - diff --git a/docs/csharp/language-reference/builtin-types/value-tuples.md b/docs/csharp/language-reference/builtin-types/value-tuples.md index 49bc04158dcce..698b46f3a6285 100644 --- a/docs/csharp/language-reference/builtin-types/value-tuples.md +++ b/docs/csharp/language-reference/builtin-types/value-tuples.md @@ -34,7 +34,7 @@ One of the most common use cases of tuples is as a method return type. That is, As the preceding example shows, you can work with the returned tuple instance directly or [deconstruct](#tuple-assignment-and-deconstruction) it in separate variables. -You can also use tuple types instead of [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types); for example, in LINQ queries. For more information, see [Choosing between anonymous and tuple types](../../../standard/base-types/choosing-between-anonymous-and-tuple.md). +You can also use tuple types instead of [anonymous types](../../fundamentals/types/anonymous-types.md); for example, in LINQ queries. For more information, see [Choosing between anonymous and tuple types](../../../standard/base-types/choosing-between-anonymous-and-tuple.md). Typically, you use tuples to group loosely related data elements. That is usually useful within private and internal utility methods. In the case of public API, consider defining a [class](../keywords/class.md) or a [structure](struct.md) type. @@ -88,7 +88,7 @@ You can also use the assignment operator `=` to *deconstruct* a tuple instance i [!code-csharp-interactive[existing variables](snippets/shared/ValueTuples.cs#DeconstructExisting)] -For more information about deconstruction of tuples and other types, see [Deconstructing tuples and other types](/dotnet/csharp/fundamentals/functional/deconstruct). +For more information about deconstruction of tuples and other types, see [Deconstructing tuples and other types](../../fundamentals/functional/deconstruct.md). ## Tuple equality diff --git a/docs/csharp/language-reference/compiler-messages/cs0106.md b/docs/csharp/language-reference/compiler-messages/cs0106.md index b2ced15e1deaf..b7c7d2c335d0d 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0106.md +++ b/docs/csharp/language-reference/compiler-messages/cs0106.md @@ -24,7 +24,7 @@ The modifier 'modifier' is not valid for this item In prior releases of Visual Studio, the `static` modifier was not permitted on a class, but `static` classes are allowed starting with Visual Studio 2005. - For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). + For more information, see [Interfaces](../../fundamentals/types/interfaces.md). ## Example diff --git a/docs/csharp/language-reference/compiler-messages/cs0116.md b/docs/csharp/language-reference/compiler-messages/cs0116.md index 92c28ff676f62..1b3e225318b8f 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0116.md +++ b/docs/csharp/language-reference/compiler-messages/cs0116.md @@ -101,4 +101,4 @@ namespace x - [General Structure of a C# Program](/dotnet/csharp/fundamentals/program-structure) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) -- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) +- [Namespaces](../../fundamentals/types/namespaces.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs0229.md b/docs/csharp/language-reference/compiler-messages/cs0229.md index 5a9a0edf2f3ba..e37b2273714aa 100644 --- a/docs/csharp/language-reference/compiler-messages/cs0229.md +++ b/docs/csharp/language-reference/compiler-messages/cs0229.md @@ -12,7 +12,7 @@ ms.assetid: f1ff7e91-1243-4d36-b792-26ba69186f8f Ambiguity between 'member1' and 'member2' - Members of different interfaces have the same name. If you want to keep the same names, you must qualify the names. For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). + Members of different interfaces have the same name. If you want to keep the same names, you must qualify the names. For more information, see [Interfaces](../../fundamentals/types/interfaces.md). > [!NOTE] > In some cases, this ambiguity can be resolved by providing an explicit prefix to the identifier via a [using](../keywords/using-directive.md) alias. diff --git a/docs/csharp/language-reference/compiler-messages/cs1058.md b/docs/csharp/language-reference/compiler-messages/cs1058.md index d0311c99a1734..98cef1189a4c3 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1058.md +++ b/docs/csharp/language-reference/compiler-messages/cs1058.md @@ -14,7 +14,7 @@ A previous catch clause already catches all exceptions. All exceptions thrown wi This attribute causes CS1058 if a `catch()` block has no specified exception type after a `catch (System.Exception e)` block. The warning advises that the `catch()` block will not catch any exceptions. -A `catch()` block after a `catch (System.Exception e)` block can catch non-CLS exceptions if the `RuntimeCompatibilityAttribute` is set to false in the AssemblyInfo.cs file: `[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]`. If this attribute is not set explicitly to false, all thrown non-CLS exceptions are wrapped as Exceptions and the `catch (System.Exception e)` block catches them. For more information, see [How to catch a non-CLS exception](/dotnet/csharp/how-to/how-to-catch-a-non-cls-exception). +A `catch()` block after a `catch (System.Exception e)` block can catch non-CLS exceptions if the `RuntimeCompatibilityAttribute` is set to false in the AssemblyInfo.cs file: `[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]`. If this attribute is not set explicitly to false, all thrown non-CLS exceptions are wrapped as Exceptions and the `catch (System.Exception e)` block catches them. For more information, see [How to catch a non-CLS exception](../../how-to/how-to-catch-a-non-cls-exception.md). ## Example diff --git a/docs/csharp/language-reference/compiler-messages/cs1519.md b/docs/csharp/language-reference/compiler-messages/cs1519.md index baa2dda873ac0..c39caa91f3272 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1519.md +++ b/docs/csharp/language-reference/compiler-messages/cs1519.md @@ -40,7 +40,7 @@ class Test 42 ## See also -- [Classes](/dotnet/csharp/fundamentals/types/classes) +- [Classes](../../fundamentals/types/classes.md) - [Structure types](../builtin-types/struct.md) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) - [Methods](../../programming-guide/classes-and-structs/methods.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs1540.md b/docs/csharp/language-reference/compiler-messages/cs1540.md index e7a42153f3173..d5f973060e113 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1540.md +++ b/docs/csharp/language-reference/compiler-messages/cs1540.md @@ -71,7 +71,7 @@ namespace CS1540 ## See also -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) -- [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism) +- [Inheritance](../../fundamentals/object-oriented/inheritance.md) +- [Polymorphism](../../fundamentals/object-oriented/polymorphism.md) - [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md) - [Abstract and Sealed Classes and Class Members](../../programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs1721.md b/docs/csharp/language-reference/compiler-messages/cs1721.md index 3ffa31c2349ed..492e120c14947 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1721.md +++ b/docs/csharp/language-reference/compiler-messages/cs1721.md @@ -47,5 +47,5 @@ The following are different ways to correct this error: ## See also -- [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Polymorphism](../../fundamentals/object-oriented/polymorphism.md) +- [Interfaces](../../fundamentals/types/interfaces.md) diff --git a/docs/csharp/language-reference/compiler-messages/cs1729.md b/docs/csharp/language-reference/compiler-messages/cs1729.md index 7687d10f6e058..22496871e59d2 100644 --- a/docs/csharp/language-reference/compiler-messages/cs1729.md +++ b/docs/csharp/language-reference/compiler-messages/cs1729.md @@ -72,5 +72,5 @@ public class Child2 : Parent ## See also -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Inheritance](../../fundamentals/object-oriented/inheritance.md) - [Constructors](../../programming-guide/classes-and-structs/constructors.md) diff --git a/docs/csharp/language-reference/compiler-options/advanced.md b/docs/csharp/language-reference/compiler-options/advanced.md index 7988780ab4937..31b2f7d385d5e 100644 --- a/docs/csharp/language-reference/compiler-options/advanced.md +++ b/docs/csharp/language-reference/compiler-options/advanced.md @@ -58,10 +58,10 @@ or MyNamespace.Program ``` -Where `Program` is the type that contains the `Main` method. The provided class name must be fully qualified; it must include the full namespace containing the class, followed by the class name. For example, when the `Main` method is located inside the `Program` class in the `MyApplication.Core` namespace, the compiler option has to be `-main:MyApplication.Core.Program`. If your compilation includes more than one type with a [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method, you can specify which type contains the `Main` method. +Where `Program` is the type that contains the `Main` method. The provided class name must be fully qualified; it must include the full namespace containing the class, followed by the class name. For example, when the `Main` method is located inside the `Program` class in the `MyApplication.Core` namespace, the compiler option has to be `-main:MyApplication.Core.Program`. If your compilation includes more than one type with a [`Main`](../../fundamentals/program-structure/main-command-line.md) method, you can specify which type contains the `Main` method. > [!NOTE] -> This option can't be used for a project that includes [top-level statements](/dotnet/csharp/fundamentals/program-structure/top-level-statements), even if that project contains one or more `Main` methods. +> This option can't be used for a project that includes [top-level statements](../../fundamentals/program-structure/top-level-statements.md), even if that project contains one or more `Main` methods. ## PdbFile diff --git a/docs/csharp/language-reference/compiler-options/output.md b/docs/csharp/language-reference/compiler-options/output.md index 61dc345e40361..0b8e8e7282e94 100644 --- a/docs/csharp/language-reference/compiler-options/output.md +++ b/docs/csharp/language-reference/compiler-options/output.md @@ -116,11 +116,11 @@ If you create an assembly, you can indicate that all or part of your code is CLS ### library -The **library** option causes the compiler to create a dynamic-link library (DLL) rather than an executable file (EXE). The DLL will be created with the *.dll* extension. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. When building a *.dll* file, a [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method isn't required. +The **library** option causes the compiler to create a dynamic-link library (DLL) rather than an executable file (EXE). The DLL will be created with the *.dll* extension. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. When building a *.dll* file, a [`Main`](../../fundamentals/program-structure/main-command-line.md) method isn't required. ### exe -The **exe** option causes the compiler to create an executable (EXE), console application. The executable file will be created with the .exe extension. Use **winexe** to create a Windows program executable. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the entry point ([Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method or top-level statements). One and only one entry point is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) compiler option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. +The **exe** option causes the compiler to create an executable (EXE), console application. The executable file will be created with the .exe extension. Use **winexe** to create a Windows program executable. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the entry point ([Main](../../fundamentals/program-structure/main-command-line.md) method or top-level statements). One and only one entry point is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) compiler option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. ### module @@ -128,16 +128,16 @@ This option causes the compiler to not generate an assembly manifest. By default ### winexe -The **winexe** option causes the compiler to create an executable (EXE), Windows program. The executable file will be created with the .exe extension. A Windows program is one that provides a user interface from either the .NET library or with the Windows APIs. Use **exe** to create a console application. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. One and only one `Main` method is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. +The **winexe** option causes the compiler to create an executable (EXE), Windows program. The executable file will be created with the .exe extension. A Windows program is one that provides a user interface from either the .NET library or with the Windows APIs. Use **exe** to create a console application. Unless otherwise specified with the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](../../fundamentals/program-structure/main-command-line.md) method. One and only one `Main` method is required in the source code files that are compiled into an .exe file. The [**StartupObject**](./advanced.md#mainentrypoint-or-startupobject) option lets you specify which class contains the `Main` method, in cases where your code has more than one class with a `Main` method. ### winmdobj If you use the **winmdobj** option, the compiler creates an intermediate *.winmdobj* file that you can convert to a Windows Runtime binary (*.winmd*) file. The *.winmd* file can then be consumed by JavaScript and C++ programs, in addition to managed language programs. -The **winmdobj** setting signals to the compiler that an intermediate module is required. The *.winmdobj* file can then be fed through the export tool to produce a Windows metadata (*.winmd*) file. The *.winmd* file contains both the code from the original library and the WinMD metadata that is used by JavaScript or C++ and by the Windows Runtime. The output of a file that’s compiled by using the **winmdobj** compiler option is used only as input for the WimMDExp export tool. The *.winmdobj* file itself isn’t referenced directly. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. A [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method isn’t required. +The **winmdobj** setting signals to the compiler that an intermediate module is required. The *.winmdobj* file can then be fed through the export tool to produce a Windows metadata (*.winmd*) file. The *.winmd* file contains both the code from the original library and the WinMD metadata that is used by JavaScript or C++ and by the Windows Runtime. The output of a file that’s compiled by using the **winmdobj** compiler option is used only as input for the WimMDExp export tool. The *.winmdobj* file itself isn’t referenced directly. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the first input file. A [`Main`](../../fundamentals/program-structure/main-command-line.md) method isn’t required. ### appcontainerexe If you use the **appcontainerexe** compiler option, the compiler creates a Windows executable (*.exe*) file that must be run in an app container. This option is equivalent to [-target:winexe](output.md) but is designed for Windows 8.x Store apps. -To require the app to run in an app container, this option sets a bit in the [Portable Executable](/windows/desktop/Debug/pe-format) (PE) file. When that bit is set, an error occurs if the CreateProcess method tries to launch the executable file outside an app container. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. +To require the app to run in an app container, this option sets a bit in the [Portable Executable](/windows/desktop/Debug/pe-format) (PE) file. When that bit is set, an error occurs if the CreateProcess method tries to launch the executable file outside an app container. Unless you use the [**OutputAssembly**](#outputassembly) option, the output file name takes the name of the input file that contains the [`Main`](../../fundamentals/program-structure/main-command-line.md) method. diff --git a/docs/csharp/language-reference/keywords/class.md b/docs/csharp/language-reference/keywords/class.md index a3130e52be571..3b116021662d3 100644 --- a/docs/csharp/language-reference/keywords/class.md +++ b/docs/csharp/language-reference/keywords/class.md @@ -62,9 +62,9 @@ A class can contain declarations of the following members: - [Delegates](../../programming-guide/delegates/index.md) -- [Classes](/dotnet/csharp/fundamentals/types/classes) +- [Classes](../../fundamentals/types/classes.md) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) - [Structure types](../builtin-types/struct.md) diff --git a/docs/csharp/language-reference/keywords/event.md b/docs/csharp/language-reference/keywords/event.md index c8d05aea81b93..fa88382a5e99b 100644 --- a/docs/csharp/language-reference/keywords/event.md +++ b/docs/csharp/language-reference/keywords/event.md @@ -32,13 +32,13 @@ The following keywords apply to events. |Keyword|Description|For more information| |-------------|-----------------|--------------------------| |[static](./static.md)|Makes the event available to callers at any time, even if no instance of the class exists.|[Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md)| -|[virtual](./virtual.md)|Allows derived classes to override the event behavior by using the [override](./override.md) keyword.|[Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance)| +|[virtual](./virtual.md)|Allows derived classes to override the event behavior by using the [override](./override.md) keyword.|[Inheritance](../../fundamentals/object-oriented/inheritance.md)| |[sealed](./sealed.md)|Specifies that for derived classes it is no longer virtual.|| |[abstract](./abstract.md)|The compiler will not generate the `add` and `remove` event accessor blocks and therefore derived classes must provide their own implementation.|| An event may be declared as a static event by using the [static](./static.md) keyword. This makes the event available to callers at any time, even if no instance of the class exists. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md). -An event can be marked as a virtual event by using the [virtual](./virtual.md) keyword. This enables derived classes to override the event behavior by using the [override](./override.md) keyword. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). An event overriding a virtual event can also be [sealed](./sealed.md), which specifies that for derived classes it is no longer virtual. Lastly, an event can be declared [abstract](./abstract.md), which means that the compiler will not generate the `add` and `remove` event accessor blocks. Therefore derived classes must provide their own implementation. +An event can be marked as a virtual event by using the [virtual](./virtual.md) keyword. This enables derived classes to override the event behavior by using the [override](./override.md) keyword. For more information, see [Inheritance](../../fundamentals/object-oriented/inheritance.md). An event overriding a virtual event can also be [sealed](./sealed.md), which specifies that for derived classes it is no longer virtual. Lastly, an event can be declared [abstract](./abstract.md), which means that the compiler will not generate the `add` and `remove` event accessor blocks. Therefore derived classes must provide their own implementation. ## C# language specification diff --git a/docs/csharp/language-reference/keywords/index.md b/docs/csharp/language-reference/keywords/index.md index 889a572a74175..b1cd3b7d8c887 100644 --- a/docs/csharp/language-reference/keywords/index.md +++ b/docs/csharp/language-reference/keywords/index.md @@ -144,7 +144,7 @@ A contextual keyword is used to provide a specific meaning in the code, but it i [orderby](orderby-clause.md) [partial (type)](partial-type.md) [partial (method)](partial-method.md) - [record](/dotnet/csharp/fundamentals/types/records) + [record](../../fundamentals/types/records.md) [remove](remove.md) [select](select-clause.md) :::column-end::: diff --git a/docs/csharp/language-reference/keywords/interface.md b/docs/csharp/language-reference/keywords/interface.md index 60eed951798f9..1c3b2f2d3fb5b 100644 --- a/docs/csharp/language-reference/keywords/interface.md +++ b/docs/csharp/language-reference/keywords/interface.md @@ -14,7 +14,7 @@ An interface defines a contract. Any [`class`](class.md) or [`struct`](../builti In the following example, class `ImplementationClass` must implement a method named `SampleMethod` that has no parameters and returns `void`. -For more information and examples, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). +For more information and examples, see [Interfaces](../../fundamentals/types/interfaces.md). ## Example @@ -70,6 +70,6 @@ For more information, see the [Interfaces](~/_csharplang/spec/interfaces.md) sec - [C# Programming Guide](../../programming-guide/index.md) - [C# Keywords](index.md) - [Reference Types](reference-types.md) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) - [Using Properties](../../programming-guide/classes-and-structs/using-properties.md) - [Using Indexers](../../programming-guide/indexers/using-indexers.md) diff --git a/docs/csharp/language-reference/keywords/namespace.md b/docs/csharp/language-reference/keywords/namespace.md index 157d6cd8e45af..242a6af9bc026 100644 --- a/docs/csharp/language-reference/keywords/namespace.md +++ b/docs/csharp/language-reference/keywords/namespace.md @@ -57,4 +57,4 @@ For more information, see the [Namespaces](~/_csharplang/spec/namespaces.md) sec - [using](using-directive.md) - [using static](using-static.md) - [Namespace alias qualifier `::`](../operators/namespace-alias-qualifier.md) -- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) +- [Namespaces](../../fundamentals/types/namespaces.md) diff --git a/docs/csharp/language-reference/keywords/new-constraint.md b/docs/csharp/language-reference/keywords/new-constraint.md index 347fb95983312..173bca7246187 100644 --- a/docs/csharp/language-reference/keywords/new-constraint.md +++ b/docs/csharp/language-reference/keywords/new-constraint.md @@ -31,4 +31,4 @@ For more information, see the [Type parameter constraints](~/_csharplang/spec/cl - [C# Reference](../index.md) - [C# Programming Guide](../../programming-guide/index.md) - [C# Keywords](index.md) -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../fundamentals/types/generics.md) diff --git a/docs/csharp/language-reference/keywords/override.md b/docs/csharp/language-reference/keywords/override.md index 62a6eced07a53..2b6ea324dec41 100644 --- a/docs/csharp/language-reference/keywords/override.md +++ b/docs/csharp/language-reference/keywords/override.md @@ -27,7 +27,7 @@ You cannot use the `new`, `static`, or `virtual` modifiers to modify an `overrid An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property. Beginning with C# 9.0, read-only overriding properties support covariant return types. The overridden property must be `virtual`, `abstract`, or `override`. -For more information about how to use the `override` keyword, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md) and [Knowing when to use Override and New Keywords](../../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). For information about inheritance, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). +For more information about how to use the `override` keyword, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md) and [Knowing when to use Override and New Keywords](../../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). For information about inheritance, see [Inheritance](../../fundamentals/object-oriented/inheritance.md). ## Example @@ -44,10 +44,10 @@ For more information about covariant return types, see the [feature proposal not ## See also - [C# reference](../index.md) -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Inheritance](../../fundamentals/object-oriented/inheritance.md) - [C# keywords](index.md) - [Modifiers](index.md) - [abstract](abstract.md) - [virtual](virtual.md) - [new (modifier)](new-modifier.md) -- [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism) +- [Polymorphism](../../fundamentals/object-oriented/polymorphism.md) diff --git a/docs/csharp/language-reference/keywords/partial-type.md b/docs/csharp/language-reference/keywords/partial-type.md index 4c1ded76d571a..49fb91e46046f 100644 --- a/docs/csharp/language-reference/keywords/partial-type.md +++ b/docs/csharp/language-reference/keywords/partial-type.md @@ -34,4 +34,4 @@ Splitting a class, struct or interface type over several files can be useful whe - [C# Reference](../index.md) - [C# Programming Guide](../../programming-guide/index.md) - [Modifiers](index.md) -- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) +- [Introduction to Generics](../../fundamentals/types/generics.md) diff --git a/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md b/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md index 894d93ea5fa61..0d5947a3101e9 100644 --- a/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md +++ b/docs/csharp/language-reference/keywords/restrictions-on-using-accessibility-levels.md @@ -19,8 +19,8 @@ The following table summarizes the restrictions on declared accessibility levels |Context|Remarks| |-------------|-------------| -|[Classes](/dotnet/csharp/fundamentals/types/classes)|The direct base class of a class type must be at least as accessible as the class type itself.| -|[Interfaces](/dotnet/csharp/fundamentals/types/interfaces)|The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.| +|[Classes](../../fundamentals/types/classes.md)|The direct base class of a class type must be at least as accessible as the class type itself.| +|[Interfaces](../../fundamentals/types/interfaces.md)|The explicit base interfaces of an interface type must be at least as accessible as the interface type itself.| |[Delegates](../../programming-guide/delegates/index.md)|The return type and parameter types of a delegate type must be at least as accessible as the delegate type itself.| |[Constants](../../programming-guide/classes-and-structs/constants.md)|The type of a constant must be at least as accessible as the constant itself.| |[Fields](../../programming-guide/classes-and-structs/fields.md)|The type of a field must be at least as accessible as the field itself.| diff --git a/docs/csharp/language-reference/keywords/sealed.md b/docs/csharp/language-reference/keywords/sealed.md index 7bd7a48ec1d75..9a414fe6f8b74 100644 --- a/docs/csharp/language-reference/keywords/sealed.md +++ b/docs/csharp/language-reference/keywords/sealed.md @@ -34,7 +34,7 @@ When applied to a method or property, the `sealed` modifier must always be used Because structs are implicitly sealed, they cannot be inherited. -For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). +For more information, see [Inheritance](../../fundamentals/object-oriented/inheritance.md). For more examples, see [Abstract and Sealed Classes and Class Members](../../programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md). diff --git a/docs/csharp/language-reference/keywords/select-clause.md b/docs/csharp/language-reference/keywords/select-clause.md index ed21f5413bc79..a4970bb467f5b 100644 --- a/docs/csharp/language-reference/keywords/select-clause.md +++ b/docs/csharp/language-reference/keywords/select-clause.md @@ -38,6 +38,6 @@ At compile time, the `select` clause is translated to a method call to the [!NOTE] > For an introduction to asynchronous programming, see [Asynchronous programming with async and await](../../programming-guide/concepts/async/index.md). Asynchronous programming with `async` and `await` follows the [task-based asynchronous pattern](../../../standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap.md). @@ -40,7 +40,7 @@ You use the `await using` statement to work with an asynchronously disposable ob ## await operator in the Main method -Beginning with C# 7.1, the [`Main` method](/dotnet/csharp/fundamentals/program-structure/main-command-line), which is the application entry point, can return `Task` or `Task`, enabling it to be async so you can use the `await` operator in its body. In earlier C# versions, to ensure that the `Main` method waits for the completion of an asynchronous operation, you can retrieve the value of the property of the instance that is returned by the corresponding async method. For asynchronous operations that don't produce a value, you can call the method. For information about how to select the language version, see [C# language versioning](../configure-language-version.md). +Beginning with C# 7.1, the [`Main` method](../../fundamentals/program-structure/main-command-line.md), which is the application entry point, can return `Task` or `Task`, enabling it to be async so you can use the `await` operator in its body. In earlier C# versions, to ensure that the `Main` method waits for the completion of an asynchronous operation, you can retrieve the value of the property of the instance that is returned by the corresponding async method. For asynchronous operations that don't produce a value, you can call the method. For information about how to select the language version, see [C# language versioning](../configure-language-version.md). ## C# language specification diff --git a/docs/csharp/language-reference/operators/delegate-operator.md b/docs/csharp/language-reference/operators/delegate-operator.md index c3f5195bc9b74..6bee929a2c24c 100644 --- a/docs/csharp/language-reference/operators/delegate-operator.md +++ b/docs/csharp/language-reference/operators/delegate-operator.md @@ -25,7 +25,7 @@ When you use the `delegate` operator, you might omit the parameter list. If you That's the only functionality of anonymous methods that is not supported by lambda expressions. In all other cases, a lambda expression is a preferred way to write inline code. -Beginning with C# 9.0, you can use [discards](/dotnet/csharp/fundamentals/functional/discards) to specify two or more input parameters of an anonymous method that aren't used by the method: +Beginning with C# 9.0, you can use [discards](../../fundamentals/functional/discards.md) to specify two or more input parameters of an anonymous method that aren't used by the method: :::code language="csharp" source="snippets/shared/DelegateOperator.cs" id="SnippetDiscards" ::: diff --git a/docs/csharp/language-reference/operators/is.md b/docs/csharp/language-reference/operators/is.md index 243377a39c64d..faebce896894a 100644 --- a/docs/csharp/language-reference/operators/is.md +++ b/docs/csharp/language-reference/operators/is.md @@ -51,5 +51,5 @@ For more information, see [The is operator](~/_csharplang/spec/expressions.md#th - [C# reference](../index.md) - [C# operators and expressions](index.md) - [Patterns](patterns.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md) - [Type-testing and cast operators](../operators/type-testing-and-cast.md) diff --git a/docs/csharp/language-reference/operators/lambda-expressions.md b/docs/csharp/language-reference/operators/lambda-expressions.md index 3a7e2bb5e3332..08fc0d1dad09f 100644 --- a/docs/csharp/language-reference/operators/lambda-expressions.md +++ b/docs/csharp/language-reference/operators/lambda-expressions.md @@ -86,7 +86,7 @@ Sometimes the compiler can't infer the types of input parameters. You can specif Input parameter types must be all explicit or all implicit; otherwise, a [CS0748](../../misc/cs0748.md) compiler error occurs. -Beginning with C# 9.0, you can use [discards](/dotnet/csharp/fundamentals/functional/discards) to specify two or more input parameters of a lambda expression that aren't used in the expression: +Beginning with C# 9.0, you can use [discards](../../fundamentals/functional/discards.md) to specify two or more input parameters of a lambda expression that aren't used in the expression: :::code language="csharp" source="snippets/lambda-expressions/GeneralExamples.cs" id="SnippetDiscards"::: diff --git a/docs/csharp/language-reference/operators/new-operator.md b/docs/csharp/language-reference/operators/new-operator.md index 7d4f0b16259b6..15b4f2e2103b8 100644 --- a/docs/csharp/language-reference/operators/new-operator.md +++ b/docs/csharp/language-reference/operators/new-operator.md @@ -46,7 +46,7 @@ For more information about arrays, see [Arrays](../../programming-guide/arrays/i ## Instantiation of anonymous types -To create an instance of an [anonymous type](/dotnet/csharp/fundamentals/types/anonymous-types), use the `new` operator and object initializer syntax: +To create an instance of an [anonymous type](../../fundamentals/types/anonymous-types.md), use the `new` operator and object initializer syntax: [!code-csharp-interactive[anonymous type](snippets/shared/NewOperator.cs#AnonymousType)] diff --git a/docs/csharp/language-reference/operators/patterns.md b/docs/csharp/language-reference/operators/patterns.md index e2edeb690c7ff..95c3b7934579d 100644 --- a/docs/csharp/language-reference/operators/patterns.md +++ b/docs/csharp/language-reference/operators/patterns.md @@ -34,7 +34,7 @@ In those constructs, you can match an input expression against any of the follow [Logical](#logical-patterns), [property](#property-pattern), and [positional](#positional-pattern) patterns are *recursive* patterns. That is, they can contain *nested* patterns. -For the example of how to use those patterns to build a data-driven algorithm, see [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching). +For the example of how to use those patterns to build a data-driven algorithm, see [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md). ## Declaration and type patterns @@ -168,7 +168,7 @@ Beginning with C# 8.0, you use a *positional pattern* to deconstruct an expressi :::code language="csharp" source="snippets/patterns/PositionalPattern.cs" id="BasicExample"::: -At the preceding example, the type of an expression contains the [Deconstruct](/dotnet/csharp/fundamentals/functional/deconstruct) method, which is used to deconstruct an expression result. You can also match expressions of [tuple types](../builtin-types/value-tuples.md) against positional patterns. In that way, you can match multiple inputs against various patterns, as the following example shows: +At the preceding example, the type of an expression contains the [Deconstruct](../../fundamentals/functional/deconstruct.md) method, which is used to deconstruct an expression result. You can also match expressions of [tuple types](../builtin-types/value-tuples.md) against positional patterns. In that way, you can match multiple inputs against various patterns, as the following example shows: :::code language="csharp" source="snippets/patterns/PositionalPattern.cs" id="MatchTuple"::: @@ -244,4 +244,4 @@ For more information, see the following feature proposal notes: - [C# reference](../index.md) - [C# operators and expressions](index.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md) diff --git a/docs/csharp/language-reference/operators/switch-expression.md b/docs/csharp/language-reference/operators/switch-expression.md index 003467dbb382f..4afbc18c42780 100644 --- a/docs/csharp/language-reference/operators/switch-expression.md +++ b/docs/csharp/language-reference/operators/switch-expression.md @@ -56,5 +56,5 @@ For more information, see the [`switch` expression](~/_csharplang/proposals/csha - [C# reference](../index.md) - [C# operators and expressions](index.md) - [Patterns](patterns.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md) - [`switch` statement](../keywords/switch.md) diff --git a/docs/csharp/language-reference/operators/type-testing-and-cast.md b/docs/csharp/language-reference/operators/type-testing-and-cast.md index 01c80fc2a0a21..d17ff682fdfc2 100644 --- a/docs/csharp/language-reference/operators/type-testing-and-cast.md +++ b/docs/csharp/language-reference/operators/type-testing-and-cast.md @@ -152,5 +152,5 @@ For more information, see the following sections of the [C# language specificati - [C# reference](../index.md) - [C# operators and expressions](index.md) -- [How to safely cast by using pattern matching and the is and as operators](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) +- [How to safely cast by using pattern matching and the is and as operators](../../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) - [Generics in .NET](../../../standard/generics/index.md) diff --git a/docs/csharp/linq/group-query-results.md b/docs/csharp/linq/group-query-results.md index 3d9fb4d369c24..ad0e639c270b0 100644 --- a/docs/csharp/linq/group-query-results.md +++ b/docs/csharp/linq/group-query-results.md @@ -74,7 +74,7 @@ Paste the following method into the `StudentClass` class. Change the calling sta - - [Language Integrated Query (LINQ)](index.md) - [group clause](../language-reference/keywords/group-clause.md) -- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types](../fundamentals/types/anonymous-types.md) - [Perform a Subquery on a Grouping Operation](perform-a-subquery-on-a-grouping-operation.md) - [Create a Nested Group](create-a-nested-group.md) - [Grouping Data](../programming-guide/concepts/linq/grouping-data.md) diff --git a/docs/csharp/linq/perform-grouped-joins.md b/docs/csharp/linq/perform-grouped-joins.md index f0f03eb4b00bd..d419970d4a074 100644 --- a/docs/csharp/linq/perform-grouped-joins.md +++ b/docs/csharp/linq/perform-grouped-joins.md @@ -36,4 +36,4 @@ Group joins are ideal for creating XML by using LINQ to XML. The following examp - - [Perform inner joins](perform-inner-joins.md) - [Perform left outer joins](perform-left-outer-joins.md) -- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous types](../fundamentals/types/anonymous-types.md) diff --git a/docs/csharp/linq/perform-inner-joins.md b/docs/csharp/linq/perform-inner-joins.md index c30dfdf2809a3..713fcd7b50f57 100644 --- a/docs/csharp/linq/perform-inner-joins.md +++ b/docs/csharp/linq/perform-inner-joins.md @@ -64,4 +64,4 @@ The result of `query1` is equivalent to the result set that would have been obta - - [Perform grouped joins](perform-grouped-joins.md) - [Perform left outer joins](perform-left-outer-joins.md) -- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous types](../fundamentals/types/anonymous-types.md) diff --git a/docs/csharp/linq/perform-left-outer-joins.md b/docs/csharp/linq/perform-left-outer-joins.md index efe3ae9939c95..a5a4fb5160a97 100644 --- a/docs/csharp/linq/perform-left-outer-joins.md +++ b/docs/csharp/linq/perform-left-outer-joins.md @@ -27,4 +27,4 @@ The second step is to include each element of the first (left) collection in the - - [Perform inner joins](perform-inner-joins.md) - [Perform grouped joins](perform-grouped-joins.md) -- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous types](../fundamentals/types/anonymous-types.md) diff --git a/docs/csharp/methods.md b/docs/csharp/methods.md index fb18b7b1315f6..f9b7f4a79b579 100644 --- a/docs/csharp/methods.md +++ b/docs/csharp/methods.md @@ -282,7 +282,7 @@ For more information, see [Iterators](programming-guide/concepts/iterators.md). - [Access Modifiers](language-reference/keywords/access-modifiers.md) - [Static Classes and Static Class Members](programming-guide/classes-and-structs/static-classes-and-static-class-members.md) -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Inheritance](fundamentals/object-oriented/inheritance.md) - [Abstract and Sealed Classes and Class Members](programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md) - [params](language-reference/keywords/params.md) - [out](language-reference/keywords/out-parameter-modifier.md) diff --git a/docs/csharp/misc/cs0017.md b/docs/csharp/misc/cs0017.md index cfa05d0bd09a1..ac8b11519b2b0 100644 --- a/docs/csharp/misc/cs0017.md +++ b/docs/csharp/misc/cs0017.md @@ -12,7 +12,7 @@ ms.assetid: 5e2a3eb3-6f6e-485d-8293-ceabea4d6905 Program 'output file name' has more than one entry point defined. Compile with /main to specify the type that contains the entry point. - A program can only have one [Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. + A program can only have one [Main](../fundamentals/program-structure/main-command-line.md) method. To resolve this error, you can either delete all Main methods in your code, except one, or you can use the [**StartupObject**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option to specify which Main method you want to use. diff --git a/docs/csharp/misc/cs0026.md b/docs/csharp/misc/cs0026.md index 71c5aea074fe1..b74304c51318d 100644 --- a/docs/csharp/misc/cs0026.md +++ b/docs/csharp/misc/cs0026.md @@ -12,7 +12,7 @@ ms.assetid: 8767fbc1-8ba7-4e88-a9f9-7e620411882b Keyword 'this' is not valid in a static property, static method, or static field initializer - The [this](../language-reference/keywords/this.md) keyword refers to an object, which is an instance of a type. Since static methods are independent of any instance of the containing class, the "this" keyword is meaningless and is therefore not allowed. For more information, see [Static Classes and Static Class Members](../programming-guide/classes-and-structs/static-classes-and-static-class-members.md) and [Objects](/dotnet/csharp/fundamentals/object-oriented/objects). + The [this](../language-reference/keywords/this.md) keyword refers to an object, which is an instance of a type. Since static methods are independent of any instance of the containing class, the "this" keyword is meaningless and is therefore not allowed. For more information, see [Static Classes and Static Class Members](../programming-guide/classes-and-structs/static-classes-and-static-class-members.md) and [Objects](../fundamentals/object-oriented/objects.md). ## Example diff --git a/docs/csharp/misc/cs0028.md b/docs/csharp/misc/cs0028.md index dd0e6813b46b9..a520164c9265d 100644 --- a/docs/csharp/misc/cs0028.md +++ b/docs/csharp/misc/cs0028.md @@ -12,7 +12,7 @@ ms.assetid: 47df919f-01fa-45ae-a4b7-912445e679e2 'function declaration' has the wrong signature to be an entry point - The method declaration for `Main` was invalid: it was declared with an invalid signature. `Main` must be declared as static and it must return either [int](../language-reference/builtin-types/integral-numeric-types.md) or [void](../language-reference/builtin-types/void.md). For more information, see [Main() and Command-Line Arguments](/dotnet/csharp/fundamentals/program-structure/main-command-line). + The method declaration for `Main` was invalid: it was declared with an invalid signature. `Main` must be declared as static and it must return either [int](../language-reference/builtin-types/integral-numeric-types.md) or [void](../language-reference/builtin-types/void.md). For more information, see [Main() and Command-Line Arguments](../fundamentals/program-structure/main-command-line.md). The following sample generates CS0028: diff --git a/docs/csharp/misc/cs0061.md b/docs/csharp/misc/cs0061.md index 399d25a3bfb30..eb91fa3a49c04 100644 --- a/docs/csharp/misc/cs0061.md +++ b/docs/csharp/misc/cs0061.md @@ -14,7 +14,7 @@ Inconsistent accessibility: base interface 'interface 1' is less accessible than A [public](../language-reference/keywords/public.md) construct must return a publicly accessible object. - Interface accessibility cannot be narrowed in a derived interface. For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) and [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md). + Interface accessibility cannot be narrowed in a derived interface. For more information, see [Interfaces](../fundamentals/types/interfaces.md) and [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md). The following sample generates CS0061. diff --git a/docs/csharp/misc/cs0068.md b/docs/csharp/misc/cs0068.md index df102c507d635..f9f3b41502718 100644 --- a/docs/csharp/misc/cs0068.md +++ b/docs/csharp/misc/cs0068.md @@ -12,7 +12,7 @@ ms.assetid: 9c9ac915-e12f-4ceb-8eb0-806790f11a09 'event': event in interface cannot have initializer - An event in an interface cannot have an initializer. For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). + An event in an interface cannot have an initializer. For more information, see [Interfaces](../fundamentals/types/interfaces.md). The following sample generates CS0068: diff --git a/docs/csharp/misc/cs0069.md b/docs/csharp/misc/cs0069.md index b5a519be8307b..083ee0e0106f3 100644 --- a/docs/csharp/misc/cs0069.md +++ b/docs/csharp/misc/cs0069.md @@ -12,7 +12,7 @@ ms.assetid: a1b32906-7773-47c6-8515-162a201a9be5 An event in an interface cannot have add or remove accessors - You cannot define an event's accessor functions in an [interface](../language-reference/keywords/interface.md). For more information, see [Events](../programming-guide/events/index.md) and [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). + You cannot define an event's accessor functions in an [interface](../language-reference/keywords/interface.md). For more information, see [Events](../programming-guide/events/index.md) and [Interfaces](../fundamentals/types/interfaces.md). The following sample generates CS0069: diff --git a/docs/csharp/misc/cs0080.md b/docs/csharp/misc/cs0080.md index 0c2a204450602..9a589b5453da8 100644 --- a/docs/csharp/misc/cs0080.md +++ b/docs/csharp/misc/cs0080.md @@ -12,7 +12,7 @@ ms.assetid: 99035371-37d1-48b2-a8b9-e8a1ebd04f0f Constraints are not allowed on non-generic declarations - The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](/dotnet/csharp/fundamentals/types/generics). + The syntax found may only be used in a generic declaration to apply constraints to the type parameter. For more information, see [Generics](../fundamentals/types/generics.md). The following sample generates CS0080 because MyClass is not a generic class and Foo is not a generic method. diff --git a/docs/csharp/misc/cs0081.md b/docs/csharp/misc/cs0081.md index 72c52db2f2aec..4e6e0a95b6da5 100644 --- a/docs/csharp/misc/cs0081.md +++ b/docs/csharp/misc/cs0081.md @@ -32,4 +32,4 @@ class MyClass ## See also -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../fundamentals/types/generics.md) diff --git a/docs/csharp/misc/cs0101.md b/docs/csharp/misc/cs0101.md index b55d94f7476d7..a6785cc54e80c 100644 --- a/docs/csharp/misc/cs0101.md +++ b/docs/csharp/misc/cs0101.md @@ -12,7 +12,7 @@ ms.assetid: edb5246b-c16b-4845-bb2d-0ef769d014c7 The namespace 'namespace' already contains a definition for 'type' - A [namespace](../language-reference/keywords/namespace.md) has duplicate identifiers. Rename or delete one of the duplicate identifiers. For more information, see [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) + A [namespace](../language-reference/keywords/namespace.md) has duplicate identifiers. Rename or delete one of the duplicate identifiers. For more information, see [Namespaces](../fundamentals/types/namespaces.md) The following sample generates CS0101: diff --git a/docs/csharp/misc/cs0138.md b/docs/csharp/misc/cs0138.md index 61c7bf9ae1b1f..2d7b51ff51cfd 100644 --- a/docs/csharp/misc/cs0138.md +++ b/docs/csharp/misc/cs0138.md @@ -12,7 +12,7 @@ ms.assetid: 970545f8-5ee5-428e-921a-3aa29f68d16d A using namespace directive can only be applied to namespaces; 'type' is a type not a namespace - A [using](../language-reference/keywords/using.md) directive can only take the name of a namespace as a parameter. For more information, see [Namespaces](/dotnet/csharp/fundamentals/types/namespaces). + A [using](../language-reference/keywords/using.md) directive can only take the name of a namespace as a parameter. For more information, see [Namespaces](../fundamentals/types/namespaces.md). The following sample generates CS0138: diff --git a/docs/csharp/misc/cs0144.md b/docs/csharp/misc/cs0144.md index c469aad4c3886..dd172fa11fa55 100644 --- a/docs/csharp/misc/cs0144.md +++ b/docs/csharp/misc/cs0144.md @@ -12,7 +12,7 @@ ms.assetid: 3904cab1-05bd-44ec-81d0-e36c5656f742 Cannot create an instance of the abstract class or interface 'interface' - You cannot create an instance of an [abstract](../language-reference/keywords/abstract.md) class or an [interface](../language-reference/keywords/interface.md). For more information, see [Interfaces](/dotnet/csharp/fundamentals/types/interfaces). + You cannot create an instance of an [abstract](../language-reference/keywords/abstract.md) class or an [interface](../language-reference/keywords/interface.md). For more information, see [Interfaces](../fundamentals/types/interfaces.md). The following sample generates CS0144: diff --git a/docs/csharp/misc/cs0146.md b/docs/csharp/misc/cs0146.md index fd743c8df79f4..44ff614052ce7 100644 --- a/docs/csharp/misc/cs0146.md +++ b/docs/csharp/misc/cs0146.md @@ -12,7 +12,7 @@ ms.assetid: 2be796e5-da2c-4939-af12-3145cd1828c8 Circular base class dependency involving 'class1' and 'class2' - The inheritance list for a class includes a direct or indirect reference to itself. A class cannot inherit from itself. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). + The inheritance list for a class includes a direct or indirect reference to itself. A class cannot inherit from itself. For more information, see [Inheritance](../fundamentals/object-oriented/inheritance.md). The following sample generates CS0146: diff --git a/docs/csharp/misc/cs0238.md b/docs/csharp/misc/cs0238.md index 889fc06f22ff7..5c9d9ececde80 100644 --- a/docs/csharp/misc/cs0238.md +++ b/docs/csharp/misc/cs0238.md @@ -12,7 +12,7 @@ ms.assetid: 9b50c6e2-2c3f-431d-bdb7-557b0ec51626 'member' cannot be sealed because it is not an override - [sealed](../language-reference/keywords/sealed.md) was used on a member that was not also marked with [override](../language-reference/keywords/override.md). For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). + [sealed](../language-reference/keywords/sealed.md) was used on a member that was not also marked with [override](../language-reference/keywords/override.md). For more information, see [Inheritance](../fundamentals/object-oriented/inheritance.md). The following sample generates CS0238: diff --git a/docs/csharp/misc/cs0666.md b/docs/csharp/misc/cs0666.md index 24b6ff6113da0..4b92f386392ee 100644 --- a/docs/csharp/misc/cs0666.md +++ b/docs/csharp/misc/cs0666.md @@ -12,7 +12,7 @@ ms.assetid: 44ad4574-b4a2-487b-8d05-0116762231ab 'member' : new protected member declared in struct - A [struct](../language-reference/builtin-types/struct.md) cannot be [abstract](../language-reference/keywords/abstract.md) and is always implicitly [sealed](../language-reference/keywords/sealed.md). Because structs do not support inheritance, the concept of a [protected](../language-reference/keywords/protected.md) member in a struct makes no sense. For more information, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). + A [struct](../language-reference/builtin-types/struct.md) cannot be [abstract](../language-reference/keywords/abstract.md) and is always implicitly [sealed](../language-reference/keywords/sealed.md). Because structs do not support inheritance, the concept of a [protected](../language-reference/keywords/protected.md) member in a struct makes no sense. For more information, see [Inheritance](../fundamentals/object-oriented/inheritance.md). ## Example diff --git a/docs/csharp/misc/cs0736.md b/docs/csharp/misc/cs0736.md index 654157ea68db6..60b8c2adf84cc 100644 --- a/docs/csharp/misc/cs0736.md +++ b/docs/csharp/misc/cs0736.md @@ -48,4 +48,4 @@ namespace CS0736 ## See also -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/cs0737.md b/docs/csharp/misc/cs0737.md index 255de4b1a3719..e2354d4ed4cab 100644 --- a/docs/csharp/misc/cs0737.md +++ b/docs/csharp/misc/cs0737.md @@ -51,4 +51,4 @@ public class Test ## See also -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/cs0738.md b/docs/csharp/misc/cs0738.md index 2b1a128690c62..2083f426ac71b 100644 --- a/docs/csharp/misc/cs0738.md +++ b/docs/csharp/misc/cs0738.md @@ -39,4 +39,4 @@ public class Test: ITest ## See also -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../fundamentals/types/interfaces.md) diff --git a/docs/csharp/misc/cs0746.md b/docs/csharp/misc/cs0746.md index 7ebcbf66fb3a7..7c53d31a1f4fe 100644 --- a/docs/csharp/misc/cs0746.md +++ b/docs/csharp/misc/cs0746.md @@ -43,4 +43,4 @@ public class C ## See also -- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types](../fundamentals/types/anonymous-types.md) diff --git a/docs/csharp/misc/cs0833.md b/docs/csharp/misc/cs0833.md index 3008a4994fd8e..04b5cc68decb9 100644 --- a/docs/csharp/misc/cs0833.md +++ b/docs/csharp/misc/cs0833.md @@ -38,4 +38,4 @@ public class C ## See also -- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types](../fundamentals/types/anonymous-types.md) diff --git a/docs/csharp/misc/cs1527.md b/docs/csharp/misc/cs1527.md index 45311dc43ff10..0c4194ed0e4ab 100644 --- a/docs/csharp/misc/cs1527.md +++ b/docs/csharp/misc/cs1527.md @@ -39,7 +39,7 @@ private struct S1 {} ## See also -- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) +- [Namespaces](../fundamentals/types/namespaces.md) - [:: Operator](../language-reference/operators/namespace-alias-qualifier.md) - [Accessibility Domain](../language-reference/keywords/accessibility-domain.md) - [Access Modifiers](../programming-guide/classes-and-structs/access-modifiers.md) diff --git a/docs/csharp/misc/cs1558.md b/docs/csharp/misc/cs1558.md index 35423bda10737..0fa59b5f6175b 100644 --- a/docs/csharp/misc/cs1558.md +++ b/docs/csharp/misc/cs1558.md @@ -12,7 +12,7 @@ ms.assetid: ee603d66-007e-4782-9285-7ff031975f0f 'class' does not have a suitable static Main method - The [**MainEntryPoint**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option specified a class in which to look for a **Main** method. However, the [Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method was not defined correctly. + The [**MainEntryPoint**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option specified a class in which to look for a **Main** method. However, the [Main](../fundamentals/program-structure/main-command-line.md) method was not defined correctly. The following example generates CS1558 because of invalid return type. diff --git a/docs/csharp/misc/cs1559.md b/docs/csharp/misc/cs1559.md index 1367451406264..740923871cb21 100644 --- a/docs/csharp/misc/cs1559.md +++ b/docs/csharp/misc/cs1559.md @@ -12,4 +12,4 @@ ms.assetid: 08e6c183-4e92-44a5-99f1-4d1eef3d0a67 Cannot use 'object' for Main method because it is imported - An invalid class was specified to the [**StartupObject**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option; the class cannot be used as a location for the [Main](/dotnet/csharp/fundamentals/program-structure/main-command-line) method. + An invalid class was specified to the [**StartupObject**](../language-reference/compiler-options/advanced.md#mainentrypoint-or-startupobject) compiler option; the class cannot be used as a location for the [Main](../fundamentals/program-structure/main-command-line.md) method. diff --git a/docs/csharp/misc/cs5001.md b/docs/csharp/misc/cs5001.md index 52345490625f9..470ee6f48becc 100644 --- a/docs/csharp/misc/cs5001.md +++ b/docs/csharp/misc/cs5001.md @@ -12,7 +12,7 @@ ms.assetid: e1e26e75-84e0-47c7-be8a-3c4fd0d6f497 Program does not contain a static 'Main' method suitable for an entry point -This error occurs when no static `Main` method with a correct signature is found in the code that produces an executable file. It also occurs if the entry point function, `Main`, is defined with the wrong case, such as lower-case `main`. For information about the rules that apply to the `Main` method, see [Main() and Command-Line Arguments](/dotnet/csharp/fundamentals/program-structure/main-command-line). +This error occurs when no static `Main` method with a correct signature is found in the code that produces an executable file. It also occurs if the entry point function, `Main`, is defined with the wrong case, such as lower-case `main`. For information about the rules that apply to the `Main` method, see [Main() and Command-Line Arguments](../fundamentals/program-structure/main-command-line.md). If the `Main` method has an `async` modifier, make sure that the [selected C# language version](../language-reference/configure-language-version.md) is 7.1 or higher. diff --git a/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md b/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md index e1fcbbe64a2e4..3a676a1157273 100644 --- a/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md +++ b/docs/csharp/programming-guide/arrays/implicitly-typed-arrays.md @@ -32,7 +32,7 @@ When you create an anonymous type that contains an array, the array must be impl - [C# Programming Guide](../index.md) - [Implicitly Typed Local Variables](../classes-and-structs/implicitly-typed-local-variables.md) - [Arrays](./index.md) -- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types](../../fundamentals/types/anonymous-types.md) - [Object and Collection Initializers](../classes-and-structs/object-and-collection-initializers.md) - [var](../../language-reference/keywords/var.md) - [LINQ in C#](../../linq/index.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md b/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md index 6f32db75a9e01..2732ddcaae4a3 100644 --- a/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md +++ b/docs/csharp/programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md @@ -49,7 +49,7 @@ The [abstract](../../language-reference/keywords/abstract.md) keyword enables yo - [C# Programming Guide](../index.md) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Inheritance](../../fundamentals/object-oriented/inheritance.md) - [Methods](./methods.md) - [Fields](./fields.md) - [How to define abstract properties](./how-to-define-abstract-properties.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md b/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md index 55a4b22e65be2..780d122c51b80 100644 --- a/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md +++ b/docs/csharp/programming-guide/classes-and-structs/access-modifiers.md @@ -66,7 +66,7 @@ Delegates behave like classes and structs. By default, they have `internal` acce - [C# Programming Guide](../index.md) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) - [private](../../language-reference/keywords/private.md) - [public](../../language-reference/keywords/public.md) - [internal](../../language-reference/keywords/internal.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/extension-methods.md b/docs/csharp/programming-guide/classes-and-structs/extension-methods.md index 869598761c6f7..3f8ec879c8a76 100644 --- a/docs/csharp/programming-guide/classes-and-structs/extension-methods.md +++ b/docs/csharp/programming-guide/classes-and-structs/extension-methods.md @@ -101,7 +101,7 @@ Extending predefined types can be difficult with `struct` types because they're While it's still considered preferable to add functionality by modifying an object's code or deriving a new type whenever it's reasonable and possible to do so, extension methods have become a crucial option for creating reusable functionality throughout the .NET ecosystem. For those occasions when the original source isn't under your control, when a derived object is inappropriate or impossible, or when the functionality shouldn't be exposed beyond its applicable scope, Extension methods are an excellent choice. -For more information on derived types, see [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance). +For more information on derived types, see [Inheritance](../../fundamentals/object-oriented/inheritance.md). When using an extension method to extend a type whose source code you aren't in control of, you run the risk that a change in the implementation of the type will cause your extension method to break. diff --git a/docs/csharp/programming-guide/classes-and-structs/fields.md b/docs/csharp/programming-guide/classes-and-structs/fields.md index 242e882da9cdb..df45cd56dcadb 100644 --- a/docs/csharp/programming-guide/classes-and-structs/fields.md +++ b/docs/csharp/programming-guide/classes-and-structs/fields.md @@ -48,6 +48,6 @@ A field can be declared [readonly](../../language-reference/keywords/readonly.md - [C# Programming Guide](../index.md) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Using Constructors](./using-constructors.md) -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Inheritance](../../fundamentals/object-oriented/inheritance.md) - [Access Modifiers](./access-modifiers.md) - [Abstract and Sealed Classes and Class Members](./abstract-and-sealed-classes-and-class-members.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md b/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md index 83fbde0b8973f..4e0b3c2e6cc67 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-know-the-difference-passing-a-struct-and-passing-a-class-to-a-method.md @@ -27,6 +27,6 @@ The following example demonstrates how passing a [struct](../../language-referen ## See also - [C# Programming Guide](../index.md) -- [Classes](/dotnet/csharp/fundamentals/types/classes) +- [Classes](../../fundamentals/types/classes.md) - [Structure types](../../language-reference/builtin-types/struct.md) - [Passing Parameters](./passing-parameters.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md b/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md index 513ce06f93d93..cc61ac7a8408b 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-return-subsets-of-element-properties-in-a-query.md @@ -47,5 +47,5 @@ To run this code, copy and paste the class into a C# console application with a ## See also - [C# Programming Guide](../index.md) -- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types](../../fundamentals/types/anonymous-types.md) - [LINQ in C#](../../linq/index.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md b/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md index e478e7aef0f36..d301b13667be0 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md @@ -16,7 +16,7 @@ You can use implicitly typed local variables whenever you want the compiler to d ## Example - The following example shows a common scenario in which the `var` keyword is required: a query expression that produces a sequence of anonymous types. In this scenario, both the query variable and the iteration variable in the `foreach` statement must be implicitly typed by using `var` because you do not have access to a type name for the anonymous type. For more information about anonymous types, see [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types). + The following example shows a common scenario in which the `var` keyword is required: a query expression that produces a sequence of anonymous types. In this scenario, both the query variable and the iteration variable in the `foreach` statement must be implicitly typed by using `var` because you do not have access to a type name for the anonymous type. For more information about anonymous types, see [Anonymous Types](../../fundamentals/types/anonymous-types.md). [!code-csharp[csProgGuideLINQ#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideLINQ/CS/csRef30LangFeatures_2.cs#32)] diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md b/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md index 2c8e3ce2358e1..0ac06341ea1e7 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md @@ -11,7 +11,7 @@ ms.assetid: fba899b5-fc41-428e-a745-3ebdbf37990a --- # How to write a copy constructor (C# Programming Guide) -C# [records](/dotnet/csharp/fundamentals/types/records) provide a copy constructor for objects, but for classes you have to write one yourself. +C# [records](../../fundamentals/types/records.md) provide a copy constructor for objects, but for classes you have to write one yourself. ## Example @@ -22,7 +22,7 @@ C# [records](/dotnet/csharp/fundamentals/types/records) provide a copy construct ## See also - -- [Records](/dotnet/csharp/fundamentals/types/records) +- [Records](../../fundamentals/types/records.md) - [C# Programming Guide](../index.md) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Constructors](./constructors.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md b/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md index e7b7afdf0e9a5..0580ed45eb87b 100644 --- a/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md +++ b/docs/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md @@ -43,7 +43,7 @@ For more information, see [How to use implicitly typed local variables and array ## var and anonymous types -In many cases the use of `var` is optional and is just a syntactic convenience. However, when a variable is initialized with an anonymous type you must declare the variable as `var` if you need to access the properties of the object at a later point. This is a common scenario in LINQ query expressions. For more information, see [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types). +In many cases the use of `var` is optional and is just a syntactic convenience. However, when a variable is initialized with an anonymous type you must declare the variable as `var` if you need to access the properties of the object at a later point. This is a common scenario in LINQ query expressions. For more information, see [Anonymous Types](../../fundamentals/types/anonymous-types.md). From the perspective of your source code, an anonymous type has no name. Therefore, if a query variable has been initialized with `var`, then the only way to access the properties in the returned sequence of objects is to use `var` as the type of the iteration variable in the `foreach` statement. @@ -83,14 +83,14 @@ The `var` keyword can also be useful when the specific type of the variable is t [!code-csharp[cscsrefQueryKeywords#13](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsCsrefQueryKeywords/CS/Group.cs#13)] -The use of `var` helps simplify your code, but its use should be restricted to cases where it is required, or when it makes your code easier to read. For more information about when to use `var` properly, see the [Implicitly typed local variables](/dotnet/csharp/fundamentals/coding-style/coding-conventions#implicitly-typed-local-variables) section on the C# Coding Guidelines article. +The use of `var` helps simplify your code, but its use should be restricted to cases where it is required, or when it makes your code easier to read. For more information about when to use `var` properly, see the [Implicitly typed local variables](../../fundamentals/coding-style/coding-conventions.md#implicitly-typed-local-variables) section on the C# Coding Guidelines article. ## See also - [C# Reference](../../language-reference/index.md) - [Implicitly Typed Arrays](../arrays/implicitly-typed-arrays.md) - [How to use implicitly typed local variables and arrays in a query expression](how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md) -- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types](../../fundamentals/types/anonymous-types.md) - [Object and Collection Initializers](object-and-collection-initializers.md) - [var](../../language-reference/keywords/var.md) - [LINQ in C#](../../linq/index.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/interface-properties.md b/docs/csharp/programming-guide/classes-and-structs/interface-properties.md index 498b5cae9ce5e..012db4c9917d8 100644 --- a/docs/csharp/programming-guide/classes-and-structs/interface-properties.md +++ b/docs/csharp/programming-guide/classes-and-structs/interface-properties.md @@ -55,4 +55,4 @@ Employee name: Hazem Abolrous - [Using Properties](./using-properties.md) - [Comparison Between Properties and Indexers](../indexers/comparison-between-properties-and-indexers.md) - [Indexers](../indexers/index.md) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/members.md b/docs/csharp/programming-guide/classes-and-structs/members.md index 0cbb17f541f3f..1da14a7fd46b9 100644 --- a/docs/csharp/programming-guide/classes-and-structs/members.md +++ b/docs/csharp/programming-guide/classes-and-structs/members.md @@ -29,4 +29,4 @@ Classes and structs have members that represent their data and behavior. A class ## See also - [C# Programming Guide](../index.md) -- [Classes](/dotnet/csharp/fundamentals/types/classes) +- [Classes](../../fundamentals/types/classes.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/methods.md b/docs/csharp/programming-guide/classes-and-structs/methods.md index 1e94af36682f2..be38a6a1742b8 100644 --- a/docs/csharp/programming-guide/classes-and-structs/methods.md +++ b/docs/csharp/programming-guide/classes-and-structs/methods.md @@ -11,14 +11,14 @@ ms.assetid: cc738f07-e8cd-4683-9585-9f40c0667c37 A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. -The `Main` method is the entry point for every C# application and it's called by the common language runtime (CLR) when the program is started. In an application that uses [top-level statements](/dotnet/csharp/fundamentals/program-structure/top-level-statements), the `Main` method is generated by the compiler and contains all top-level statements. +The `Main` method is the entry point for every C# application and it's called by the common language runtime (CLR) when the program is started. In an application that uses [top-level statements](../../fundamentals/program-structure/top-level-statements.md), the `Main` method is generated by the compiler and contains all top-level statements. > [!NOTE] > This article discusses named methods. For information about anonymous functions, see [Anonymous Functions](../statements-expressions-operators/anonymous-functions.md). ## Method signatures -Methods are declared in a [class](../../language-reference/keywords/class.md), [struct](../../language-reference/builtin-types/struct.md), or [interface](/dotnet/csharp/fundamentals/types/interfaces) by specifying the access level such as `public` or `private`, optional modifiers such as `abstract` or `sealed`, the return value, the name of the method, and any method parameters. These parts together are the signature of the method. +Methods are declared in a [class](../../language-reference/keywords/class.md), [struct](../../language-reference/builtin-types/struct.md), or [interface](../../fundamentals/types/interfaces.md) by specifying the access level such as `public` or `private`, optional modifiers such as `abstract` or `sealed`, the return value, the name of the method, and any method parameters. These parts together are the signature of the method. > [!IMPORTANT] > A return type of a method is not part of the signature of the method for the purposes of method overloading. However, it is part of the signature of the method when determining the compatibility between a delegate and the method that it points to. @@ -168,7 +168,7 @@ For more information, see [Iterators](../concepts/iterators.md). - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Access Modifiers](access-modifiers.md) - [Static Classes and Static Class Members](static-classes-and-static-class-members.md) -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Inheritance](../../fundamentals/object-oriented/inheritance.md) - [Abstract and Sealed Classes and Class Members](abstract-and-sealed-classes-and-class-members.md) - [params](../../language-reference/keywords/params.md) - [return](../../language-reference/keywords/return.md) diff --git a/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md b/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md index dfac4267d98f4..6accaf99a949b 100644 --- a/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md +++ b/docs/csharp/programming-guide/classes-and-structs/object-and-collection-initializers.md @@ -52,7 +52,7 @@ public string this[char c, int i] { set { ... }; } ## Object Initializers with anonymous types -Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types), which can only be initialized by using an object initializer, as shown in the following declaration. +Although object initializers can be used in any context, they are especially useful in LINQ query expressions. Query expressions make frequent use of [anonymous types](../../fundamentals/types/anonymous-types.md), which can only be initialized by using an object initializer, as shown in the following declaration. ```csharp var pet = new { Age = 10, Name = "Fluffy" }; @@ -121,4 +121,4 @@ The following example shows an object that implements - [Standard Query Operators Overview (C#)](./standard-query-operators-overview.md) -- [Anonymous Types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types](../../../fundamentals/types/anonymous-types.md) - [Formulate Joins and Cross-Product Queries](../../../../framework/data/adonet/sql/linq/formulate-joins-and-cross-product-queries.md) - [join clause](../../../language-reference/keywords/join-clause.md) - [Join by using composite keys](../../../linq/join-by-using-composite-keys.md) diff --git a/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md b/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md index 7827b7bbadffe..f239e09c41ef6 100644 --- a/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md +++ b/docs/csharp/programming-guide/concepts/linq/linq-and-generic-types.md @@ -16,7 +16,7 @@ LINQ queries are based on generic types, which were introduced in version 2.0 of 2. is the interface that enables generic collection classes to be enumerated by using the `foreach` statement. Generic collection classes support just as non-generic collection classes such as support . - For more information about generics, see [Generics](/dotnet/csharp/fundamentals/types/generics). + For more information about generics, see [Generics](../../../fundamentals/types/generics.md). ## IEnumerable variables in LINQ Queries @@ -36,4 +36,4 @@ LINQ queries are based on generic types, which were introduced in version 2.0 of ## See also -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../../fundamentals/types/generics.md) diff --git a/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md b/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md index 091ecebea779d..ea1687d27392f 100644 --- a/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md +++ b/docs/csharp/programming-guide/generics/constraints-on-type-parameters.md @@ -129,6 +129,6 @@ You could use it as shown in the following sample to create an enum and build a - - [C# Programming Guide](../index.md) -- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) +- [Introduction to Generics](../../fundamentals/types/generics.md) - [Generic Classes](./generic-classes.md) - [new Constraint](../../language-reference/keywords/new-constraint.md) diff --git a/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md b/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md index 53e164f4bbe8b..203c5d60c97b3 100644 --- a/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md +++ b/docs/csharp/programming-guide/generics/differences-between-cpp-templates-and-csharp-generics.md @@ -31,5 +31,5 @@ C# Generics and C++ templates are both language features that provide support fo ## See also - [C# Programming Guide](../index.md) -- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) +- [Introduction to Generics](../../fundamentals/types/generics.md) - [Templates](/cpp/cpp/templates-cpp) diff --git a/docs/csharp/programming-guide/generics/generic-classes.md b/docs/csharp/programming-guide/generics/generic-classes.md index 18754d4dda47f..53b9b6f4fe11b 100644 --- a/docs/csharp/programming-guide/generics/generic-classes.md +++ b/docs/csharp/programming-guide/generics/generic-classes.md @@ -31,7 +31,7 @@ Generic classes encapsulate operations that are not specific to a particular dat For example, if you are designing a class that will be used to create items in a generics-based collection, you may have to implement an interface such as where `T` is the type of your class. - For an example of a simple generic class, see [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics). + For an example of a simple generic class, see [Introduction to Generics](../../fundamentals/types/generics.md). The rules for type parameters and constraints have several implications for generic class behavior, especially regarding inheritance and member accessibility. Before proceeding, you should understand some terms. For a generic class `Node,` client code can reference the class either by specifying a type argument, to create a closed constructed type (`Node`). Alternatively, it can leave the type parameter unspecified, for example when you specify a generic base class, to create an open constructed type (`Node`). Generic classes can inherit from concrete, closed constructed, or open constructed base classes: @@ -65,6 +65,6 @@ Generic classes encapsulate operations that are not specific to a particular dat - - [C# Programming Guide](../index.md) -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../fundamentals/types/generics.md) - [Saving the State of Enumerators](/archive/blogs/wesdyer/saving-the-state-of-enumerators) - [An Inheritance Puzzle, Part One](/archive/blogs/ericlippert/an-inheritance-puzzle-part-one) diff --git a/docs/csharp/programming-guide/generics/generic-delegates.md b/docs/csharp/programming-guide/generics/generic-delegates.md index 17e1eff68a5e5..68acb41376147 100644 --- a/docs/csharp/programming-guide/generics/generic-delegates.md +++ b/docs/csharp/programming-guide/generics/generic-delegates.md @@ -33,7 +33,7 @@ A [delegate](../../language-reference/builtin-types/reference-types.md) can defi - - [C# Programming Guide](../index.md) -- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) +- [Introduction to Generics](../../fundamentals/types/generics.md) - [Generic Methods](./generic-methods.md) - [Generic Classes](./generic-classes.md) - [Generic Interfaces](./generic-interfaces.md) diff --git a/docs/csharp/programming-guide/generics/generic-interfaces.md b/docs/csharp/programming-guide/generics/generic-interfaces.md index 47061e215ca5e..0ff4af11da78f 100644 --- a/docs/csharp/programming-guide/generics/generic-interfaces.md +++ b/docs/csharp/programming-guide/generics/generic-interfaces.md @@ -11,7 +11,7 @@ ms.assetid: a8fa49a1-6e78-4a09-87e5-84a0b9f5ffbe It is often useful to define interfaces either for generic collection classes, or for the generic classes that represent items in the collection. The preference for generic classes is to use generic interfaces, such as rather than , in order to avoid boxing and unboxing operations on value types. The .NET class library defines several generic interfaces for use with the collection classes in the namespace. - When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used. The following code example shows a `SortedList` class that derives from the `GenericList` class. For more information, see [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics). `SortedList` adds the constraint `where T : IComparable`. This enables the `BubbleSort` method in `SortedList` to use the generic method on list elements. In this example, list elements are a simple class, `Person`, that implements `IComparable`. + When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used. The following code example shows a `SortedList` class that derives from the `GenericList` class. For more information, see [Introduction to Generics](../../fundamentals/types/generics.md). `SortedList` adds the constraint `where T : IComparable`. This enables the `BubbleSort` method in `SortedList` to use the generic method on list elements. In this example, list elements are a simple class, `Person`, that implements `IComparable`. [!code-csharp[csProgGuideGenerics#29](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics2.cs#29)] @@ -42,6 +42,6 @@ It is often useful to define interfaces either for generic collection classes, o ## See also - [C# Programming Guide](../index.md) -- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) +- [Introduction to Generics](../../fundamentals/types/generics.md) - [interface](../../language-reference/keywords/interface.md) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/generics/generic-methods.md b/docs/csharp/programming-guide/generics/generic-methods.md index 1ce1695a39783..bcc93eefbf0a4 100644 --- a/docs/csharp/programming-guide/generics/generic-methods.md +++ b/docs/csharp/programming-guide/generics/generic-methods.md @@ -46,5 +46,5 @@ A generic method is a method that is declared with type parameters, as follows: - - [C# Programming Guide](../index.md) -- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) +- [Introduction to Generics](../../fundamentals/types/generics.md) - [Methods](../classes-and-structs/methods.md) diff --git a/docs/csharp/programming-guide/generics/generic-type-parameters.md b/docs/csharp/programming-guide/generics/generic-type-parameters.md index b182c484f300a..397451b9f3f9c 100644 --- a/docs/csharp/programming-guide/generics/generic-type-parameters.md +++ b/docs/csharp/programming-guide/generics/generic-type-parameters.md @@ -9,7 +9,7 @@ ms.assetid: a03b0ab2-0606-4b41-b7bf-e64d5bb4d18f --- # Generic type parameters (C# Programming Guide) -In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they create an instance of the generic type. A generic class, such as `GenericList` listed in [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics), cannot be used as-is because it is not really a type; it is more like a blueprint for a type. To use `GenericList`, client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler. Any number of constructed type instances can be created, each one using a different type argument, as follows: +In a generic type or method definition, a type parameter is a placeholder for a specific type that a client specifies when they create an instance of the generic type. A generic class, such as `GenericList` listed in [Introduction to Generics](../../fundamentals/types/generics.md), cannot be used as-is because it is not really a type; it is more like a blueprint for a type. To use `GenericList`, client code must declare and instantiate a constructed type by specifying a type argument inside the angle brackets. The type argument for this particular class can be any type recognized by the compiler. Any number of constructed type instances can be created, each one using a different type argument, as follows: [!code-csharp[csProgGuideGenerics#7](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideGenerics/CS/Generics.cs#7)] @@ -37,5 +37,5 @@ The code analysis rule [CA1715](/visualstudio/code-quality/ca1715) can be used t - - [C# Programming Guide](../index.md) -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../fundamentals/types/generics.md) - [Differences Between C++ Templates and C# Generics](./differences-between-cpp-templates-and-csharp-generics.md) diff --git a/docs/csharp/programming-guide/generics/generics-and-arrays.md b/docs/csharp/programming-guide/generics/generics-and-arrays.md index 5ae9f5a84173b..e6367b6130828 100644 --- a/docs/csharp/programming-guide/generics/generics-and-arrays.md +++ b/docs/csharp/programming-guide/generics/generics-and-arrays.md @@ -19,6 +19,6 @@ In C# 2.0 and later, single-dimensional arrays that have a lower bound of zero a - - [C# Programming Guide](../index.md) -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../fundamentals/types/generics.md) - [Arrays](../arrays/index.md) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/generics/generics-and-attributes.md b/docs/csharp/programming-guide/generics/generics-and-attributes.md index b41c05ef6d632..059a55e9f0262 100644 --- a/docs/csharp/programming-guide/generics/generics-and-attributes.md +++ b/docs/csharp/programming-guide/generics/generics-and-attributes.md @@ -42,5 +42,5 @@ Attributes can be applied to generic types in the same way as non-generic types. ## See also - [C# Programming Guide](../index.md) -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../fundamentals/types/generics.md) - [Attributes](../../../standard/attributes/index.md) diff --git a/docs/csharp/programming-guide/generics/generics-and-reflection.md b/docs/csharp/programming-guide/generics/generics-and-reflection.md index 28c587e0d6d33..e2e60ad427d7c 100644 --- a/docs/csharp/programming-guide/generics/generics-and-reflection.md +++ b/docs/csharp/programming-guide/generics/generics-and-reflection.md @@ -43,6 +43,6 @@ Because the Common Language Runtime (CLR) has access to generic type information ## See also - [C# Programming Guide](../index.md) -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../fundamentals/types/generics.md) - [Reflection and Generic Types](../../../framework/reflection-and-codedom/reflection-and-generic-types.md) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/generics/generics-in-the-run-time.md b/docs/csharp/programming-guide/generics/generics-in-the-run-time.md index 3de147c785995..b5d3db1e1a556 100644 --- a/docs/csharp/programming-guide/generics/generics-in-the-run-time.md +++ b/docs/csharp/programming-guide/generics/generics-in-the-run-time.md @@ -46,5 +46,5 @@ When a generic type or method is compiled into Microsoft intermediate language ( - - [C# Programming Guide](../index.md) -- [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics) +- [Introduction to Generics](../../fundamentals/types/generics.md) - [Generics](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/index.md b/docs/csharp/programming-guide/index.md index 565e045c7cf27..43d11a3cb805d 100644 --- a/docs/csharp/programming-guide/index.md +++ b/docs/csharp/programming-guide/index.md @@ -23,7 +23,7 @@ This section provides detailed information on key C# language features and featu [Inside a C# Program](/dotnet/csharp/fundamentals/program-structure) -[Main() and Command-Line Arguments](/dotnet/csharp/fundamentals/program-structure/main-command-line) +[Main() and Command-Line Arguments](../fundamentals/program-structure/main-command-line.md) ## Language Sections @@ -33,7 +33,7 @@ This section provides detailed information on key C# language features and featu [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) - [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) + [Interfaces](../fundamentals/types/interfaces.md) [Delegates](./delegates/index.md) @@ -47,13 +47,13 @@ This section provides detailed information on key C# language features and featu [Events](./events/index.md) - [Generics](/dotnet/csharp/fundamentals/types/generics) + [Generics](../fundamentals/types/generics.md) [Iterators](./concepts/iterators.md) [LINQ Query Expressions](../linq/index.md) - [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) + [Namespaces](../fundamentals/types/namespaces.md) [Unsafe Code and Pointers](../language-reference/unsafe-code.md) diff --git a/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md b/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md index 1d73166e9350e..bf9468b4bffa8 100644 --- a/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md +++ b/docs/csharp/programming-guide/indexers/indexers-in-interfaces.md @@ -61,4 +61,4 @@ implements the indexer on the `ICitizen` interface. - [C# Programming Guide](../index.md) - [Indexers](./index.md) - [Properties](../classes-and-structs/properties.md) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) diff --git a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md index fe69805e13d0a..2ebc5b2b0eb1a 100644 --- a/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md +++ b/docs/csharp/programming-guide/interfaces/explicit-interface-implementation.md @@ -44,5 +44,5 @@ Any class that implements the `IControl` interface can override the default `Pai - [C# Programming Guide](../index.md) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) -- [Inheritance](/dotnet/csharp/fundamentals/object-oriented/inheritance) +- [Interfaces](../../fundamentals/types/interfaces.md) +- [Inheritance](../../fundamentals/object-oriented/inheritance.md) diff --git a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md index 48c1e00832ad4..819afcdd665e7 100644 --- a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md +++ b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-interface-members.md @@ -29,5 +29,5 @@ This example declares an [interface](../../language-reference/keywords/interface - [C# Programming Guide](../index.md) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) - [How to explicitly implement members of two interfaces](./how-to-explicitly-implement-members-of-two-interfaces.md) diff --git a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md index 203c97be85e63..5be537d4e031c 100644 --- a/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md +++ b/docs/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces.md @@ -30,5 +30,5 @@ Explicit [interface](../../language-reference/keywords/interface.md) implementat - [C# Programming Guide](../index.md) - [Classes, structs, and records](/dotnet/csharp/fundamentals/object-oriented) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../fundamentals/types/interfaces.md) - [How to explicitly implement interface members](./how-to-explicitly-implement-interface-members.md) diff --git a/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md b/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md index 76f03a678db94..96c1e92e3648c 100644 --- a/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md +++ b/docs/csharp/programming-guide/namespaces/how-to-use-the-my-namespace.md @@ -39,5 +39,5 @@ The namespace (`My` in Visual Basic) pro ## See also - [C# Programming Guide](../index.md) -- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) +- [Namespaces](../../fundamentals/types/namespaces.md) - [Using Namespaces](./using-namespaces.md) diff --git a/docs/csharp/programming-guide/namespaces/using-namespaces.md b/docs/csharp/programming-guide/namespaces/using-namespaces.md index ffd9d26b17f42..adacf268e0d53 100644 --- a/docs/csharp/programming-guide/namespaces/using-namespaces.md +++ b/docs/csharp/programming-guide/namespaces/using-namespaces.md @@ -88,7 +88,7 @@ Namespaces are heavily used within C# programs in two ways. Firstly, the .NET cl ## See also - [C# Programming Guide](../index.md) -- [Namespaces](/dotnet/csharp/fundamentals/types/namespaces) +- [Namespaces](../../fundamentals/types/namespaces.md) - [Member access expression](../../language-reference/operators/member-access-operators.md#member-access-expression-) - [:: operator](../../language-reference/operators/namespace-alias-qualifier.md) - [extern alias](../../language-reference/keywords/extern-alias.md) diff --git a/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md b/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md index 0400c8b1b16ba..c3e2ffd3658c7 100644 --- a/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md +++ b/docs/csharp/programming-guide/statements-expressions-operators/equality-comparisons.md @@ -51,7 +51,7 @@ However, there is no requirement that equivalence be based on all the fields in |[How to define value equality for a type](./how-to-define-value-equality-for-a-type.md)|Describes how to provide a custom definition of value equality for a type.| |[C# Programming Guide](../index.md)|Provides links to detailed information about important C# language features and features that are available to C# through .NET.| |[Types](/dotnet/csharp/fundamentals/types)|Provides information about the C# type system and links to additional information.| -|[Records](/dotnet/csharp/fundamentals/types/records)|Provides information about record types, which test for value equality by default.| +|[Records](../../fundamentals/types/records.md)|Provides information about record types, which test for value equality by default.| ## See also diff --git a/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md b/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md index 9bbb89c9a80e6..dfd95fc3bc550 100644 --- a/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md +++ b/docs/csharp/programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md @@ -13,7 +13,7 @@ ms.assetid: 4084581e-b931-498b-9534-cf7ef5b68690 --- # How to define value equality for a class or struct (C# Programming Guide) -[Records](/dotnet/csharp/fundamentals/types/records) automatically implement value equality. Consider defining a `record` instead of a `class` when your type models data and should implement value equality. +[Records](../../fundamentals/types/records.md) automatically implement value equality. Consider defining a `record` instead of a `class` when your type models data and should implement value equality. When you define a class or struct, you decide whether it makes sense to create a custom definition of value equality (or equivalence) for the type. Typically, you implement value equality when you expect to add objects of the type to a collection, or when their primary purpose is to store a set of fields or properties. You can base your definition of value equality on a comparison of all the fields and properties in the type, or you can base the definition on a subset. diff --git a/docs/csharp/programming-guide/types/casting-and-type-conversions.md b/docs/csharp/programming-guide/types/casting-and-type-conversions.md index d97a34ac2cbc3..d6cb86ab91925 100644 --- a/docs/csharp/programming-guide/types/casting-and-type-conversions.md +++ b/docs/csharp/programming-guide/types/casting-and-type-conversions.md @@ -74,7 +74,7 @@ Animal a = g; Giraffe g2 = (Giraffe)a; ``` -A cast operation between reference types does not change the run-time type of the underlying object; it only changes the type of the value that is being used as a reference to that object. For more information, see [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism). +A cast operation between reference types does not change the run-time type of the underlying object; it only changes the type of the value that is being used as a reference to that object. For more information, see [Polymorphism](../../fundamentals/object-oriented/polymorphism.md). ## Type conversion exceptions at run time @@ -82,7 +82,7 @@ In some reference type conversions, the compiler cannot determine whether a cast [!code-csharp[csProgGuideTypes#41](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#41)] -The `Test` method has an `Animal` parameter, thus explicitly casting the argument `a` to a `Reptile` makes a dangerous assumption. It is safer to not make assumptions, but rather check the type. C# provides the [is](../../language-reference/operators/type-testing-and-cast.md#is-operator) operator to enable you to test for compatibility before actually performing a cast. For more information, see [How to safely cast using pattern matching and the as and is operators](/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). +The `Test` method has an `Animal` parameter, thus explicitly casting the argument `a` to a `Reptile` makes a dangerous assumption. It is safer to not make assumptions, but rather check the type. C# provides the [is](../../language-reference/operators/type-testing-and-cast.md#is-operator) operator to enable you to test for compatibility before actually performing a cast. For more information, see [How to safely cast using pattern matching and the as and is operators](../../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). ## C# language specification diff --git a/docs/csharp/programming-guide/xmldoc/typeparam.md b/docs/csharp/programming-guide/xmldoc/typeparam.md index 40ee68a3c037d..49be312d75ea2 100644 --- a/docs/csharp/programming-guide/xmldoc/typeparam.md +++ b/docs/csharp/programming-guide/xmldoc/typeparam.md @@ -31,7 +31,7 @@ ms.assetid: 9b99d400-e911-4e55-99c6-64367c96aa4f The `` tag should be used in the comment for a generic type or method declaration to describe a type parameter. Add a tag for each type parameter of the generic type or method. -For more information, see [Generics](/dotnet/csharp/fundamentals/types/generics). +For more information, see [Generics](../../fundamentals/types/generics.md). The text for the `` tag will be displayed in IntelliSense, the [Object Browser Window](/visualstudio/ide/viewing-the-structure-of-code#BKMK_ObjectBrowser) code comment web report. diff --git a/docs/csharp/programming-guide/xmldoc/typeparamref.md b/docs/csharp/programming-guide/xmldoc/typeparamref.md index 1add68d4ee1ce..76fbbb56311b0 100644 --- a/docs/csharp/programming-guide/xmldoc/typeparamref.md +++ b/docs/csharp/programming-guide/xmldoc/typeparamref.md @@ -25,7 +25,7 @@ ms.assetid: 6d8ffc58-12c5-4688-8db6-833a7ded5886 ## Remarks -For more information on type parameters in generic types and methods, see [Generics](/dotnet/csharp/fundamentals/types/generics). +For more information on type parameters in generic types and methods, see [Generics](../../fundamentals/types/generics.md). Use this tag to enable consumers of the documentation file to format the word in some distinct way, for example in italics. diff --git a/docs/csharp/tour-of-csharp/features.md b/docs/csharp/tour-of-csharp/features.md index b32c52b0d45f4..0e8f234c19965 100644 --- a/docs/csharp/tour-of-csharp/features.md +++ b/docs/csharp/tour-of-csharp/features.md @@ -112,7 +112,7 @@ The following code sample demonstrates how to get the `HelpAttribute` instances ## Learn more -You can explore more about C# by trying one of our [tutorials](/dotnet/csharp/fundamentals/object-oriented/tutorials/classes). +You can explore more about C# by trying one of our [tutorials](../fundamentals/object-oriented/tutorials/classes.md). >[!div class="step-by-step"] >[Previous](program-building-blocks.md) diff --git a/docs/csharp/tour-of-csharp/index.md b/docs/csharp/tour-of-csharp/index.md index 1128da1b7f842..16a80a4f44ca8 100644 --- a/docs/csharp/tour-of-csharp/index.md +++ b/docs/csharp/tour-of-csharp/index.md @@ -118,7 +118,7 @@ There are several kinds of *variables* in C#, including fields, array elements, ## Program structure -The key organizational concepts in C# are [***programs***](/dotnet/csharp/fundamentals/program-structure), [***namespaces***](/dotnet/csharp/fundamentals/types/namespaces), [***types***](/dotnet/csharp/fundamentals/types), [***members***](../programming-guide/classes-and-structs/members.md), and [***assemblies***](../../standard/assembly/index.md). Programs declare types, which contain members and can be organized into namespaces. Classes, structs, and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they're physically packaged into assemblies. Assemblies typically have the file extension `.exe` or `.dll`, depending on whether they implement ***applications*** or ***libraries***, respectively. +The key organizational concepts in C# are [***programs***](/dotnet/csharp/fundamentals/program-structure), [***namespaces***](../fundamentals/types/namespaces.md), [***types***](/dotnet/csharp/fundamentals/types), [***members***](../programming-guide/classes-and-structs/members.md), and [***assemblies***](../../standard/assembly/index.md). Programs declare types, which contain members and can be organized into namespaces. Classes, structs, and interfaces are examples of types. Fields, methods, properties, and events are examples of members. When C# programs are compiled, they're physically packaged into assemblies. Assemblies typically have the file extension `.exe` or `.dll`, depending on whether they implement ***applications*** or ***libraries***, respectively. As a small example, consider an assembly that contains the following code: diff --git a/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md b/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md index 1f78cedb78a64..547b54825d8b4 100644 --- a/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md +++ b/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md @@ -205,6 +205,6 @@ You can see an example solution by [looking at the finished sample code on GitHu With each iteration of the loop, you're taking the last two integers in the list, summing them, and adding that value to the list. The loop repeats until you've added 20 items to the list. -Congratulations, you've completed the list tutorial. You can continue with [additional](/dotnet/csharp/fundamentals/object-oriented/tutorials/classes) tutorials in your own development environment. +Congratulations, you've completed the list tutorial. You can continue with [additional](../../fundamentals/object-oriented/tutorials/classes.md) tutorials in your own development environment. You can learn more about working with the `List` type in the .NET fundamentals article on [collections](../../../standard/collections/index.md). You'll also learn about many other collection types. diff --git a/docs/csharp/tour-of-csharp/types.md b/docs/csharp/tour-of-csharp/types.md index 4592a9a053214..d553fcbe0e341 100644 --- a/docs/csharp/tour-of-csharp/types.md +++ b/docs/csharp/tour-of-csharp/types.md @@ -32,7 +32,7 @@ The memory occupied by an object is automatically reclaimed when the object is n ### Type parameters -Generic classes define [***type parameters***](/dotnet/csharp/fundamentals/types/generics). Type parameters are a list of type parameter names enclosed in angle brackets. Type parameters follow the class name. The type parameters can then be used in the body of the class declarations to define the members of the class. In the following example, the type parameters of `Pair` are `TFirst` and `TSecond`: +Generic classes define [***type parameters***](../fundamentals/types/generics.md). Type parameters are a list of type parameter names enclosed in angle brackets. Type parameters follow the class name. The type parameters can then be used in the body of the class declarations to define the members of the class. In the following example, the type parameters of `Pair` are `TFirst` and `TSecond`: :::code language="csharp" source="./snippets/shared/Types.cs" ID="DefinePairClass"::: @@ -63,7 +63,7 @@ Classes define types that support inheritance and polymorphism. They enable you ## Interfaces -An [***interface***](/dotnet/csharp/fundamentals/types/interfaces) defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers. An interface typically doesn't provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface. +An [***interface***](../fundamentals/types/interfaces.md) defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers. An interface typically doesn't provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface. Interfaces may employ ***multiple inheritance***. In the following example, the interface `IComboBox` inherits from both `ITextBox` and `IListBox`. diff --git a/docs/csharp/tutorials/console-webapiclient.md b/docs/csharp/tutorials/console-webapiclient.md index 4dbf6ebaca079..36407f030f0d9 100644 --- a/docs/csharp/tutorials/console-webapiclient.md +++ b/docs/csharp/tutorials/console-webapiclient.md @@ -182,7 +182,7 @@ The following steps convert the JSON response into C# objects. You use the is an `await` expression. `await` expressions can appear almost anywhere in your code, even though up to now, you've only seen them as part of an assignment statement. The other two parameters, `JsonSerializerOptions` and `CancellationToken`, are optional and are omitted in the code snippet. - The `DeserializeAsync` method is [*generic*](/dotnet/csharp/fundamentals/types/generics), which means you supply type arguments for what kind of objects should be created from the JSON text. In this example, you're deserializing to a `List`, which is another generic object, a . The `List` class stores a collection of objects. The type argument declares the type of objects stored in the `List`. The type argument is your `Repository` class, because the JSON text represents a collection of repository objects. + The `DeserializeAsync` method is [*generic*](../fundamentals/types/generics.md), which means you supply type arguments for what kind of objects should be created from the JSON text. In this example, you're deserializing to a `List`, which is another generic object, a . The `List` class stores a collection of objects. The type argument declares the type of objects stored in the `List`. The type argument is your `Repository` class, because the JSON text represents a collection of repository objects. 1. Add code to display the name of each repository. Replace the lines that read: diff --git a/docs/csharp/tutorials/exploration/interpolated-strings-local.md b/docs/csharp/tutorials/exploration/interpolated-strings-local.md index af17777b7306b..c41f271ca4c67 100644 --- a/docs/csharp/tutorials/exploration/interpolated-strings-local.md +++ b/docs/csharp/tutorials/exploration/interpolated-strings-local.md @@ -40,7 +40,7 @@ Let's try a few more string interpolation examples with some other data types. In the previous section, you used string interpolation to insert one string inside of another. The result of an interpolation expression can be of any data type, though. Let's include values of various data types in an interpolated string. -In the following example, we first define a [class](/dotnet/csharp/fundamentals/types/classes) data type `Vegetable` that has a `Name` [property](../../properties.md) and a `ToString` [method](../../methods.md), which [overrides](../../language-reference/keywords/override.md) the behavior of the method. The [`public` access modifier](../../language-reference/keywords/public.md) makes that method available to any client code to get the string representation of a `Vegetable` instance. In the example the `Vegetable.ToString` method returns the value of the `Name` property that is initialized at the `Vegetable` [constructor](../../programming-guide/classes-and-structs/constructors.md): +In the following example, we first define a [class](../../fundamentals/types/classes.md) data type `Vegetable` that has a `Name` [property](../../properties.md) and a `ToString` [method](../../methods.md), which [overrides](../../language-reference/keywords/override.md) the behavior of the method. The [`public` access modifier](../../language-reference/keywords/public.md) makes that method available to any client code to get the string representation of a `Vegetable` instance. In the example the `Vegetable.ToString` method returns the value of the `Name` property that is initialized at the `Vegetable` [constructor](../../programming-guide/classes-and-structs/constructors.md): ```csharp public Vegetable(string name) => Name = name; diff --git a/docs/csharp/whats-new/csharp-7.md b/docs/csharp/whats-new/csharp-7.md index 30a987d34fc74..6b7f3e9db6634 100644 --- a/docs/csharp/whats-new/csharp-7.md +++ b/docs/csharp/whats-new/csharp-7.md @@ -131,7 +131,7 @@ public class Example } ``` -For more information, see [Discards](/dotnet/csharp/fundamentals/functional/discards). +For more information, see [Discards](../fundamentals/functional/discards.md). ## Pattern matching @@ -228,7 +228,7 @@ static async Task Main() } ``` -You can read more about the details in the [async main](/dotnet/csharp/fundamentals/program-structure/main-command-line) article in the programming guide. +You can read more about the details in the [async main](../fundamentals/program-structure/main-command-line.md) article in the programming guide. ## Local functions diff --git a/docs/csharp/whats-new/csharp-8.md b/docs/csharp/whats-new/csharp-8.md index fb31f4cc0d7c8..6318a37a72bd7 100644 --- a/docs/csharp/whats-new/csharp-8.md +++ b/docs/csharp/whats-new/csharp-8.md @@ -259,7 +259,7 @@ static Quadrant GetQuadrant(Point point) => point switch The discard pattern in the preceding switch matches when either `x` or `y` is 0, but not both. A switch expression must either produce a value or throw an exception. If none of the cases match, the switch expression throws an exception. The compiler generates a warning for you if you don't cover all possible cases in your switch expression. -You can explore pattern matching techniques in this [advanced tutorial on pattern matching](/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching). For more information about a positional pattern, see the [Positional pattern](../language-reference/operators/patterns.md#positional-pattern) section of the [Patterns](../language-reference/operators/patterns.md) article. +You can explore pattern matching techniques in this [advanced tutorial on pattern matching](../fundamentals/functional/tutorials/pattern-matching.md). For more information about a positional pattern, see the [Positional pattern](../language-reference/operators/patterns.md#positional-pattern) section of the [Patterns](../language-reference/operators/patterns.md) article. ## Using declarations diff --git a/docs/csharp/whats-new/csharp-9.md b/docs/csharp/whats-new/csharp-9.md index e44b09e18fca7..2af386ebd3f8a 100644 --- a/docs/csharp/whats-new/csharp-9.md +++ b/docs/csharp/whats-new/csharp-9.md @@ -191,7 +191,7 @@ One of the most common uses for this feature is creating teaching materials. Beg Most importantly, top-level statements don't limit your application's scope or complexity. Those statements can access or use any .NET class. They also don't limit your use of command-line arguments or return values. Top-level statements can access an array of strings named `args`. If the top-level statements return an integer value, that value becomes the integer return code from a synthesized `Main` method. The top-level statements may contain async expressions. In that case, the synthesized entry point returns a `Task`, or `Task`. -For more information, see [Top-level statements](/dotnet/csharp/fundamentals/program-structure/top-level-statements) in the C# Programming Guide. +For more information, see [Top-level statements](../fundamentals/program-structure/top-level-statements.md) in the C# Programming Guide. ## Pattern matching enhancements diff --git a/docs/csharp/whats-new/csharp-version-history.md b/docs/csharp/whats-new/csharp-version-history.md index ee53fcc630329..acee58effae54 100644 --- a/docs/csharp/whats-new/csharp-version-history.md +++ b/docs/csharp/whats-new/csharp-version-history.md @@ -31,9 +31,9 @@ C# version 1.0 looked stripped of features, compared to today. You'd find yourse The major features of C# 1.0 included: -- [Classes](/dotnet/csharp/fundamentals/types/classes) +- [Classes](../fundamentals/types/classes.md) - [Structs](../language-reference/builtin-types/struct.md) -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../fundamentals/types/interfaces.md) - [Events](../events-overview.md) - [Properties](../properties.md) - [Delegates](../delegates-overview.md) @@ -49,7 +49,7 @@ C# version 1.2 shipped with Visual Studio .NET 2003. It contained a few small en Now things start to get interesting. Let's take a look at some major features of C# 2.0, released in 2005, along with Visual Studio 2005: -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../fundamentals/types/generics.md) - [Partial types](../programming-guide/classes-and-structs/partial-classes-and-methods.md#partial-classes) - [Anonymous methods](../language-reference/operators/delegate-operator.md) - [Nullable value types](../language-reference/builtin-types/nullable-value-types.md) @@ -76,7 +76,7 @@ And yet, C# continued to play a bit of catch-up with Java. Java had already rele C# version 3.0 came in late 2007, along with Visual Studio 2008, though the full boat of language features would actually come with .NET Framework version 3.5. This version marked a major change in the growth of C#. It established C# as a truly formidable programming language. Let's take a look at some major features in this version: - [Auto-implemented properties](../programming-guide/classes-and-structs/auto-implemented-properties.md) -- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous types](../fundamentals/types/anonymous-types.md) - [Query expressions](../linq/query-expression-basics.md) - [Lambda expressions](../language-reference/operators/lambda-expressions.md) - [Expression trees](../expression-trees.md) diff --git a/docs/csharp/whats-new/tutorials/top-level-statements.md b/docs/csharp/whats-new/tutorials/top-level-statements.md index 639c5df2513c7..2ce62e5873d63 100644 --- a/docs/csharp/whats-new/tutorials/top-level-statements.md +++ b/docs/csharp/whats-new/tutorials/top-level-statements.md @@ -216,4 +216,4 @@ This adds the call to `Utilities.ShowConsoleAnimation`, and adds an additional ` Top-level statements make it easier to create simple programs for use to explore new algorithms. You can experiment with algorithms by trying different snippets of code. Once you've learned what works, you can refactor the code to be more maintainable. -Top-level statements simplify programs that are based on console applications. These include Azure functions, GitHub actions, and other small utilities. For more information, see [Top-level statements (C# Programming Guide)](/dotnet/csharp/fundamentals/program-structure/top-level-statements). +Top-level statements simplify programs that are based on console applications. These include Azure functions, GitHub actions, and other small utilities. For more information, see [Top-level statements (C# Programming Guide)](../../fundamentals/program-structure/top-level-statements.md). diff --git a/docs/fsharp/language-reference/anonymous-records.md b/docs/fsharp/language-reference/anonymous-records.md index f795b320e7e23..211839392a848 100644 --- a/docs/fsharp/language-reference/anonymous-records.md +++ b/docs/fsharp/language-reference/anonymous-records.md @@ -234,7 +234,7 @@ Anonymous records are useful for sending lightweight data over a network without ### Anonymous records interoperate with C# anonymous types -It is possible to use a .NET API that requires the use of [C# anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types). C# anonymous types are trivial to interoperate with by using anonymous records. The following example shows how to use anonymous records to call a [LINQ](../../csharp/programming-guide/concepts/linq/index.md) overload that requires an anonymous type: +It is possible to use a .NET API that requires the use of [C# anonymous types](../../csharp/fundamentals/types/anonymous-types.md). C# anonymous types are trivial to interoperate with by using anonymous records. The following example shows how to use anonymous records to call a [LINQ](../../csharp/programming-guide/concepts/linq/index.md) overload that requires an anonymous type: ```fsharp open System.Linq diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1000.md b/docs/fundamentals/code-analysis/quality-rules/ca1000.md index da444fdacbaef..402b3df49a066 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1000.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1000.md @@ -80,4 +80,4 @@ You can configure this option for just this rule, for all rules, or for all rule ## See also -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../../csharp/fundamentals/types/generics.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1002.md b/docs/fundamentals/code-analysis/quality-rules/ca1002.md index fd73055c23f5c..77b1376d613cc 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1002.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1002.md @@ -72,4 +72,4 @@ You can configure this option for just this rule, for all rules, or for all rule ## See also -[Generics](/dotnet/csharp/fundamentals/types/generics) +[Generics](../../../csharp/fundamentals/types/generics.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1003.md b/docs/fundamentals/code-analysis/quality-rules/ca1003.md index d577d781d3ab3..4648fa06787af 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1003.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1003.md @@ -74,4 +74,4 @@ The following code snippet removes the delegate declaration from the previous ex ## See also -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../../csharp/fundamentals/types/generics.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1005.md b/docs/fundamentals/code-analysis/quality-rules/ca1005.md index 2f936eed8e6bf..b28b34b5aa9fd 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1005.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1005.md @@ -62,4 +62,4 @@ You can configure this option for just this rule, for all rules, or for all rule ## See also -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../../csharp/fundamentals/types/generics.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1010.md b/docs/fundamentals/code-analysis/quality-rules/ca1010.md index a716941648cf9..3539640f49297 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1010.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1010.md @@ -111,4 +111,4 @@ Changing the base class of an already released class is considered a breaking ch ## See also -- [Generics](/dotnet/csharp/fundamentals/types/generics) +- [Generics](../../../csharp/fundamentals/types/generics.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1033.md b/docs/fundamentals/code-analysis/quality-rules/ca1033.md index 8222e39038f54..32f3c58cae546 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1033.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1033.md @@ -48,4 +48,4 @@ The following example shows a type, `ViolatingBase`, that violates the rule and ## See also -- [Interfaces](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces](../../../csharp/fundamentals/types/interfaces.md) diff --git a/docs/fundamentals/code-analysis/quality-rules/ca1801.md b/docs/fundamentals/code-analysis/quality-rules/ca1801.md index 990374fc63b84..b57598f23171c 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca1801.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca1801.md @@ -43,7 +43,7 @@ This rule does not examine the following kinds of methods: - Methods declared with the `extern` (`Declare` statement in Visual Basic) modifier. -This rule does not flag parameters that are named with the [discard](/dotnet/csharp/fundamentals/functional/discards) symbol, for example, `_`, `_1`, and `_2`. This reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. +This rule does not flag parameters that are named with the [discard](../../../csharp/fundamentals/functional/discards.md) symbol, for example, `_`, `_1`, and `_2`. This reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. > [!NOTE] > This rule has been deprecated in favor of [IDE0060](../style-rules/ide0060.md). For information about how to enforce the IDE0060 analyzer at build, see [code-style analysis](../overview.md#code-style-analysis). diff --git a/docs/fundamentals/code-analysis/quality-rules/ca2119.md b/docs/fundamentals/code-analysis/quality-rules/ca2119.md index 77abe60daaa1a..0a1960ef3f55a 100644 --- a/docs/fundamentals/code-analysis/quality-rules/ca2119.md +++ b/docs/fundamentals/code-analysis/quality-rules/ca2119.md @@ -67,5 +67,5 @@ The following example exploits the virtual method implementation of the previous ## See also -- [Interfaces (C#)](/dotnet/csharp/fundamentals/types/interfaces) +- [Interfaces (C#)](../../../csharp/fundamentals/types/interfaces.md) - [Interfaces (Visual Basic)](../../../visual-basic/programming-guide/language-features/interfaces/index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0050.md b/docs/fundamentals/code-analysis/style-rules/ide0050.md index 181c9f3ad3643..9cd90aacdd6c2 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0050.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0050.md @@ -25,7 +25,7 @@ dev_langs: ## Overview -This rule recommends use of [tuples](../../../csharp/language-reference/builtin-types/value-tuples.md) over [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types), when the anonymous type has two or more fields. This rule has no associated code style option. +This rule recommends use of [tuples](../../../csharp/language-reference/builtin-types/value-tuples.md) over [anonymous types](../../../csharp/fundamentals/types/anonymous-types.md), when the anonymous type has two or more fields. This rule has no associated code style option. ## Example @@ -48,7 +48,7 @@ Dim t1 = (a:=1, b:=2) ## See also - [Tuples](../../../csharp/language-reference/builtin-types/value-tuples.md) -- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous types](../../../csharp/fundamentals/types/anonymous-types.md) - [Expression-level preferences](expression-level-preferences.md) - [Code style language rules](language-rules.md) - [Code style rules reference](index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0058.md b/docs/fundamentals/code-analysis/style-rules/ide0058.md index 291c876d5e6a3..0af97e02eeae4 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0058.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0058.md @@ -44,7 +44,7 @@ Users can take one of the following actions to fix this violation: - If the expression has no side effects, remove the entire statement. This improves performance by avoiding unnecessary computation. -- If the expression has side effects, replace the left side of the assignment with a [discard](/dotnet/csharp/fundamentals/functional/discards) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. +- If the expression has side effects, replace the left side of the assignment with a [discard](../../../csharp/fundamentals/functional/discards.md) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. ```csharp _ = Compute(); @@ -56,7 +56,7 @@ Users can take one of the following actions to fix this violation: |-|-| | **Option name** | csharp_style_unused_value_expression_statement_preference | **Applicable languages** | C# | -| **Option values** | `discard_variable` - Prefer to assign an unused expression to a [discard](/dotnet/csharp/fundamentals/functional/discards)

`unused_local_variable` - Prefer to assign an unused expression to a local variable that is never used | +| **Option values** | `discard_variable` - Prefer to assign an unused expression to a [discard](../../../csharp/fundamentals/functional/discards.md)

`unused_local_variable` - Prefer to assign an unused expression to a local variable that is never used | | **Default option value** | `discard_variable` | ### Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0059.md b/docs/fundamentals/code-analysis/style-rules/ide0059.md index 04f6dc7a7fb6e..5498e3e31f9c3 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0059.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0059.md @@ -44,7 +44,7 @@ Users can take one of the following actions to fix this violation: int v = Compute2(); ``` -- If the expression on the right side of the assignment has side effects, replace the left side of the assignment with a [discard](/dotnet/csharp/fundamentals/functional/discards) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. +- If the expression on the right side of the assignment has side effects, replace the left side of the assignment with a [discard](../../../csharp/fundamentals/functional/discards.md) or a local variable that's never used. This improves code clarity and explicit intent of discarding an unused value. The option for this rule concerns the use of a discard versus unused local variable. ```csharp _ = Compute(); @@ -57,7 +57,7 @@ Users can take one of the following actions to fix this violation: |-|-| | **Option name** | csharp_style_unused_value_assignment_preference | **Applicable languages** | C# | -| **Option values** | `discard_variable` - Prefer to use a [discard](/dotnet/csharp/fundamentals/functional/discards) when assigning a value that's not used

`unused_local_variable` - Prefer to use a local variable when assigning a value that's not used | +| **Option values** | `discard_variable` - Prefer to use a [discard](../../../csharp/fundamentals/functional/discards.md) when assigning a value that's not used

`unused_local_variable` - Prefer to use a local variable when assigning a value that's not used | | **Default option value** | `discard_variable` | ### Example diff --git a/docs/fundamentals/code-analysis/style-rules/ide0060.md b/docs/fundamentals/code-analysis/style-rules/ide0060.md index a3a6d2fe4cfaa..6f2ac2d4e5547 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0060.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0060.md @@ -29,7 +29,7 @@ dev_langs: This rule flags unused parameters. The option value determines if unused parameters should be flagged only for non-public methods or for both public and non-public methods. -This rule does not flag parameters that are named with the [discard](/dotnet/csharp/fundamentals/functional/discards) symbol `_`. In addition, the rule ignores parameters that are named with the discard symbol followed by an integer, for example, `_1`. This behavior reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. +This rule does not flag parameters that are named with the [discard](../../../csharp/fundamentals/functional/discards.md) symbol `_`. In addition, the rule ignores parameters that are named with the discard symbol followed by an integer, for example, `_1`. This behavior reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code. ## dotnet_code_quality_unused_parameters diff --git a/docs/standard/base-types/choosing-between-anonymous-and-tuple.md b/docs/standard/base-types/choosing-between-anonymous-and-tuple.md index a9ab65f496c17..b30206f8819c7 100644 --- a/docs/standard/base-types/choosing-between-anonymous-and-tuple.md +++ b/docs/standard/base-types/choosing-between-anonymous-and-tuple.md @@ -48,7 +48,7 @@ internal sealed class f__AnonymousType0 } ``` -For more information, see [anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types). The same functionality exists with tuples when projecting into LINQ queries, you can select properties into tuples. These tuples flow through the query, just as anonymous types would. Now consider the following example using the `System.Tuple`. +For more information, see [anonymous types](../../csharp/fundamentals/types/anonymous-types.md). The same functionality exists with tuples when projecting into LINQ queries, you can select properties into tuples. These tuples flow through the query, just as anonymous types would. Now consider the following example using the `System.Tuple`. ```csharp-interactive var dates = new[] @@ -114,7 +114,7 @@ As a developer choosing between tuples and anonymous types, there are several fa ## See also -- [Anonymous types](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous types](../../csharp/fundamentals/types/anonymous-types.md) - [Expression trees](../../csharp/expression-trees.md) - [Tuple types (C# reference)](../../csharp/language-reference/builtin-types/value-tuples.md) - [Tuples (Visual Basic)](../../visual-basic/programming-guide/language-features/data-types/tuples.md) diff --git a/docs/standard/generics.md b/docs/standard/generics.md index f6a9cbe228be7..c5cc29dc2f60b 100644 --- a/docs/standard/generics.md +++ b/docs/standard/generics.md @@ -62,4 +62,4 @@ An additional advantage of the runtime knowing the type of your generic is a bet ## See also -- [C# Programming Guide - Generics](/dotnet/csharp/fundamentals/types/generics) +- [C# Programming Guide - Generics](../csharp/fundamentals/types/generics.md) diff --git a/docs/standard/generics/covariance-and-contravariance.md b/docs/standard/generics/covariance-and-contravariance.md index 327b8002d9907..db5aa04d30fa9 100644 --- a/docs/standard/generics/covariance-and-contravariance.md +++ b/docs/standard/generics/covariance-and-contravariance.md @@ -36,7 +36,7 @@ When you're referring to a type system, covariance, contravariance, and invarian You cannot assign an instance of `List` to a variable of type `List` or vice versa. - Covariant type parameters enable you to make assignments that look much like ordinary [Polymorphism](/dotnet/csharp/fundamentals/object-oriented/polymorphism), as shown in the following code. + Covariant type parameters enable you to make assignments that look much like ordinary [Polymorphism](../../csharp/fundamentals/object-oriented/polymorphism.md), as shown in the following code. [!code-csharp[CoContraSimpleIEnum#1](../../../samples/snippets/csharp/VS_Snippets_CLR/cocontrasimpleienum/cs/example.cs#1)] [!code-vb[CoContraSimpleIEnum#1](../../../samples/snippets/visualbasic/VS_Snippets_CLR/cocontrasimpleienum/vb/example.vb#1)] diff --git a/docs/standard/generics/index.md b/docs/standard/generics/index.md index 2953f2c4d40b5..ead58eb1f8f6d 100644 --- a/docs/standard/generics/index.md +++ b/docs/standard/generics/index.md @@ -122,7 +122,7 @@ Generics let you tailor a method, class, structure, or interface to the precise The common language runtime provides new opcodes and prefixes to support generic types in Microsoft intermediate language (MSIL), including , , , , and . - Visual C++, C#, and Visual Basic all provide full support for defining and using generics. For more information about language support, see [Generic Types in Visual Basic](../../visual-basic/programming-guide/language-features/data-types/generic-types.md), [Introduction to Generics](/dotnet/csharp/fundamentals/types/generics), and [Overview of Generics in Visual C++](/cpp/windows/overview-of-generics-in-visual-cpp). + Visual C++, C#, and Visual Basic all provide full support for defining and using generics. For more information about language support, see [Generic Types in Visual Basic](../../visual-basic/programming-guide/language-features/data-types/generic-types.md), [Introduction to Generics](../../csharp/fundamentals/types/generics.md), and [Overview of Generics in Visual C++](/cpp/windows/overview-of-generics-in-visual-cpp). ## Nested Types and Generics @@ -140,7 +140,7 @@ Generics let you tailor a method, class, structure, or interface to the precise |[When to Use Generic Collections](../collections/when-to-use-generic-collections.md)|Describes general rules for determining when to use generic collection types.| |[How to: Define a Generic Type with Reflection Emit](../../framework/reflection-and-codedom/how-to-define-a-generic-type-with-reflection-emit.md)|Explains how to generate dynamic assemblies that include generic types and methods.| |[Generic Types in Visual Basic](../../visual-basic/programming-guide/language-features/data-types/generic-types.md)|Describes the generics feature for Visual Basic users, including how-to topics for using and defining generic types.| -|[Introduction to Generics](/dotnet/csharp/fundamentals/types/generics)|Provides an overview of defining and using generic types for C# users.| +|[Introduction to Generics](../../csharp/fundamentals/types/generics.md)|Provides an overview of defining and using generic types for C# users.| |[Overview of Generics in Visual C++](/cpp/windows/overview-of-generics-in-visual-cpp)|Describes the generics feature for C++ users, including the differences between generics and templates.| ## Reference diff --git a/docs/standard/linq/concepts-terminology-functional-transformation.md b/docs/standard/linq/concepts-terminology-functional-transformation.md index 04833292ff212..ca59203ffa2e6 100644 --- a/docs/standard/linq/concepts-terminology-functional-transformation.md +++ b/docs/standard/linq/concepts-terminology-functional-transformation.md @@ -57,7 +57,7 @@ A structured set of data, usually of a uniform type. To be compatible with LINQ, tuple (anonymous types) \ A mathematical concept, a tuple is a finite sequence of objects, each of a specific type. A tuple is also known as an ordered list. Anonymous types are a language implementation of this concept, which enable an unnamed class type to be declared and an object of that type to be instantiated at the same time. -For more information, see [Anonymous Types (C# Programming Guide)](/dotnet/csharp/fundamentals/types/anonymous-types) and [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md). +For more information, see [Anonymous Types (C# Programming Guide)](../../csharp/fundamentals/types/anonymous-types.md) and [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md). type inference (implicit typing) \ The ability of a compiler to determine the type of a variable in the absence of an explicit type declaration. diff --git a/docs/standard/linq/project-anonymous-type.md b/docs/standard/linq/project-anonymous-type.md index d3dd27596ba6f..e9e5345cf478e 100644 --- a/docs/standard/linq/project-anonymous-type.md +++ b/docs/standard/linq/project-anonymous-type.md @@ -25,7 +25,7 @@ The type of each property is inferred by the compiler. The type name is generate For more information about anonymous types, see: -- [Anonymous Types (C# Programming Guide)](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types (C# Programming Guide)](../../csharp/fundamentals/types/anonymous-types.md) - [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md) ## Example: Project an anonymous type by creating objects in the `select` clause @@ -72,5 +72,5 @@ LETSS:Let's Stop N Shop:Jaime Yorres ## See also -- [Anonymous Types (C# Programming Guide)](/dotnet/csharp/fundamentals/types/anonymous-types) +- [Anonymous Types (C# Programming Guide)](../../csharp/fundamentals/types/anonymous-types.md) - [Anonymous Types (Visual Basic)](../../visual-basic/programming-guide/language-features/objects-and-classes/anonymous-types.md) diff --git a/docs/whats-new/2021-03.md b/docs/whats-new/2021-03.md index e3ed3da9124c9..7bd44aac6175e 100644 --- a/docs/whats-new/2021-03.md +++ b/docs/whats-new/2021-03.md @@ -38,10 +38,10 @@ Welcome to what's new in the .NET docs from March 1, 2021 through April 1, 2021. - [C# preprocessor directives](../csharp/language-reference/preprocessor-directives.md) - [`nint` and `nuint` types (C# reference)](../csharp/language-reference/builtin-types/nint-nuint.md) -- [Top-level statements (C# Programming Guide)](/dotnet/csharp/fundamentals/program-structure/top-level-statements) +- [Top-level statements (C# Programming Guide)](../csharp/fundamentals/program-structure/top-level-statements.md) - [init (C# Reference)](../csharp/language-reference/keywords/init.md) - [Records (C# reference)](../csharp/language-reference/builtin-types/record.md) -- [Records (C# Programming Guide)](/dotnet/csharp/fundamentals/types/records) +- [Records (C# Programming Guide)](../csharp/fundamentals/types/records.md) ### Updated articles From 7ce5d96c863a50944b13d9e64b58562e12e3e103 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 11:49:22 -0400 Subject: [PATCH 44/60] fix warnings with repeated H2s --- docs/csharp/language-reference/keywords/interface.md | 4 ++-- docs/csharp/language-reference/keywords/sealed.md | 2 -- docs/csharp/linq/group-query-results.md | 12 ++++++------ .../how-to-define-abstract-properties.md | 6 +----- ...cal-variables-and-arrays-in-a-query-expression.md | 4 +--- .../types/how-to-convert-a-byte-array-to-an-int.md | 4 +--- ...-between-hexadecimal-strings-and-numeric-types.md | 12 +----------- 7 files changed, 12 insertions(+), 32 deletions(-) diff --git a/docs/csharp/language-reference/keywords/interface.md b/docs/csharp/language-reference/keywords/interface.md index 1c3b2f2d3fb5b..fb02e23b330fb 100644 --- a/docs/csharp/language-reference/keywords/interface.md +++ b/docs/csharp/language-reference/keywords/interface.md @@ -16,7 +16,7 @@ In the following example, class `ImplementationClass` must implement a method na For more information and examples, see [Interfaces](../../fundamentals/types/interfaces.md). -## Example +## Example interface [!code-csharp[csrefKeywordsTypes#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsTypes/CS/keywordsTypes.cs#14)] @@ -54,7 +54,7 @@ A class that implements an interface can explicitly implement members of that in For more information about explicit interface implementation, see [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md). -## Example +## Example interface implementation The following example demonstrates interface implementation. In this example, the interface contains the property declaration and the class contains the implementation. Any instance of a class that implements `IPoint` has integer properties `x` and `y`. diff --git a/docs/csharp/language-reference/keywords/sealed.md b/docs/csharp/language-reference/keywords/sealed.md index 9a414fe6f8b74..ed55d476fca0a 100644 --- a/docs/csharp/language-reference/keywords/sealed.md +++ b/docs/csharp/language-reference/keywords/sealed.md @@ -38,8 +38,6 @@ For more information, see [Inheritance](../../fundamentals/object-oriented/inher For more examples, see [Abstract and Sealed Classes and Class Members](../../programming-guide/classes-and-structs/abstract-and-sealed-classes-and-class-members.md). -## Example - [!code-csharp[csrefKeywordsModifiers#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#17)] In the previous example, you might try to inherit from the sealed class by using the following statement: diff --git a/docs/csharp/linq/group-query-results.md b/docs/csharp/linq/group-query-results.md index ad0e639c270b0..cd6d005995c34 100644 --- a/docs/csharp/linq/group-query-results.md +++ b/docs/csharp/linq/group-query-results.md @@ -20,13 +20,13 @@ Grouping is one of the most powerful capabilities of LINQ. The following example In addition, the last two queries project their results into a new anonymous type that contains only the student's first and last name. For more information, see the [group clause](../language-reference/keywords/group-clause.md). -## Example +## Example helper class and data source All the examples in this topic use the following helper classes and data sources. [!code-csharp[csProgGuideLINQ#15](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_1.cs)] -## Example +## Group by single property example The following example shows how to group source elements by using a single property of the element as the group key. In this case the key is a `string`, the student's last name. It is also possible to use a substring for the key. The grouping operation uses the default equality comparer for the type. @@ -34,7 +34,7 @@ Paste the following method into the `StudentClass` class. Change the calling sta [!code-csharp[csProgGuideLINQ#17](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_2.cs)] -## Example +## Group by value example The following example shows how to group source elements by using something other than a property of the object for the group key. In this example, the key is the first letter of the student's last name. @@ -42,7 +42,7 @@ Paste the following method into the `StudentClass` class. Change the calling sta [!code-csharp[csProgGuideLINQ#18](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_3.cs)] -## Example +## Group by a range example The following example shows how to group source elements by using a numeric range as a group key. The query then projects the results into an anonymous type that contains only the first and last name and the percentile range to which the student belongs. An anonymous type is used because it is not necessary to use the complete `Student` object to display the results. `GetPercentile` is a helper function that calculates a percentile based on the student's average score. The method returns an integer between 0 and 10. @@ -52,7 +52,7 @@ Paste the following method into the `StudentClass` class. Change the calling sta [!code-csharp[csProgGuideLINQ#19](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_5.cs)] -## Example +## Group by comparison example The following example shows how to group source elements by using a Boolean comparison expression. In this example, the Boolean expression tests whether a student's average exam score is greater than 75. As in previous examples, the results are projected into an anonymous type because the complete source element is not needed. Note that the properties in the anonymous type become properties on the `Key` member and can be accessed by name when the query is executed. @@ -60,7 +60,7 @@ Paste the following method into the `StudentClass` class. Change the calling sta [!code-csharp[csProgGuideLINQ#20](~/samples/snippets/csharp/concepts/linq/how-to-group-query-results_6.cs)] -## Example +## Group by anonymous type The following example shows how to use an anonymous type to encapsulate a key that contains multiple values. In this example, the first key value is the first letter of the student's last name. The second key value is a Boolean that specifies whether the student scored over 85 on the first exam. You can order the groups by any property in the key. diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md b/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md index c103220472de7..4a262a890f105 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties.md @@ -27,7 +27,7 @@ The following example shows how to define [abstract](../../language-reference/ke This will create the executable file shapetest.exe. -## Example +## Examples This file declares the `Shape` class that contains the `Area` property of the type `double`. @@ -41,14 +41,10 @@ The following example shows how to define [abstract](../../language-reference/ke - When declaring an abstract property (such as `Area` in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a [get](../../language-reference/keywords/get.md) accessor is available, so the property is read-only. -## Example - The following code shows three subclasses of `Shape` and how they override the `Area` property to provide their own implementation. [!code-csharp[csProgGuideInheritance#2](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideInheritance/CS/Inheritance.cs#2)] -## Example - The following code shows a test program that creates a number of `Shape`-derived objects and prints out their areas. [!code-csharp[csProgGuideInheritance#3](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideInheritance/CS/Inheritance.cs#3)] diff --git a/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md b/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md index d301b13667be0..8e9545b40320f 100644 --- a/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md +++ b/docs/csharp/programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md @@ -14,14 +14,12 @@ You can use implicitly typed local variables whenever you want the compiler to d Implicitly typed local variables are declared by using the [var](../../language-reference/keywords/var.md) contextual keyword. For more information, see [Implicitly Typed Local Variables](./implicitly-typed-local-variables.md) and [Implicitly Typed Arrays](../arrays/implicitly-typed-arrays.md). -## Example +## Examples The following example shows a common scenario in which the `var` keyword is required: a query expression that produces a sequence of anonymous types. In this scenario, both the query variable and the iteration variable in the `foreach` statement must be implicitly typed by using `var` because you do not have access to a type name for the anonymous type. For more information about anonymous types, see [Anonymous Types](../../fundamentals/types/anonymous-types.md). [!code-csharp[csProgGuideLINQ#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideLINQ/CS/csRef30LangFeatures_2.cs#32)] -## Example - The following example uses the `var` keyword in a situation that is similar, but in which the use of `var` is optional. Because `student.LastName` is a string, execution of the query returns a sequence of strings. Therefore, the type of `queryID` could be declared as `System.Collections.Generic.IEnumerable` instead of `var`. Keyword `var` is used for convenience. In the example, the iteration variable in the `foreach` statement is explicitly typed as a string, but it could instead be declared by using `var`. Because the type of the iteration variable is not an anonymous type, the use of `var` is an option, not a requirement. Remember, `var` itself is not a type, but an instruction to the compiler to infer and assign the type. [!code-csharp[csProgGuideLINQ#33](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideLINQ/CS/csRef30LangFeatures_2.cs#33)] diff --git a/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md b/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md index d972de80dcb22..a280472aaa5e6 100644 --- a/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md +++ b/docs/csharp/programming-guide/types/how-to-convert-a-byte-array-to-an-int.md @@ -26,7 +26,7 @@ This example shows you how to use the class to conver |`uint`|[ToUInt32(Byte\[\], Int32)](xref:System.BitConverter.ToUInt32(System.Byte[],System.Int32))| |`ulong`|[ToUInt64(Byte\[\], Int32)](xref:System.BitConverter.ToUInt64(System.Byte[],System.Int32))| -## Example +## Examples This example initializes an array of bytes, reverses the array if the computer architecture is little-endian (that is, the least significant byte is stored first), and then calls the [ToInt32(Byte\[\], Int32)](xref:System.BitConverter.ToInt32(System.Byte[],System.Int32)) method to convert four bytes in the array to an `int`. The second argument to [ToInt32(Byte\[\], Int32)](xref:System.BitConverter.ToInt32(System.Byte[],System.Int32)) specifies the start index of the array of bytes. @@ -35,8 +35,6 @@ This example initializes an array of bytes, reverses the array if the computer a [!code-csharp[csProgGuideTypes#22](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#22)] -## Example - In this example, the method of the class is called to convert an `int` to an array of bytes. > [!NOTE] diff --git a/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md b/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md index fb44f4717aeee..038c32a1717d8 100644 --- a/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md +++ b/docs/csharp/programming-guide/types/how-to-convert-between-hexadecimal-strings-and-numeric-types.md @@ -25,38 +25,28 @@ These examples show you how to perform the following tasks: - Convert a [byte](../../language-reference/builtin-types/integral-numeric-types.md) array to a hexadecimal `string`. -## Example +## Examples This example outputs the hexadecimal value of each character in a `string`. First it parses the `string` to an array of characters. Then it calls on each character to obtain its numeric value. Finally, it formats the number as its hexadecimal representation in a `string`. [!code-csharp[csProgGuideTypes#30](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#30)] -## Example - This example parses a `string` of hexadecimal values and outputs the character corresponding to each hexadecimal value. First it calls the [Split(Char\[\])](xref:System.String.Split(System.Char[])) method to obtain each hexadecimal value as an individual `string` in an array. Then it calls to convert the hexadecimal value to a decimal value represented as an [int](../../language-reference/builtin-types/integral-numeric-types.md). It shows two different ways to obtain the character corresponding to that character code. The first technique uses , which returns the character corresponding to the integer argument as a `string`. The second technique explicitly casts the `int` to a [char](../../language-reference/builtin-types/char.md). [!code-csharp[csProgGuideTypes#31](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#31)] -## Example - This example shows another way to convert a hexadecimal `string` to an integer, by calling the method. [!code-csharp[csProgGuideTypes#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#32)] -## Example - The following example shows how to convert a hexadecimal `string` to a [float](../../language-reference/builtin-types/floating-point-numeric-types.md) by using the class and the method. [!code-csharp[csProgGuideTypes#39](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#39)] -## Example - The following example shows how to convert a [byte](../../language-reference/builtin-types/integral-numeric-types.md) array to a hexadecimal string by using the class. [!code-csharp[csProgGuideTypes#38](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#38)] -## Example - The following example shows how to convert a [byte](../../language-reference/builtin-types/integral-numeric-types.md) array to a hexadecimal string by calling the method introduced in .NET 5.0. [!code-csharp[csProgGuideTypes#47](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#47)] From 16981fff1b34c7804bac32c2dc0ed41c14dea612 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 17:05:31 -0400 Subject: [PATCH 45/60] Apply suggestions from code review Co-authored-by: Tom Dykstra Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com> --- .../fundamentals/program-structure/index.md | 4 ++-- .../program-structure/main-command-line.md | 18 +++++++++--------- .../program-structure/top-level-statements.md | 2 +- docs/csharp/fundamentals/types/index.md | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index a2ca1ad40fd77..c06e01339c29a 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -8,7 +8,7 @@ ms.assetid: 5ae964a5-0ef0-40fe-88fb-6d1793371d0d --- # General Structure of a C# Program -C# programs consists of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements. +C# programs consist of one or more files. Each file contains zero or more namespaces. A namespace contains types such as classes, structs, interfaces, enumerations, and delegates, or other namespaces. The following example is the skeleton of a C# program that contains all of these elements. :::code language="csharp" source="snippets/toplevel-structure/Program.cs"::: @@ -18,7 +18,7 @@ The preceding example uses *top-level statements* for the program's entry point. ## Related Sections -You learned about these program elements in the [types](../types/index.md) section of the fundamentals guide: +You learn about these program elements in the [types](../types/index.md) section of the fundamentals guide: - [Classes](../types/classes.md) - [Structs](../../language-reference/builtin-types/struct.md) diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index 185096d37189e..0d5dfd8e0c8d6 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -35,7 +35,7 @@ For information about how to write application code with an implicit entry point - `Main` is declared inside a class or struct. `Main` must be [`static`](../../language-reference/keywords/static.md) and it need not be [`public`](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [`private`](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. >>>>>>> cd631f0b85 (edit program structure) - `Main` can either have a `void`, `int`, or, starting with C# 7.1, `Task`, or `Task` return type. -- If and only if `Main` returns a `Task` or `Task`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. Note that this specifically excludes an `async void Main` method. +- If and only if `Main` returns a `Task` or `Task`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. This specifically excludes an `async void Main` method. - The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the method to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the method. The following list shows valid `Main` signatures: @@ -51,7 +51,7 @@ public static async Task Main(string[] args) { } public static async Task Main(string[] args) { } ``` -The preceding examples all use the `public` accessor modifier. That is typical, but not required. +The preceding examples all use the `public` accessor modifier. That's typical, but not required. The addition of `async` and `Task`, `Task` return types simplifies program code when console applications need to start and `await` asynchronous operations in `Main`. @@ -107,14 +107,14 @@ if ($LastExitCode -eq 0) { Write-Host "Return value = " $LastExitCode ``` -```txt +```output Execution succeeded Return value = 0 ``` ### Async Main return values -Async Main return values move the boilerplate code necessary for calling asynchronous methods in `Main` to code generated by the compiler. Previously, you would need to write this construct to call asynchronous code and ensure your program ran until the asynchronous operation completed: +When you declare an `async` return value for `Main`, the compiler generates the boilerplate code for calling asynchronous methods in `Main`. If you don't specify the `async` keyword, you need to write that code yourself, as shown in the following example. The code in the example ensures that your program runs until the asynchronous operation is completed: ```csharp public static void Main() @@ -129,11 +129,11 @@ private static async Task AsyncConsoleWork() } ``` -Now, this can be replaced by: +This boilerplate code can be replaced by: -:::code language="csharp" source="snippets/main-arguments/Program.cs"::: +:::code language="csharp" source="snippets/main-arguments/Program.cs" id="AsyncMain"::: -The advantage of the new syntax is that the compiler always generates the correct code. +An advantage of declaring `Main` as `async` is that the compiler always generates the correct code. When the application entry point returns a `Task` or `Task`, the compiler generates a new entry point that calls the entry point method declared in the application code. Assuming that this entry point is called `$GeneratedMain`, the compiler generates the following code for these entry points: @@ -166,14 +166,14 @@ If the arguments are not used, you can omit `args` from the method signature for | Return value, uses `await` | `static async Task Main()` | > [!NOTE] -> To enable command-line arguments in the `Main` method in a Windows Forms application, you must manually modify the signature of `Main` in *program.cs*. The code generated by the Windows Forms designer creates a `Main` without an input parameter. You can also use or to access the command-line arguments from any point in a console or Windows application. +> You can also use or to access the command-line arguments from any point in a console or Windows Forms application. To enable command-line arguments in the `Main` method signature in a Windows Forms application, you must manually modify the signature of `Main`. The code generated by the Windows Forms designer creates `Main` without an input parameter. The parameter of the `Main` method is a array that represents the command-line arguments. Usually you determine whether arguments exist by testing the `Length` property, for example: :::code language="csharp" source="snippets/main-command-line/Program.cs" ID="Snippet4"::: > [!TIP] -> The `args` array cannot be null. So, it's safe to access the `Length` property without null checking. +> The `args` array can't be null. So, it's safe to access the `Length` property without null checking. You can also convert the string arguments to numeric types by using the class or the `Parse` method. For example, the following statement converts the `string` to a `long` number by using the method: diff --git a/docs/csharp/fundamentals/program-structure/top-level-statements.md b/docs/csharp/fundamentals/program-structure/top-level-statements.md index 0bfd95fbc1d1d..a9b279fae9213 100644 --- a/docs/csharp/fundamentals/program-structure/top-level-statements.md +++ b/docs/csharp/fundamentals/program-structure/top-level-statements.md @@ -6,7 +6,7 @@ helpviewer_keywords: - "C# language, top-level statements" - "C# language, Main method" --- -# Top-level statements - programs without Main methods +# Top-level statements - programs without `Main` methods Starting in C# 9, you don't have to explicitly include a `Main` method in a console application project. Instead, you can use the *top-level statements* feature to minimize the code you have to write. In this case, the compiler generates a class and `Main` method entry point for the application. diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 978408c00982f..9dfb8d7c77dc5 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -142,7 +142,7 @@ It can be inconvenient to create a named type for simple sets of related values Ordinary value types can't have a value of [`null`](../../language-reference/keywords/null.md). However, you can create *nullable value types* by appending a `?` after the type. For example, `int?` is an `int` type that can also have the value [`null`](../../language-reference/keywords/null.md). Nullable value types are instances of the generic struct type . Nullable value types are especially useful when you're passing data to and from databases in which numeric values might be `null`. For more information, see [Nullable value types](../../language-reference/builtin-types/nullable-value-types.md). -## Compile-time type and runtime type +## Compile-time type and run-time type A variable can have different compile-time and run-time types. The *compile-time type* is the declared or inferred type of the variable in the source code. The *run-time type* is the type of the instance referred to by that variable. Often those two types are the same, as in the following example: From 143331e6d59ecc6b6392a6f71226f0386fb46eb3 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 17:06:17 -0400 Subject: [PATCH 46/60] respond to feedback --- .openpublishing.redirection.json | 2 +- docs/core/introduction.md | 2 +- docs/csharp/fundamentals/functional/deconstruct.md | 13 ++++++------- docs/csharp/fundamentals/functional/discards.md | 1 - .../functional/tutorials/pattern-matching.md | 1 - .../object-oriented/tutorials/inheritance.md | 1 - 6 files changed, 8 insertions(+), 12 deletions(-) diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 21593d4aab5d8..3cbfde9357679 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -1799,7 +1799,7 @@ }, { "source_path": "docs/csharp/pattern-matching.md", - "redirect_url": "/dotnet/csharp/fundamentals/functiona/pattern-matching" + "redirect_url": "/dotnet/csharp/fundamentals/functional/pattern-matching" }, { "source_path": "docs/csharp/programming-guide/arrays/arrays-as-objects.md", diff --git a/docs/core/introduction.md b/docs/core/introduction.md index 8449e35408e36..9ac06ae0790fb 100644 --- a/docs/core/introduction.md +++ b/docs/core/introduction.md @@ -91,7 +91,7 @@ For more information, see [Releases and support for .NET Core and .NET 5](releas Here are some of the capabilities that .NET languages support: * [Type safety](../standard/base-types/common-type-system.md) -* Type inference - [C#](/dotnet/csharp/fundamentals/types#specifying-types-in-variable-declarations), [F#](../fsharp/language-reference/type-inference.md), [Visual Basic](../visual-basic/programming-guide/language-features/variables/local-type-inference.md) +* Type inference - [C#](../csharp/fundamentals/types#specifying-types-in-variable-declarations), [F#](../fsharp/language-reference/type-inference.md), [Visual Basic](../visual-basic/programming-guide/language-features/variables/local-type-inference.md) * [Generic types](../standard/generics.md) * [Delegates](../standard/delegates-lambdas.md) * [Lambdas](../standard/delegates-lambdas.md) diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index dfc26767433be..2c2dae790ac4c 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -1,7 +1,6 @@ --- title: Deconstructing tuples and other types description: Learn how to deconstruct tuples and other types. -ms.technology: csharp-fundamentals ms.date: 03/22/2021 --- # Deconstructing tuples and other types @@ -14,7 +13,7 @@ Retrieving multiple field and property values from an object can be equally cumb In C# 7.0 and later, you can retrieve multiple elements from a tuple or retrieve multiple field, property, and computed values from an object in a single *deconstruct* operation. To deconstruct a tuple, you assign its elements to individual variables. When you deconstruct an object, you assign selected values to individual variables. -## Deconstructing a tuple +## Tuples C# features built-in support for deconstructing tuples, which lets you unpackage all the items in a tuple in a single operation. The general syntax for deconstructing a tuple is similar to the syntax for defining one: you enclose the variables to which each element is to be assigned in parentheses in the left side of an assignment statement. For example, the following statement assigns the elements of a four-tuple to four separate variables: @@ -49,7 +48,7 @@ You must assign each element of the tuple to a variable. If you omit any element You can't mix declarations and assignments to existing variables on the left-hand side of a deconstruction. The compiler generates error CS8184, "a deconstruction can't mix declarations and expressions on the left-hand-side." when the members include newly declared and existing variables. -## Deconstructing tuple elements with discards +## Tuple elements with discards Often when deconstructing a tuple, you're interested in the values of only some elements. Starting with C# 7.0, you can take advantage of C#'s support for *discards*, which are write-only variables whose values you've chosen to ignore. A discard is chosen by an underscore character ("\_") in an assignment. You can discard as many values as you like; all are represented by the single discard, `_`. @@ -57,7 +56,7 @@ The following example illustrates the use of tuples with discards. The `QueryCit :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple1.cs"::: -## Deconstructing user-defined types +## User-defined types C# doesn't offer built-in support for deconstructing non-tuple types other than the [`record`](#deconstructing-a-record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: @@ -77,7 +76,7 @@ The following example overloads the `Deconstruct` method to return various combi Multiple `Deconstruct` methods having the same number of parameters are ambiguous. You must be careful to define `Deconstruct` methods with different numbers of parameters, or "arity". `Deconstruct` methods with the same number of parameters cannot be distinguished during overload resolution. -## Deconstructing a user-defined type with discards +## User-defined type with discards Just as you do with [tuples](#deconstructing-tuple-elements-with-discards), you can use discards to ignore selected items returned by a `Deconstruct` method. Each discard is defined by a variable named "\_", and a single deconstruction operation can include multiple discards. @@ -85,7 +84,7 @@ The following example deconstructs a `Person` object into four strings (the firs :::code language="csharp" source="./snippets/deconstructing-tuples/class-discard1.cs" ID="Snippet1"::: -## Deconstructing a user-defined type with an extension method +## Extension methods for user-defined types If you didn't author a class, struct, or interface, you can still deconstruct objects of that type by implementing one or more `Deconstruct` [extension methods](../../programming-guide/classes-and-structs/extension-methods.md) to return the values in which you're interested. @@ -93,7 +92,7 @@ The following example defines two `Deconstruct` extension methods for the Date: Mon, 17 May 2021 17:14:44 -0400 Subject: [PATCH 47/60] fix broken links. --- ...ely-cast-using-pattern-matching-is-and-as-operators.md | 2 +- docs/csharp/language-reference/keywords/switch.md | 2 +- docs/csharp/toc.yml | 8 -------- docs/csharp/tour-of-csharp/features.md | 2 +- docs/csharp/whats-new/csharp-7.md | 2 +- .../tutorials/mixins-with-default-interface-methods.md | 2 +- docs/fundamentals/code-analysis/style-rules/ide0019.md | 4 ++-- .../code-analysis/style-rules/ide0020-ide0038.md | 4 ++-- .../style-rules/pattern-matching-preferences.md | 2 +- 9 files changed, 10 insertions(+), 18 deletions(-) diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md index ecb45bc5c38a7..a981e6461d53c 100644 --- a/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md +++ b/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md @@ -9,7 +9,7 @@ helpviewer_keywords: --- # How to safely cast by using pattern matching and the is and as operators -Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](/dotnet/csharp/fundamentals/types). To access the derived type's instance members, it's necessary to [cast](../../../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../../../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../../../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. +Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](/dotnet/csharp/fundamentals/types). To access the derived type's instance members, it's necessary to [cast](../../../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](/dotnet/csharp/fundamentals/functional/pattern-matching) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../../../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../../../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. The following example shows how to use the pattern matching `is` statement: diff --git a/docs/csharp/language-reference/keywords/switch.md b/docs/csharp/language-reference/keywords/switch.md index 7befd29f3fb99..457ae5cfb38e5 100644 --- a/docs/csharp/language-reference/keywords/switch.md +++ b/docs/csharp/language-reference/keywords/switch.md @@ -201,4 +201,4 @@ For more information, see [The switch statement](~/_csharplang/spec/statements.m - [C# Programming Guide](../../programming-guide/index.md) - [C# Keywords](index.md) - [if-else](if-else.md) -- [Pattern Matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) +- [Pattern Matching](/dotnet/csharp/fundamentals/functional/pattern-matching) diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 8bfe3f5333a6c..8d8705ed67ab1 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -208,16 +208,12 @@ items: href: nullable-references.md - name: Choose a strategy for enabling nullable reference types href: nullable-migration-strategies.md - - name: Deconstructing tuples and other types - href: /dotnet/csharp/fundamentals/functional/deconstruct - name: Methods href: methods.md - name: Properties href: properties.md - name: Indexers href: indexers.md - - name: Discards - href: /dotnet/csharp/fundamentals/functional/discards - name: Iterators href: iterators.md - name: Delegates & events @@ -280,8 +276,6 @@ items: href: linq/handle-null-values-in-query-expressions.md - name: Handle exceptions in query expressions href: linq/handle-exceptions-in-query-expressions.md - - name: Pattern Matching - href: /dotnet/csharp/fundamentals/functiona/pattern-matching - name: Write safe, efficient code href: write-safe-efficient-code.md - name: Expression trees @@ -323,8 +317,6 @@ items: href: how-to/modify-string-contents.md - name: Compare strings href: how-to/compare-strings.md - - name: Safely cast using pattern matching and is/as operators - href: /dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md - name: "How to catch a non-CLS exception" href: how-to/how-to-catch-a-non-cls-exception.md - name: The .NET Compiler Platform SDK (Roslyn APIs) diff --git a/docs/csharp/tour-of-csharp/features.md b/docs/csharp/tour-of-csharp/features.md index 0e8f234c19965..afd622607cc1c 100644 --- a/docs/csharp/tour-of-csharp/features.md +++ b/docs/csharp/tour-of-csharp/features.md @@ -58,7 +58,7 @@ An interpolated string is declared using the `$` token. String interpolation eva ## Pattern matching -The C# language provides [***pattern matching***](/dotnet/csharp/fundamentals/functiona/pattern-matching) expressions to query the state of an object and execute code based on that state. You can inspect types and the values of properties and fields to determine which action to take. The `switch` expression is the primary expression for pattern matching. +The C# language provides [***pattern matching***](/dotnet/csharp/fundamentals/functional/pattern-matching) expressions to query the state of an object and execute code based on that state. You can inspect types and the values of properties and fields to determine which action to take. The `switch` expression is the primary expression for pattern matching. ## Delegates and lambda expressions diff --git a/docs/csharp/whats-new/csharp-7.md b/docs/csharp/whats-new/csharp-7.md index 6b7f3e9db6634..154e0ea07445c 100644 --- a/docs/csharp/whats-new/csharp-7.md +++ b/docs/csharp/whats-new/csharp-7.md @@ -194,7 +194,7 @@ public static int SumPositiveNumbers(IEnumerable sequence) Beginning with C# 7.1, the pattern expression for `is` and the `switch` type pattern may have the type of a generic type parameter. This can be most useful when checking types that may be either `struct` or `class` types, and you want to avoid boxing. -You can learn more about pattern matching in [Pattern Matching in C#](/dotnet/csharp/fundamentals/functiona/pattern-matching). +You can learn more about pattern matching in [Pattern Matching in C#](/dotnet/csharp/fundamentals/functional/pattern-matching). ## Async main diff --git a/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md b/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md index c13b9baff8e07..d358eb14927ad 100644 --- a/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md +++ b/docs/csharp/whats-new/tutorials/mixins-with-default-interface-methods.md @@ -97,7 +97,7 @@ The `HalogenLight` you created earlier doesn't support blinking. So, don't add t ## Detect the light types using pattern matching -Next, let's write some test code. You can make use of C#'s [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) feature to determine a light's capabilities by examining which interfaces it supports. The following method exercises the supported capabilities of each light: +Next, let's write some test code. You can make use of C#'s [pattern matching](/dotnet/csharp/fundamentals/functional/pattern-matching) feature to determine a light's capabilities by examining which interfaces it supports. The following method exercises the supported capabilities of each light: [!code-csharp[Test a light's capabilities](./snippets/mixins-with-default-interface-methods/Program.cs?name=SnippetTestLightFunctions)] diff --git a/docs/fundamentals/code-analysis/style-rules/ide0019.md b/docs/fundamentals/code-analysis/style-rules/ide0019.md index f92a403bbec89..809af76c6bddd 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0019.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0019.md @@ -26,7 +26,7 @@ dev_langs: ## Overview -This style rule concerns the use of C# [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) over an `as` expression followed by a `null` check. +This style rule concerns the use of C# [pattern matching](/dotnet/csharp/fundamentals/functional/pattern-matching) over an `as` expression followed by a `null` check. ## csharp_style_pattern_matching_over_as_with_null_check @@ -49,7 +49,7 @@ if (s != null) {...} ## See also -- [Pattern matching in C#](/dotnet/csharp/fundamentals/functiona/pattern-matching) +- [Pattern matching in C#](/dotnet/csharp/fundamentals/functional/pattern-matching) - [Pattern matching preferences](pattern-matching-preferences.md) - [Code style language rules](language-rules.md) - [Code style rules reference](index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md b/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md index ca01b38631ba7..180349384dbcb 100644 --- a/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md +++ b/docs/fundamentals/code-analysis/style-rules/ide0020-ide0038.md @@ -28,7 +28,7 @@ dev_langs: ## Overview -This style rule concerns the use of C# [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) over an `is` check followed by a cast. For example, recommending use of `o is int i` instead of `if (o is int) { ... (int)o ...`. The option value determines if pattern matching is preferred or `is` check followed by a cast is preferred. Different rule IDs are used based on whether or not the cast expression is saved into a separate local variable in original code: +This style rule concerns the use of C# [pattern matching](/dotnet/csharp/fundamentals/functional/pattern-matching) over an `is` check followed by a cast. For example, recommending use of `o is int i` instead of `if (o is int) { ... (int)o ...`. The option value determines if pattern matching is preferred or `is` check followed by a cast is preferred. Different rule IDs are used based on whether or not the cast expression is saved into a separate local variable in original code: - IDE0020: Cast expression _is_ saved into a local variable. For example, original code is `if (o is int) { var i = (int)o; }`, which saves the result of `(int)o` in a local variable. - IDE0038: Cast expression _is not_ saved into a local variable. For example, original code is `if (o is int) { if ((int)o == 1) { ... } }`, which does not save the result of `(int)o` into a local variable. @@ -53,7 +53,7 @@ if (o is int) {var i = (int)o; ... } ## See also -- [Pattern matching in C#](/dotnet/csharp/fundamentals/functiona/pattern-matching) +- [Pattern matching in C#](/dotnet/csharp/fundamentals/functional/pattern-matching) - [Pattern matching preferences](pattern-matching-preferences.md) - [Code style language rules](language-rules.md) - [Code style rules reference](index.md) diff --git a/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md b/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md index 7a12407409c00..1d532a56bf5e2 100644 --- a/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md +++ b/docs/fundamentals/code-analysis/style-rules/pattern-matching-preferences.md @@ -12,7 +12,7 @@ dev_langs: ## C# preferences -The style rules in this section concern the use of [pattern matching](/dotnet/csharp/fundamentals/functiona/pattern-matching) in C#. +The style rules in this section concern the use of [pattern matching](/dotnet/csharp/fundamentals/functional/pattern-matching) in C#. - [Use pattern matching to avoid `as` followed by a `null` check (IDE0019](ide0019.md) - [Use pattern matching to avoid `is` check followed by a cast (IDE0020 and IDE0038)](ide0020-ide0038.md) From c3f53fdde623e59d6445a1df0293b5cdc19cfd43 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 17:44:09 -0400 Subject: [PATCH 48/60] respond to feedback. --- docs/csharp/fundamentals/program-structure/index.md | 2 ++ .../program-structure/main-command-line.md | 11 ++++------- .../program-structure/snippets/structure/Program.cs | 2 +- .../how-to-display-command-line-arguments.md | 4 ++-- docs/csharp/toc.yml | 4 ++-- .../VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs | 2 +- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index c06e01339c29a..e9269beedd864 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -24,6 +24,8 @@ You learn about these program elements in the [types](../types/index.md) section - [Structs](../../language-reference/builtin-types/struct.md) - [Namespaces](../types/namespaces.md) - [Interfaces](../types/interfaces.md) +- [Enums](../../language-reference/builtin-types/enum.md) +- [Delegates](../delegates-overview.md) ## C# Language Specification diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index 0d5dfd8e0c8d6..bac24de08e087 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -81,7 +81,7 @@ The following example shows how the exit code for the process can be accessed. This example uses [.NET Core](../../../core/introduction.md) command-line tools. If you are unfamiliar with .NET Core command-line tools, you can learn about them in this [get-started article](../../../core/tutorials/with-visual-studio-code.md). -Modify the `Main` method in *program.cs* as follows: +Create a new application by running `dotnet new console`. Modify the `Main` method in *Program.cs* as follows: :::code language="csharp" source="snippets/main-command-line/MainReturnValTest.cs"::: @@ -93,11 +93,8 @@ Next, create a PowerShell script to run the application and display the result. Because the code returns zero, the batch file will report success. However, if you change MainReturnValTest.cs to return a non-zero value and then recompile the program, subsequent execution of the PowerShell script will report failure. -```dotnetcli -dotnet run -``` - ```powershell +dotnet run if ($LastExitCode -eq 0) { Write-Host "Execution succeeded" } else @@ -207,13 +204,13 @@ To compile and run the application from a command prompt, follow these steps: 3. Enter the following command to compile the application. - `csc Factorial.cs` + `dotnet build` If your application has no compilation errors, an executable file that's named *Factorial.exe* is created. 4. Enter the following command to calculate the factorial of 3: - `Factorial 3` + `dotnet run -- 3` 5. The command produces this output: `The factorial of 3 is 6.` diff --git a/docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs b/docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs index 3797a45f35c38..f76ef8317270f 100644 --- a/docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs +++ b/docs/csharp/fundamentals/program-structure/snippets/structure/Program.cs @@ -27,7 +27,7 @@ struct YourStruct } } - class YourMainClass + class Program { static void Main(string[] args) { diff --git a/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md b/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md index 98a05d1cab941..e6e9649053e99 100644 --- a/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md +++ b/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md @@ -1,5 +1,5 @@ --- -title: "How to display command-line arguments - C# Programming Guide" +title: "How to display command-line arguments" description: Learn how to display command-line arguments. See a code example and view additional available resources. ms.date: 03/08/2021 ms.topic: how-to @@ -7,7 +7,7 @@ helpviewer_keywords: - "command-line arguments [C#], displaying" ms.assetid: b8479f2d-9e05-4d38-82da-2e61246e5437 --- -# How to display command-line arguments (C# Programming Guide) +# How to display command-line arguments Arguments provided to an executable on the command line are accessible in [top-level statements](../top-level-statements.md) or through an optional parameter to `Main`. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable: diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 8d8705ed67ab1..5f8542e559295 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -41,10 +41,10 @@ items: items: - name: Overview href: fundamentals/program-structure/index.md - - name: Top-level statements - href: fundamentals/program-structure/top-level-statements.md - name: Main method href: fundamentals/program-structure/main-command-line.md + - name: Top-level statements + href: fundamentals/program-structure/top-level-statements.md - name: Tutorials items: - name: "How to display command-line arguments" diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs index 90265dba85643..2e32bf463248d 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs @@ -2,7 +2,7 @@ { // - // Add a using directive for System if the directive isn't already present. + using System; public class Functions { From b139af36c2fa13ae6b508e4f8b3c6a2e337e86e1 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 17:59:48 -0400 Subject: [PATCH 49/60] fix warnings --- docs/core/introduction.md | 2 +- docs/csharp/fundamentals/functional/deconstruct.md | 4 ++-- docs/csharp/fundamentals/functional/discards.md | 4 ++-- docs/csharp/fundamentals/program-structure/index.md | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/core/introduction.md b/docs/core/introduction.md index 9ac06ae0790fb..94c97859897a0 100644 --- a/docs/core/introduction.md +++ b/docs/core/introduction.md @@ -91,7 +91,7 @@ For more information, see [Releases and support for .NET Core and .NET 5](releas Here are some of the capabilities that .NET languages support: * [Type safety](../standard/base-types/common-type-system.md) -* Type inference - [C#](../csharp/fundamentals/types#specifying-types-in-variable-declarations), [F#](../fsharp/language-reference/type-inference.md), [Visual Basic](../visual-basic/programming-guide/language-features/variables/local-type-inference.md) +* Type inference - [C#](../csharp/fundamentals/types/index.md#specifying-types-in-variable-declarations), [F#](../fsharp/language-reference/type-inference.md), [Visual Basic](../visual-basic/programming-guide/language-features/variables/local-type-inference.md) * [Generic types](../standard/generics.md) * [Delegates](../standard/delegates-lambdas.md) * [Lambdas](../standard/delegates-lambdas.md) diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index 2c2dae790ac4c..be086a8049691 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -58,7 +58,7 @@ The following example illustrates the use of tuples with discards. The `QueryCit ## User-defined types -C# doesn't offer built-in support for deconstructing non-tuple types other than the [`record`](#deconstructing-a-record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: +C# doesn't offer built-in support for deconstructing non-tuple types other than the [`record`](#record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple2.cs" ID="Snippet1"::: @@ -78,7 +78,7 @@ Multiple `Deconstruct` methods having the same number of parameters are ambiguou ## User-defined type with discards -Just as you do with [tuples](#deconstructing-tuple-elements-with-discards), you can use discards to ignore selected items returned by a `Deconstruct` method. Each discard is defined by a variable named "\_", and a single deconstruction operation can include multiple discards. +Just as you do with [tuples](#tuple-elements-with-discards), you can use discards to ignore selected items returned by a `Deconstruct` method. Each discard is defined by a variable named "\_", and a single deconstruction operation can include multiple discards. The following example deconstructs a `Person` object into four strings (the first and last names, the city, and the state) but discards the last name and the state. diff --git a/docs/csharp/fundamentals/functional/discards.md b/docs/csharp/fundamentals/functional/discards.md index 7bf3f15250ca4..9c51dcaef8ed2 100644 --- a/docs/csharp/fundamentals/functional/discards.md +++ b/docs/csharp/fundamentals/functional/discards.md @@ -25,13 +25,13 @@ Discards are useful in working with tuples when your application code uses some :::code language="csharp" source="snippets/discards/discard-tuple.cs" ID="DiscardTupleMember" ::: -For more information on deconstructing tuples with discards, see [Deconstructing tuples and other types](deconstruct.md#deconstructing-tuple-elements-with-discards). +For more information on deconstructing tuples with discards, see [Deconstructing tuples and other types](deconstruct.md#tuple-elements-with-discards). The `Deconstruct` method of a class, structure, or interface also allows you to retrieve and deconstruct a specific set of data from an object. You can use discards when you're interested in working with only a subset of the deconstructed values. The following example deconstructs a `Person` object into four strings (the first and last names, the city, and the state), but discards the last name and the state. :::code language="csharp" source="snippets/discards/discard-class.cs" ::: -For more information on deconstructing user-defined types with discards, see [Deconstructing tuples and other types](deconstruct.md#deconstructing-a-user-defined-type-with-discards). +For more information on deconstructing user-defined types with discards, see [Deconstructing tuples and other types](deconstruct.md#user-defined-type-with-discards). ## Pattern matching with `switch` diff --git a/docs/csharp/fundamentals/program-structure/index.md b/docs/csharp/fundamentals/program-structure/index.md index e9269beedd864..c841ea469d3bc 100644 --- a/docs/csharp/fundamentals/program-structure/index.md +++ b/docs/csharp/fundamentals/program-structure/index.md @@ -25,7 +25,7 @@ You learn about these program elements in the [types](../types/index.md) section - [Namespaces](../types/namespaces.md) - [Interfaces](../types/interfaces.md) - [Enums](../../language-reference/builtin-types/enum.md) -- [Delegates](../delegates-overview.md) +- [Delegates](../../delegates-overview.md) ## C# Language Specification From 3c66c9ddcb5bca0ca7bdd80f61c01b332892728e Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 18:23:22 -0400 Subject: [PATCH 50/60] move fundamentals tutorials up one level. Then, fix all links. --- .openpublishing.redirection.json | 10 +-- docs/core/whats-new/dotnet-core-3-0.md | 2 +- .../fundamentals/functional/deconstruct.md | 2 +- .../tutorials/classes.md | 0 .../how-to-display-command-line-arguments.md | 0 .../tutorials/inheritance.md | 0 .../media/inheritance/book-class.jpg | Bin .../media/inheritance/publication-class.jpg | Bin .../{object-oriented => }/tutorials/oop.md | 0 .../tutorials/pattern-matching.md | 0 ...ng-pattern-matching-is-and-as-operators.md | 0 .../tutorials/snippets/inheritance/Program.cs | 0 .../snippets/inheritance/base-and-derived.cs | 0 .../tutorials/snippets/inheritance/basics.cs | 0 .../snippets/inheritance/inheritance.csproj | 0 .../tutorials/snippets/inheritance/is-a.cs | 0 .../tutorials/snippets/inheritance/private.cs | 0 .../tutorials/snippets/inheritance/shape.cs | 0 .../snippets/inheritance/simpleclass.cs | 0 .../snippets/inheritance/simpleclass2.cs | 0 .../snippets/inheritance/use-publication.cs | 0 .../introduction-to-classes/BankAccount.cs | 0 .../introduction-to-classes/Program.cs | 0 .../introduction-to-classes/classes.csproj | 0 .../introduction-to-classes/transaction.cs | 0 .../BankAccount.cs | 0 .../GiftCardAccount.cs | 0 .../InterestEarningAccount.cs | 0 .../LineOfCreditAccount.cs | 0 .../object-oriented-programming/Program.cs | 0 .../object-oriented-programming.csproj | 0 .../transaction.cs | 0 .../toll-calculator/ExternalSystems.cs | 0 .../finished/toll-calculator/Program.cs | 0 .../toll-calculator/TollCalculator.cs | 0 .../toll-calculator/toll-calculator.csproj | 0 .../start/toll-calculator/ExternalSystems.cs | 0 .../patterns/start/toll-calculator/Program.cs | 0 .../toll-calculator/toll-calculator.csproj | 0 .../snippets/safelycast/asandis/Program.cs | 0 .../safelycast/asandis/asandis.csproj | 0 .../nullablepatternmatching/Program.cs | 0 .../nullablepatternmatching.csproj | 0 .../safelycast/patternmatching/Program.cs | 0 .../patternmatching/patternmatching.csproj | 0 docs/csharp/how-to/index.md | 4 +- .../builtin-types/reference-types.md | 2 +- .../csharp/language-reference/operators/is.md | 2 +- .../language-reference/operators/patterns.md | 4 +- .../operators/switch-expression.md | 2 +- .../operators/type-testing-and-cast.md | 2 +- .../types/casting-and-type-conversions.md | 2 +- docs/csharp/toc.yml | 67 ++++++++---------- docs/csharp/tour-of-csharp/features.md | 2 +- .../tutorials/arrays-and-collections.md | 2 +- docs/csharp/whats-new/csharp-8.md | 2 +- 56 files changed, 49 insertions(+), 56 deletions(-) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/classes.md (100%) rename docs/csharp/fundamentals/{program-structure => }/tutorials/how-to-display-command-line-arguments.md (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/inheritance.md (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/media/inheritance/book-class.jpg (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/media/inheritance/publication-class.jpg (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/oop.md (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/pattern-matching.md (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/Program.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/base-and-derived.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/basics.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/inheritance.csproj (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/is-a.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/private.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/shape.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/simpleclass.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/simpleclass2.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/inheritance/use-publication.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/introduction-to-classes/BankAccount.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/introduction-to-classes/Program.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/introduction-to-classes/classes.csproj (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/introduction-to-classes/transaction.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/object-oriented-programming/BankAccount.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/object-oriented-programming/GiftCardAccount.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/object-oriented-programming/InterestEarningAccount.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/object-oriented-programming/LineOfCreditAccount.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/object-oriented-programming/Program.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/object-oriented-programming/object-oriented-programming.csproj (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/object-oriented-programming/transaction.cs (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/snippets/patterns/finished/toll-calculator/ExternalSystems.cs (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/snippets/patterns/finished/toll-calculator/Program.cs (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/snippets/patterns/finished/toll-calculator/TollCalculator.cs (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/snippets/patterns/finished/toll-calculator/toll-calculator.csproj (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/snippets/patterns/start/toll-calculator/ExternalSystems.cs (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/snippets/patterns/start/toll-calculator/Program.cs (100%) rename docs/csharp/fundamentals/{functional => }/tutorials/snippets/patterns/start/toll-calculator/toll-calculator.csproj (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/safelycast/asandis/Program.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/safelycast/asandis/asandis.csproj (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/safelycast/nullablepatternmatching/Program.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/safelycast/nullablepatternmatching/nullablepatternmatching.csproj (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/safelycast/patternmatching/Program.cs (100%) rename docs/csharp/fundamentals/{object-oriented => }/tutorials/snippets/safelycast/patternmatching/patternmatching.csproj (100%) diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 3cbfde9357679..809d1fae44e88 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -989,7 +989,7 @@ }, { "source_path": "docs/csharp/how-to/safely-cast-using-pattern-matching-is-and-as-operators.md", - "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md" + "redirect_url": "/dotnet/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md" }, { @@ -2983,7 +2983,7 @@ }, { "source_path": "docs/csharp/programming-guide/main-and-command-args/how-to-display-command-line-arguments.md", - "redirect_url": "/dotnet/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments" + "redirect_url": "/dotnet/csharp/fundamentals/tutorials/how-to-display-command-line-arguments" }, { "source_path": "docs/csharp/programming-guide/main-and-command-args/index.md", @@ -3394,11 +3394,11 @@ }, { "source_path": "docs/csharp/tutorials/intro-to-csharp/introduction-to-classes.md", - "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/tutorials/classes" + "redirect_url": "/dotnet/csharp/fundamentals/tutorials/classes" }, { "source_path": "docs/csharp/tutorials/intro-to-csharp/object-oriented-programming.md", - "redirect_url": "/dotnet/csharp/fundamentals/object-oriented/tutorials/oop" + "redirect_url": "/dotnet/csharp/fundamentals/tutorials/oop" }, { "source_path": "docs/csharp/tutorials/microservices.md", @@ -3406,7 +3406,7 @@ }, { "source_path": "docs/csharp/tutorials/pattern-matching.md", - "redirect_url": "/dotnet/csharp/fundamentals/functional/tutorials/pattern-matching" + "redirect_url": "/dotnet/csharp/fundamentals/tutorials/pattern-matching" }, { "source_path": "docs/csharp/type-system.md", diff --git a/docs/core/whats-new/dotnet-core-3-0.md b/docs/core/whats-new/dotnet-core-3-0.md index 068d5e71f95df..700aeac269578 100644 --- a/docs/core/whats-new/dotnet-core-3-0.md +++ b/docs/core/whats-new/dotnet-core-3-0.md @@ -28,7 +28,7 @@ Tutorials related to C# 8.0 language features: - [Tutorial: Express your design intent more clearly with nullable and non-nullable reference types](../../csharp/whats-new/tutorials/nullable-reference-types.md) - [Tutorial: Generate and consume async streams using C# 8.0 and .NET Core 3.0](../../csharp/whats-new/tutorials/generate-consume-asynchronous-stream.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../csharp/fundamentals/functional/tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../csharp/fundamentals/tutorials/pattern-matching.md) Language enhancements were added to support the following API features detailed below: diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index be086a8049691..7c44f192fe892 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -58,7 +58,7 @@ The following example illustrates the use of tuples with discards. The `QueryCit ## User-defined types -C# doesn't offer built-in support for deconstructing non-tuple types other than the [`record`](#record-type) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: +C# doesn't offer built-in support for deconstructing non-tuple types other than the [`record`](#record-types) and [DictionaryEntry](xref:System.Collections.DictionaryEntry.Deconstruct%2A) types. However, as the author of a class, a struct, or an interface, you can allow instances of the type to be deconstructed by implementing one or more `Deconstruct` methods. The method returns void, and each value to be deconstructed is indicated by an [out](../../language-reference/keywords/out-parameter-modifier.md) parameter in the method signature. For example, the following `Deconstruct` method of a `Person` class returns the first, middle, and last name: :::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple2.cs" ID="Snippet1"::: diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/classes.md b/docs/csharp/fundamentals/tutorials/classes.md similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/classes.md rename to docs/csharp/fundamentals/tutorials/classes.md diff --git a/docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md b/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md similarity index 100% rename from docs/csharp/fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md rename to docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/inheritance.md b/docs/csharp/fundamentals/tutorials/inheritance.md similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/inheritance.md rename to docs/csharp/fundamentals/tutorials/inheritance.md diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/book-class.jpg b/docs/csharp/fundamentals/tutorials/media/inheritance/book-class.jpg similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/book-class.jpg rename to docs/csharp/fundamentals/tutorials/media/inheritance/book-class.jpg diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/publication-class.jpg b/docs/csharp/fundamentals/tutorials/media/inheritance/publication-class.jpg similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/media/inheritance/publication-class.jpg rename to docs/csharp/fundamentals/tutorials/media/inheritance/publication-class.jpg diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/oop.md b/docs/csharp/fundamentals/tutorials/oop.md similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/oop.md rename to docs/csharp/fundamentals/tutorials/oop.md diff --git a/docs/csharp/fundamentals/functional/tutorials/pattern-matching.md b/docs/csharp/fundamentals/tutorials/pattern-matching.md similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/pattern-matching.md rename to docs/csharp/fundamentals/tutorials/pattern-matching.md diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md b/docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md rename to docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/Program.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/Program.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/base-and-derived.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/base-and-derived.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/base-and-derived.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/basics.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/basics.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/basics.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/inheritance.csproj b/docs/csharp/fundamentals/tutorials/snippets/inheritance/inheritance.csproj similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/inheritance.csproj rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/inheritance.csproj diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/is-a.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/is-a.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/is-a.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/is-a.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/private.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/private.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/private.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/shape.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/shape.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/shape.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/shape.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/simpleclass.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/simpleclass.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/simpleclass2.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/simpleclass2.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/simpleclass2.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs b/docs/csharp/fundamentals/tutorials/snippets/inheritance/use-publication.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/inheritance/use-publication.cs rename to docs/csharp/fundamentals/tutorials/snippets/inheritance/use-publication.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/BankAccount.cs b/docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/BankAccount.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/BankAccount.cs rename to docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/BankAccount.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/Program.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/Program.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/classes.csproj b/docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/classes.csproj similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/classes.csproj rename to docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/classes.csproj diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/transaction.cs b/docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/transaction.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/introduction-to-classes/transaction.cs rename to docs/csharp/fundamentals/tutorials/snippets/introduction-to-classes/transaction.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/BankAccount.cs b/docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/BankAccount.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/BankAccount.cs rename to docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/BankAccount.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/GiftCardAccount.cs b/docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/GiftCardAccount.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/GiftCardAccount.cs rename to docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/GiftCardAccount.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/InterestEarningAccount.cs b/docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/InterestEarningAccount.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/InterestEarningAccount.cs rename to docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/InterestEarningAccount.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/LineOfCreditAccount.cs b/docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/LineOfCreditAccount.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/LineOfCreditAccount.cs rename to docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/LineOfCreditAccount.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/Program.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/Program.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/object-oriented-programming.csproj b/docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/object-oriented-programming.csproj similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/object-oriented-programming.csproj rename to docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/object-oriented-programming.csproj diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/transaction.cs b/docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/transaction.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/object-oriented-programming/transaction.cs rename to docs/csharp/fundamentals/tutorials/snippets/object-oriented-programming/transaction.cs diff --git a/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/ExternalSystems.cs b/docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/ExternalSystems.cs similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/ExternalSystems.cs rename to docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/ExternalSystems.cs diff --git a/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/Program.cs similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/Program.cs diff --git a/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/TollCalculator.cs b/docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/TollCalculator.cs similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/TollCalculator.cs rename to docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/TollCalculator.cs diff --git a/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/toll-calculator.csproj b/docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/toll-calculator.csproj similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/snippets/patterns/finished/toll-calculator/toll-calculator.csproj rename to docs/csharp/fundamentals/tutorials/snippets/patterns/finished/toll-calculator/toll-calculator.csproj diff --git a/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/ExternalSystems.cs b/docs/csharp/fundamentals/tutorials/snippets/patterns/start/toll-calculator/ExternalSystems.cs similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/ExternalSystems.cs rename to docs/csharp/fundamentals/tutorials/snippets/patterns/start/toll-calculator/ExternalSystems.cs diff --git a/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/patterns/start/toll-calculator/Program.cs similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/patterns/start/toll-calculator/Program.cs diff --git a/docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/toll-calculator.csproj b/docs/csharp/fundamentals/tutorials/snippets/patterns/start/toll-calculator/toll-calculator.csproj similarity index 100% rename from docs/csharp/fundamentals/functional/tutorials/snippets/patterns/start/toll-calculator/toll-calculator.csproj rename to docs/csharp/fundamentals/tutorials/snippets/patterns/start/toll-calculator/toll-calculator.csproj diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/safelycast/asandis/Program.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/safelycast/asandis/Program.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/asandis.csproj b/docs/csharp/fundamentals/tutorials/snippets/safelycast/asandis/asandis.csproj similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/asandis/asandis.csproj rename to docs/csharp/fundamentals/tutorials/snippets/safelycast/asandis/asandis.csproj diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/safelycast/nullablepatternmatching/Program.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/safelycast/nullablepatternmatching/Program.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/nullablepatternmatching.csproj b/docs/csharp/fundamentals/tutorials/snippets/safelycast/nullablepatternmatching/nullablepatternmatching.csproj similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/nullablepatternmatching/nullablepatternmatching.csproj rename to docs/csharp/fundamentals/tutorials/snippets/safelycast/nullablepatternmatching/nullablepatternmatching.csproj diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/safelycast/patternmatching/Program.cs similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/Program.cs rename to docs/csharp/fundamentals/tutorials/snippets/safelycast/patternmatching/Program.cs diff --git a/docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/patternmatching.csproj b/docs/csharp/fundamentals/tutorials/snippets/safelycast/patternmatching/patternmatching.csproj similarity index 100% rename from docs/csharp/fundamentals/object-oriented/tutorials/snippets/safelycast/patternmatching/patternmatching.csproj rename to docs/csharp/fundamentals/tutorials/snippets/safelycast/patternmatching/patternmatching.csproj diff --git a/docs/csharp/how-to/index.md b/docs/csharp/how-to/index.md index 638c413257927..6e05162950cc5 100644 --- a/docs/csharp/how-to/index.md +++ b/docs/csharp/how-to/index.md @@ -59,7 +59,7 @@ You may need to convert an object to a different type. - [Convert a string to a `DateTime`](../../standard/base-types/parsing-datetime.md). - [Convert a byte array to an int](../programming-guide/types/how-to-convert-a-byte-array-to-an-int.md). - [Convert a string to a number](../programming-guide/types/how-to-convert-a-string-to-a-number.md). -- [Use pattern matching, the `as` and `is` operators to safely cast to a different type](../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). +- [Use pattern matching, the `as` and `is` operators to safely cast to a different type](../fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). - [Define custom type conversions](../language-reference/operators/user-defined-conversion-operators.md). - [Determine if a type is a nullable value type](../language-reference/builtin-types/nullable-value-types.md#how-to-identify-a-nullable-value-type). - [Convert between nullable and non-nullable value types](../language-reference/builtin-types/nullable-value-types.md#conversion-from-a-nullable-value-type-to-an-underlying-type). @@ -133,4 +133,4 @@ to use these techniques. Typically, C# programs have command line arguments. These articles teach you to access and process those command line arguments. -- [Retrieve all command line arguments with `for`](../fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md). +- [Retrieve all command line arguments with `for`](../fundamentals/tutorials/how-to-display-command-line-arguments.md). diff --git a/docs/csharp/language-reference/builtin-types/reference-types.md b/docs/csharp/language-reference/builtin-types/reference-types.md index 60710b8adc2ec..8b0d104d5ba5e 100644 --- a/docs/csharp/language-reference/builtin-types/reference-types.md +++ b/docs/csharp/language-reference/builtin-types/reference-types.md @@ -172,7 +172,7 @@ The following example uses `dynamic` in several declarations. The `Main` method - [Basic String Operations](../../../standard/base-types/basic-string-operations.md) - [Creating New Strings](../../../standard/base-types/creating-new.md) - [Type-testing and cast operators](../operators/type-testing-and-cast.md) -- [How to safely cast using pattern matching and the as and is operators](../../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) +- [How to safely cast using pattern matching and the as and is operators](../../fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) - [Walkthrough: creating and using dynamic objects](../../programming-guide/types/walkthrough-creating-and-using-dynamic-objects.md) - - diff --git a/docs/csharp/language-reference/operators/is.md b/docs/csharp/language-reference/operators/is.md index faebce896894a..dd3e00a651bc9 100644 --- a/docs/csharp/language-reference/operators/is.md +++ b/docs/csharp/language-reference/operators/is.md @@ -51,5 +51,5 @@ For more information, see [The is operator](~/_csharplang/spec/expressions.md#th - [C# reference](../index.md) - [C# operators and expressions](index.md) - [Patterns](patterns.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/tutorials/pattern-matching.md) - [Type-testing and cast operators](../operators/type-testing-and-cast.md) diff --git a/docs/csharp/language-reference/operators/patterns.md b/docs/csharp/language-reference/operators/patterns.md index 95c3b7934579d..1408f6806a4a9 100644 --- a/docs/csharp/language-reference/operators/patterns.md +++ b/docs/csharp/language-reference/operators/patterns.md @@ -34,7 +34,7 @@ In those constructs, you can match an input expression against any of the follow [Logical](#logical-patterns), [property](#property-pattern), and [positional](#positional-pattern) patterns are *recursive* patterns. That is, they can contain *nested* patterns. -For the example of how to use those patterns to build a data-driven algorithm, see [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md). +For the example of how to use those patterns to build a data-driven algorithm, see [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/tutorials/pattern-matching.md). ## Declaration and type patterns @@ -244,4 +244,4 @@ For more information, see the following feature proposal notes: - [C# reference](../index.md) - [C# operators and expressions](index.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/tutorials/pattern-matching.md) diff --git a/docs/csharp/language-reference/operators/switch-expression.md b/docs/csharp/language-reference/operators/switch-expression.md index 4afbc18c42780..7b75533cb87f2 100644 --- a/docs/csharp/language-reference/operators/switch-expression.md +++ b/docs/csharp/language-reference/operators/switch-expression.md @@ -56,5 +56,5 @@ For more information, see the [`switch` expression](~/_csharplang/proposals/csha - [C# reference](../index.md) - [C# operators and expressions](index.md) - [Patterns](patterns.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/functional/tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../../fundamentals/tutorials/pattern-matching.md) - [`switch` statement](../keywords/switch.md) diff --git a/docs/csharp/language-reference/operators/type-testing-and-cast.md b/docs/csharp/language-reference/operators/type-testing-and-cast.md index d17ff682fdfc2..b4a91585ce088 100644 --- a/docs/csharp/language-reference/operators/type-testing-and-cast.md +++ b/docs/csharp/language-reference/operators/type-testing-and-cast.md @@ -152,5 +152,5 @@ For more information, see the following sections of the [C# language specificati - [C# reference](../index.md) - [C# operators and expressions](index.md) -- [How to safely cast by using pattern matching and the is and as operators](../../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) +- [How to safely cast by using pattern matching and the is and as operators](../../fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md) - [Generics in .NET](../../../standard/generics/index.md) diff --git a/docs/csharp/programming-guide/types/casting-and-type-conversions.md b/docs/csharp/programming-guide/types/casting-and-type-conversions.md index d6cb86ab91925..a4facbc8126fb 100644 --- a/docs/csharp/programming-guide/types/casting-and-type-conversions.md +++ b/docs/csharp/programming-guide/types/casting-and-type-conversions.md @@ -82,7 +82,7 @@ In some reference type conversions, the compiler cannot determine whether a cast [!code-csharp[csProgGuideTypes#41](~/samples/snippets/csharp/VS_Snippets_VBCSharp/CsProgGuideTypes/CS/Class1.cs#41)] -The `Test` method has an `Animal` parameter, thus explicitly casting the argument `a` to a `Reptile` makes a dangerous assumption. It is safer to not make assumptions, but rather check the type. C# provides the [is](../../language-reference/operators/type-testing-and-cast.md#is-operator) operator to enable you to test for compatibility before actually performing a cast. For more information, see [How to safely cast using pattern matching and the as and is operators](../../fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). +The `Test` method has an `Animal` parameter, thus explicitly casting the argument `a` to a `Reptile` makes a dangerous assumption. It is safer to not make assumptions, but rather check the type. C# provides the [is](../../language-reference/operators/type-testing-and-cast.md#is-operator) operator to enable you to test for compatibility before actually performing a cast. For more information, see [How to safely cast using pattern matching and the as and is operators](../../fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md). ## C# language specification diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 5f8542e559295..81fdf46d1762c 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -45,11 +45,6 @@ items: href: fundamentals/program-structure/main-command-line.md - name: Top-level statements href: fundamentals/program-structure/top-level-statements.md - - name: Tutorials - items: - - name: "How to display command-line arguments" - href: fundamentals/program-structure/tutorials/how-to-display-command-line-arguments.md - # Command line processing tutorials - name: Type system items: - name: Overview @@ -69,12 +64,6 @@ items: - name: Anonymous Types href: fundamentals/types/anonymous-types.md # TODO: Delegates, lambdas and events - # TODO: Tutorials - # TODO: DTOs (tuples, anonymous types and records) - # TODO: Values (record structs and structs) - # TODO: Classes and objects (interfaces and classes) - # TODO: Generics (methods and classes) - # TODO: Fundamentals of using lambdas - name: Object Oriented programming items: - name: Overview @@ -87,19 +76,6 @@ items: # Encapsulation & composition - name: Polymorphism href: fundamentals/object-oriented/polymorphism.md - - name: Tutorials - items: - - name: Introduction to classes - href: fundamentals/object-oriented/tutorials/classes.md - - name: Object-Oriented C# - href: fundamentals/object-oriented/tutorials/oop.md - # Composing objects from parts - - name: Inheritance in C# and .NET - href: fundamentals/object-oriented/tutorials/inheritance.md - # programming to interfaces - - name: Converting types - displayName: cast, is, as - href: fundamentals/object-oriented/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md - name: Functional techniques items: # - Conceptual overview @@ -117,12 +93,6 @@ items: href: fundamentals/functional/discards.md - name: Deconstructing tuples and other types href: fundamentals/functional/deconstruct.md - - name: Tutorials - items: - - name: Build data-driven algorithms with pattern matching - href: fundamentals/functional/tutorials/pattern-matching.md - # separating data and algorithms - # Data transformations - name: Exceptions and errors items: - name: Overview @@ -136,19 +106,42 @@ items: href: fundamentals/exceptions/creating-and-throwing-exceptions.md - name: Compiler-Generated Exceptions href: fundamentals/exceptions/compiler-generated-exceptions.md - ## TODO: Replace with a tutorial (include async exceptions) - - name: How to - items: - - name: "How to handle an exception using try-catch" - href: fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md - - name: "How to execute cleanup code using finally" - href: fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md - name: Coding style items: - name: Identifier names href: fundamentals/coding-style/identifier-names.md - name: C# Coding Conventions href: fundamentals/coding-style/coding-conventions.md + - name: Tutorials + items: + - name: "How to display command-line arguments" + href: fundamentals/tutorials/how-to-display-command-line-arguments.md + # TODO: Command line processing tutorials + # TODO: DTOs (tuples, anonymous types and records) + # TODO: Values (record structs and structs) + # TODO: Classes and objects (interfaces and classes) + # TODO: Generics (methods and classes) + # TODO: Fundamentals of using lambdas + - name: Introduction to classes + href: fundamentals/tutorials/classes.md + - name: Object-Oriented C# + href: fundamentals/tutorials/oop.md + # Composing objects from parts + - name: Inheritance in C# and .NET + href: fundamentals/tutorials/inheritance.md + # programming to interfaces + - name: Converting types + displayName: cast, is, as + href: fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md + - name: Build data-driven algorithms with pattern matching + href: fundamentals/tutorials/pattern-matching.md + # separating data and algorithms + # Data transformations + ## TODO: Replace with a tutorial (include async exceptions) + - name: "How to handle an exception using try-catch" + href: fundamentals/exceptions/how-to-handle-an-exception-using-try-catch.md + - name: "How to execute cleanup code using finally" + href: fundamentals/exceptions/how-to-execute-cleanup-code-using-finally.md - name: What's new in C# items: - name: C# 9.0 diff --git a/docs/csharp/tour-of-csharp/features.md b/docs/csharp/tour-of-csharp/features.md index afd622607cc1c..c110a2f6f7d5e 100644 --- a/docs/csharp/tour-of-csharp/features.md +++ b/docs/csharp/tour-of-csharp/features.md @@ -112,7 +112,7 @@ The following code sample demonstrates how to get the `HelpAttribute` instances ## Learn more -You can explore more about C# by trying one of our [tutorials](../fundamentals/object-oriented/tutorials/classes.md). +You can explore more about C# by trying one of our [tutorials](../fundamentals/tutorials/classes.md). >[!div class="step-by-step"] >[Previous](program-building-blocks.md) diff --git a/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md b/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md index 547b54825d8b4..752367c99950e 100644 --- a/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md +++ b/docs/csharp/tour-of-csharp/tutorials/arrays-and-collections.md @@ -205,6 +205,6 @@ You can see an example solution by [looking at the finished sample code on GitHu With each iteration of the loop, you're taking the last two integers in the list, summing them, and adding that value to the list. The loop repeats until you've added 20 items to the list. -Congratulations, you've completed the list tutorial. You can continue with [additional](../../fundamentals/object-oriented/tutorials/classes.md) tutorials in your own development environment. +Congratulations, you've completed the list tutorial. You can continue with [additional](../../fundamentals/tutorials/classes.md) tutorials in your own development environment. You can learn more about working with the `List` type in the .NET fundamentals article on [collections](../../../standard/collections/index.md). You'll also learn about many other collection types. diff --git a/docs/csharp/whats-new/csharp-8.md b/docs/csharp/whats-new/csharp-8.md index 6318a37a72bd7..b338731402068 100644 --- a/docs/csharp/whats-new/csharp-8.md +++ b/docs/csharp/whats-new/csharp-8.md @@ -259,7 +259,7 @@ static Quadrant GetQuadrant(Point point) => point switch The discard pattern in the preceding switch matches when either `x` or `y` is 0, but not both. A switch expression must either produce a value or throw an exception. If none of the cases match, the switch expression throws an exception. The compiler generates a warning for you if you don't cover all possible cases in your switch expression. -You can explore pattern matching techniques in this [advanced tutorial on pattern matching](../fundamentals/functional/tutorials/pattern-matching.md). For more information about a positional pattern, see the [Positional pattern](../language-reference/operators/patterns.md#positional-pattern) section of the [Patterns](../language-reference/operators/patterns.md) article. +You can explore pattern matching techniques in this [advanced tutorial on pattern matching](../fundamentals/tutorials/pattern-matching.md). For more information about a positional pattern, see the [Positional pattern](../language-reference/operators/patterns.md#positional-pattern) section of the [Patterns](../language-reference/operators/patterns.md) article. ## Using declarations From 8a307fe0c34e18ea6fcebf835b5ed48566076f24 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Mon, 17 May 2021 18:51:30 -0400 Subject: [PATCH 51/60] fix warnings --- .../functional/pattern-matching.md | 2 +- .../fundamentals/object-oriented/index.md | 2 +- .../program-structure/main-command-line.md | 2 +- docs/csharp/fundamentals/tutorials/classes.md | 16 ++++---- .../how-to-display-command-line-arguments.md | 2 +- .../fundamentals/tutorials/inheritance.md | 40 +++++++++---------- .../tutorials/pattern-matching.md | 10 ++--- ...ng-pattern-matching-is-and-as-operators.md | 6 +-- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/docs/csharp/fundamentals/functional/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md index ad63af0a333f0..93274795b2cf6 100644 --- a/docs/csharp/fundamentals/functional/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -79,5 +79,5 @@ This article provided a tour of the kinds of code you can write with pattern mat ## See also - [Exploration: Use pattern matching to build your class behavior for better code](../../whats-new/tutorials/patterns-objects.md) -- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](./tutorials/pattern-matching.md) +- [Tutorial: Use pattern matching to build type-driven and data-driven algorithms](../tutorials/pattern-matching.md) - [Reference: Pattern matching](../../language-reference/operators/patterns.md) diff --git a/docs/csharp/fundamentals/object-oriented/index.md b/docs/csharp/fundamentals/object-oriented/index.md index 5b923959bac5e..027fe4c8525db 100644 --- a/docs/csharp/fundamentals/object-oriented/index.md +++ b/docs/csharp/fundamentals/object-oriented/index.md @@ -32,7 +32,7 @@ In general, classes are used to model more complex behavior, or data that is int ## Encapsulation - *Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that aren't intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see [Object-oriented programming](./tutorials/oop.md). + *Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that aren't intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see [Object-oriented programming](../tutorials/oop.md). ## Members diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index bac24de08e087..8c225643d10c2 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -224,4 +224,4 @@ To compile and run the application from a command prompt, follow these steps: ## See also - -- [How to display command line arguments](tutorials/how-to-display-command-line-arguments.md) +- [How to display command line arguments](../tutorials/how-to-display-command-line-arguments.md) diff --git a/docs/csharp/fundamentals/tutorials/classes.md b/docs/csharp/fundamentals/tutorials/classes.md index 7ecd00cf625d6..fa0856b80ada6 100644 --- a/docs/csharp/fundamentals/tutorials/classes.md +++ b/docs/csharp/fundamentals/tutorials/classes.md @@ -9,7 +9,7 @@ In this tutorial, you'll build a console application and see the basic object-or ## Prerequisites -The tutorial expects that you have a machine set up for local development. On Windows, Linux, or macOS, you can use the .NET CLI to create, build, and run applications. On Windows, you can use Visual Studio 2019. For setup instructions, see [Set up your local environment](../../../tour-of-csharp/tutorials/local-environment.md). +The tutorial expects that you have a machine set up for local development. On Windows, Linux, or macOS, you can use the .NET CLI to create, build, and run applications. On Windows, you can use Visual Studio 2019. For setup instructions, see [Set up your local environment](../../tour-of-csharp/tutorials/local-environment.md). ## Create your application @@ -86,7 +86,7 @@ public BankAccount(string name, decimal initialBalance) } ``` -Constructors are called when you create an object using [`new`](../../../language-reference/operators/new-operator.md). Replace the line `Console.WriteLine("Hello World!");` in *Program.cs* with the following code (replace `` with your name): +Constructors are called when you create an object using [`new`](../../language-reference/operators/new-operator.md). Replace the line `Console.WriteLine("Hello World!");` in *Program.cs* with the following code (replace `` with your name): ```csharp var account = new BankAccount("", 1000); @@ -142,7 +142,7 @@ This introduces the concept of ***exceptions***. The standard way of indicating :::code language="csharp" source="./snippets/introduction-to-classes/BankAccount.cs" id="DepositAndWithdrawal"::: -The [`throw`](../../../language-reference/keywords/throw.md) statement **throws** an exception. Execution of the current block ends, and control transfers to the first matching `catch` block found in the call stack. You'll add a `catch` block to test this code a little later on. +The [`throw`](../../language-reference/keywords/throw.md) statement **throws** an exception. Execution of the current block ends, and control transfers to the first matching `catch` block found in the call stack. You'll add a `catch` block to test this code a little later on. The constructor should get one change so that it adds an initial transaction, rather than updating the balance directly. Since you already wrote the `MakeDeposit` method, call it from your constructor. The finished constructor should look like this: @@ -174,7 +174,7 @@ catch (ArgumentOutOfRangeException e) } ``` -You use the [`try` and `catch` statements](../../../language-reference/keywords/try-catch.md) to mark a block of code that may throw exceptions and to catch those errors that you expect. You can use the same technique to test the code that throws an exception for a negative balance. Add the following code at the end of your `Main` method: +You use the [`try` and `catch` statements](../../language-reference/keywords/try-catch.md) to mark a block of code that may throw exceptions and to catch those errors that you expect. You can use the same technique to test the code that throws an exception for a negative balance. Add the following code at the end of your `Main` method: ```csharp // Test for a negative balance. @@ -215,7 +215,7 @@ You can continue with the [object oriented programming](oop.md) tutorial. You can learn more about these concepts in these articles: -- [If and else statement](../../../language-reference/keywords/if-else.md) -- [While statement](../../../language-reference/keywords/while.md) -- [Do statement](../../../language-reference/keywords/do.md) -- [For statement](../../../language-reference/keywords/for.md) +- [If and else statement](../../language-reference/keywords/if-else.md) +- [While statement](../../language-reference/keywords/while.md) +- [Do statement](../../language-reference/keywords/do.md) +- [For statement](../../language-reference/keywords/for.md) diff --git a/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md b/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md index e6e9649053e99..f14351aaa3dd8 100644 --- a/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md +++ b/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md @@ -9,7 +9,7 @@ ms.assetid: b8479f2d-9e05-4d38-82da-2e61246e5437 --- # How to display command-line arguments -Arguments provided to an executable on the command line are accessible in [top-level statements](../top-level-statements.md) or through an optional parameter to `Main`. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable: +Arguments provided to an executable on the command line are accessible in [top-level statements](../program-structure/top-level-statements.md) or through an optional parameter to `Main`. The arguments are provided in the form of an array of strings. Each element of the array contains one argument. White-space between arguments is removed. For example, consider these command-line invocations of a fictitious executable: |Input on command line|Array of strings passed to Main| |----------------------------|-------------------------------------| diff --git a/docs/csharp/fundamentals/tutorials/inheritance.md b/docs/csharp/fundamentals/tutorials/inheritance.md index 76cba363bf420..e6eb400f16a5e 100644 --- a/docs/csharp/fundamentals/tutorials/inheritance.md +++ b/docs/csharp/fundamentals/tutorials/inheritance.md @@ -14,16 +14,16 @@ This tutorial assumes that you've installed the .NET Core SDK. Visit the [.NET C ## Running the examples -To create and run the examples in this tutorial, you use the [dotnet](../../../../core/tools/dotnet.md) utility from the command line. Follow these steps for each example: +To create and run the examples in this tutorial, you use the [dotnet](../../../core/tools/dotnet.md) utility from the command line. Follow these steps for each example: 1. Create a directory to store the example. -1. Enter the [dotnet new console](../../../../core/tools/dotnet-new.md) command at a command prompt to create a new .NET Core project. +1. Enter the [dotnet new console](../../../core/tools/dotnet-new.md) command at a command prompt to create a new .NET Core project. 1. Copy and paste the code from the example into your code editor. -1. Enter the [dotnet restore](../../../../core/tools/dotnet-restore.md) command from the command line to load or restore the project's dependencies. +1. Enter the [dotnet restore](../../../core/tools/dotnet-restore.md) command from the command line to load or restore the project's dependencies. [!INCLUDE[DotNet Restore Note](~/includes/dotnet-restore-note.md)] -1. Enter the [dotnet run](../../../../core/tools/dotnet-run.md) command to compile and execute the example. +1. Enter the [dotnet run](../../../core/tools/dotnet-run.md) command to compile and execute the example. ## Background: What is inheritance? @@ -33,27 +33,27 @@ C# and .NET support *single inheritance* only. That is, a class can only inherit Not all members of a base class are inherited by derived classes. The following members are not inherited: -- [Static constructors](../../../programming-guide/classes-and-structs/static-constructors.md), which initialize the static data of a class. +- [Static constructors](../../programming-guide/classes-and-structs/static-constructors.md), which initialize the static data of a class. -- [Instance constructors](../../../programming-guide/classes-and-structs/constructors.md), which you call to create a new instance of the class. Each class must define its own constructors. +- [Instance constructors](../../programming-guide/classes-and-structs/constructors.md), which you call to create a new instance of the class. Each class must define its own constructors. -- [Finalizers](../../../programming-guide/classes-and-structs/destructors.md), which are called by the runtime's garbage collector to destroy instances of a class. +- [Finalizers](../../programming-guide/classes-and-structs/destructors.md), which are called by the runtime's garbage collector to destroy instances of a class. While all other members of a base class are inherited by derived classes, whether they are visible or not depends on their accessibility. A member's accessibility affects its visibility for derived classes as follows: -- [Private](../../../language-reference/keywords/private.md) members are visible only in derived classes that are nested in their base class. Otherwise, they are not visible in derived classes. In the following example, `A.B` is a nested class that derives from `A`, and `C` derives from `A`. The private `A.value` field is visible in A.B. However, if you remove the comments from the `C.GetValue` method and attempt to compile the example, it produces compiler error CS0122: "'A.value' is inaccessible due to its protection level." +- [Private](../../language-reference/keywords/private.md) members are visible only in derived classes that are nested in their base class. Otherwise, they are not visible in derived classes. In the following example, `A.B` is a nested class that derives from `A`, and `C` derives from `A`. The private `A.value` field is visible in A.B. However, if you remove the comments from the `C.GetValue` method and attempt to compile the example, it produces compiler error CS0122: "'A.value' is inaccessible due to its protection level." [!code-csharp[Inheritance](./snippets/inheritance/private.cs#1)] -- [Protected](../../../language-reference/keywords/protected.md) members are visible only in derived classes. +- [Protected](../../language-reference/keywords/protected.md) members are visible only in derived classes. -- [Internal](../../../language-reference/keywords/internal.md) members are visible only in derived classes that are located in the same assembly as the base class. They are not visible in derived classes located in a different assembly from the base class. +- [Internal](../../language-reference/keywords/internal.md) members are visible only in derived classes that are located in the same assembly as the base class. They are not visible in derived classes located in a different assembly from the base class. -- [Public](../../../language-reference/keywords/public.md) members are visible in derived classes and are part of the derived class' public interface. Public inherited members can be called just as if they are defined in the derived class. In the following example, class `A` defines a method named `Method1`, and class `B` inherits from class `A`. The example then calls `Method1` as if it were an instance method on `B`. +- [Public](../../language-reference/keywords/public.md) members are visible in derived classes and are part of the derived class' public interface. Public inherited members can be called just as if they are defined in the derived class. In the following example, class `A` defines a method named `Method1`, and class `B` inherits from class `A`. The example then calls `Method1` as if it were an instance method on `B`. [!code-csharp[Inheritance](./snippets/inheritance/basics.cs#1)] -Derived classes can also *override* inherited members by providing an alternate implementation. In order to be able to override a member, the member in the base class must be marked with the [virtual](../../../language-reference/keywords/virtual.md) keyword. By default, base class members are not marked as `virtual` and cannot be overridden. Attempting to override a non-virtual member, as the following example does, generates compiler error CS0506: "\ cannot override inherited member \ because it is not marked virtual, abstract, or override. +Derived classes can also *override* inherited members by providing an alternate implementation. In order to be able to override a member, the member in the base class must be marked with the [virtual](../../language-reference/keywords/virtual.md) keyword. By default, base class members are not marked as `virtual` and cannot be overridden. Attempting to override a non-virtual member, as the following example does, generates compiler error CS0506: "\ cannot override inherited member \ because it is not marked virtual, abstract, or override. ```csharp public class A @@ -73,7 +73,7 @@ public class B : A } ``` -In some cases, a derived class *must* override the base class implementation. Base class members marked with the [abstract](../../../language-reference/keywords/abstract.md) keyword require that derived classes override them. Attempting to compile the following example generates compiler error CS0534, "<class> does not implement inherited abstract member <member>", because class `B` provides no implementation for `A.Method1`. +In some cases, a derived class *must* override the base class implementation. Base class members marked with the [abstract](../../language-reference/keywords/abstract.md) keyword require that derived classes override them. Attempting to compile the following example generates compiler error CS0534, "<class> does not implement inherited abstract member <member>", because class `B` provides no implementation for `A.Method1`. ```csharp public abstract class A @@ -176,17 +176,17 @@ In designing your `Publication` class, you need to make several design decisions For your example, you'll use the small hierarchy of a `Publication` class and a single derived class, `Book`. You could easily extend the example to create a number of additional classes that derive from `Publication`, such as `Magazine` and `Article`. -- Whether it makes sense to instantiate the base class. If it does not, you should apply the [abstract](../../../language-reference/keywords/abstract.md) keyword to the class. Otherwise, your `Publication` class can be instantiated by calling its class constructor. If an attempt is made to instantiate a class marked with the `abstract` keyword by a direct call to its class constructor, the C# compiler generates error CS0144, "Cannot create an instance of the abstract class or interface." If an attempt is made to instantiate the class by using reflection, the reflection method throws a . +- Whether it makes sense to instantiate the base class. If it does not, you should apply the [abstract](../../language-reference/keywords/abstract.md) keyword to the class. Otherwise, your `Publication` class can be instantiated by calling its class constructor. If an attempt is made to instantiate a class marked with the `abstract` keyword by a direct call to its class constructor, the C# compiler generates error CS0144, "Cannot create an instance of the abstract class or interface." If an attempt is made to instantiate the class by using reflection, the reflection method throws a . By default, a base class can be instantiated by calling its class constructor. You do not have to explicitly define a class constructor. If one is not present in the base class' source code, the C# compiler automatically provides a default (parameterless) constructor. - For your example, you'll mark the `Publication` class as [abstract](../../../language-reference/keywords/abstract.md) so that it cannot be instantiated. An `abstract` class without any `abstract` methods indicates that this class represents an abstract concept that is shared among several concrete classes (like a `Book`, `Journal`). + For your example, you'll mark the `Publication` class as [abstract](../../language-reference/keywords/abstract.md) so that it cannot be instantiated. An `abstract` class without any `abstract` methods indicates that this class represents an abstract concept that is shared among several concrete classes (like a `Book`, `Journal`). -- Whether derived classes must inherit the base class implementation of particular members, whether they have the option to override the base class implementation, or whether they must provide an implementation. You use the [abstract](../../../language-reference/keywords/abstract.md) keyword to force derived classes to provide an implementation. You use the [virtual](../../../language-reference/keywords/virtual.md) keyword to allow derived classes to override a base class method. By default, methods defined in the base class are *not* overridable. +- Whether derived classes must inherit the base class implementation of particular members, whether they have the option to override the base class implementation, or whether they must provide an implementation. You use the [abstract](../../language-reference/keywords/abstract.md) keyword to force derived classes to provide an implementation. You use the [virtual](../../language-reference/keywords/virtual.md) keyword to allow derived classes to override a base class method. By default, methods defined in the base class are *not* overridable. The `Publication` class does not have any `abstract` methods, but the class itself is `abstract`. -- Whether a derived class represents the final class in the inheritance hierarchy and cannot itself be used as a base class for additional derived classes. By default, any class can serve as a base class. You can apply the [sealed](../../../language-reference/keywords/sealed.md) keyword to indicate that a class cannot serve as a base class for any additional classes. Attempting to derive from a sealed class generated compiler error CS0509, "cannot derive from sealed type \". +- Whether a derived class represents the final class in the inheritance hierarchy and cannot itself be used as a base class for additional derived classes. By default, any class can serve as a base class. You can apply the [sealed](../../language-reference/keywords/sealed.md) keyword to indicate that a class cannot serve as a base class for any additional classes. Attempting to derive from a sealed class generated compiler error CS0509, "cannot derive from sealed type \". For your example, you'll mark your derived class as `sealed`. @@ -243,9 +243,9 @@ In addition to the members that it inherits from `Publication`, the `Book` class The two `Book` constructors share three common parameters. Two, *title* and *publisher*, correspond to parameters of the `Publication` constructor. The third is *author*, which is stored to a public immutable `Author` property. One constructor includes an *isbn* parameter, which is stored in the `ISBN` auto-property. - The first constructor uses the [this](../../../language-reference/keywords/this.md) keyword to call the other constructor. Constructor chaining is a common pattern in defining constructors. Constructors with fewer parameters provide default values when calling the constructor with the greatest number of parameters. + The first constructor uses the [this](../../language-reference/keywords/this.md) keyword to call the other constructor. Constructor chaining is a common pattern in defining constructors. Constructors with fewer parameters provide default values when calling the constructor with the greatest number of parameters. - The second constructor uses the [base](../../../language-reference/keywords/base.md) keyword to pass the title and publisher name to the base class constructor. If you don't make an explicit call to a base class constructor in your source code, the C# compiler automatically supplies a call to the base class' default or parameterless constructor. + The second constructor uses the [base](../../language-reference/keywords/base.md) keyword to pass the title and publisher name to the base class constructor. If you don't make an explicit call to a base class constructor in your source code, the C# compiler automatically supplies a call to the base class' default or parameterless constructor. - A read-only `ISBN` property, which returns the `Book` object's International Standard Book Number, a unique 10- or 13-digit number. The ISBN is supplied as an argument to one of the `Book` constructors. The ISBN is stored in a private backing field, which is auto-generated by the compiler. @@ -277,7 +277,7 @@ In the previous example, you defined a base class that provided an implementatio For example, each closed two-dimensional geometric shape includes two properties: area, the inner extent of the shape; and perimeter, or the distance along the edges of the shape. The way in which these properties are calculated, however, depends completely on the specific shape. The formula for calculating the perimeter (or circumference) of a circle, for example, is different from that of a triangle. The `Shape` class is an `abstract` class with `abstract` methods. That indicates derived classes share the same functionality, but those derived classes implement that functionality differently. -The following example defines an abstract base class named `Shape` that defines two properties: `Area` and `Perimeter`. In addition to marking the class with the [abstract](../../../language-reference/keywords/abstract.md) keyword, each instance member is also marked with the [abstract](../../../language-reference/keywords/abstract.md) keyword. In this case, `Shape` also overrides the method to return the name of the type, rather than its fully qualified name. And it defines two static members, `GetArea` and `GetPerimeter`, that allow callers to easily retrieve the area and perimeter of an instance of any derived class. When you pass an instance of a derived class to either of these methods, the runtime calls the method override of the derived class. +The following example defines an abstract base class named `Shape` that defines two properties: `Area` and `Perimeter`. In addition to marking the class with the [abstract](../../language-reference/keywords/abstract.md) keyword, each instance member is also marked with the [abstract](../../language-reference/keywords/abstract.md) keyword. In this case, `Shape` also overrides the method to return the name of the type, rather than its fully qualified name. And it defines two static members, `GetArea` and `GetPerimeter`, that allow callers to easily retrieve the area and perimeter of an instance of any derived class. When you pass an instance of a derived class to either of these methods, the runtime calls the method override of the derived class. [!code-csharp[Inheritance](./snippets/inheritance/shape.cs#1)] diff --git a/docs/csharp/fundamentals/tutorials/pattern-matching.md b/docs/csharp/fundamentals/tutorials/pattern-matching.md index 64070ef33a6c5..7a0edebb06247 100644 --- a/docs/csharp/fundamentals/tutorials/pattern-matching.md +++ b/docs/csharp/fundamentals/tutorials/pattern-matching.md @@ -82,7 +82,7 @@ namespace toll_calculator } ``` -The preceding code uses a [`switch` expression](../../../language-reference/operators/switch-expression.md) (not the same as a [`switch`](../../../language-reference/keywords/switch.md) statement) that tests the [declaration pattern](../../../language-reference/operators/patterns.md#declaration-and-type-patterns). A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The `{ }` case must follow the cases for each vehicle type. If the order were reversed, the `{ }` case would take precedence. Finally, the `null` [constant pattern](../../../language-reference/operators/patterns.md#constant-pattern) detects when `null` is passed to this method. The `null` pattern can be last because the other patterns match only a non-null object of the correct type. +The preceding code uses a [`switch` expression](../../language-reference/operators/switch-expression.md) (not the same as a [`switch`](../../language-reference/keywords/switch.md) statement) that tests the [declaration pattern](../../language-reference/operators/patterns.md#declaration-and-type-patterns). A **switch expression** begins with the variable, `vehicle` in the preceding code, followed by the `switch` keyword. Next comes all the **switch arms** inside curly braces. The `switch` expression makes other refinements to the syntax that surrounds the `switch` statement. The `case` keyword is omitted, and the result of each arm is an expression. The last two arms show a new language feature. The `{ }` case matches any non-null object that didn't match an earlier arm. This arm catches any incorrect types passed to this method. The `{ }` case must follow the cases for each vehicle type. If the order were reversed, the `{ }` case would take precedence. Finally, the `null` [constant pattern](../../language-reference/operators/patterns.md#constant-pattern) detects when `null` is passed to this method. The `null` pattern can be last because the other patterns match only a non-null object of the correct type. You can test this code using the following code in `Program.cs`: @@ -145,7 +145,7 @@ The toll authority wants to encourage vehicles to travel at maximum capacity. Th - Buses that are less than 50% full pay an extra $2.00. - Buses that are more than 90% full get a $1.00 discount. -These rules can be implemented using a [property pattern](../../../language-reference/operators/patterns.md#property-pattern) in the same switch expression. A property pattern compares a property value to a constant value. The property pattern examines properties of the object once the type has been determined. The single case for a `Car` expands to four different cases: +These rules can be implemented using a [property pattern](../../language-reference/operators/patterns.md#property-pattern) in the same switch expression. A property pattern compares a property value to a constant value. The property pattern examines properties of the object once the type has been determined. The single case for a `Car` expands to four different cases: ```csharp vehicle switch @@ -336,7 +336,7 @@ Next, add a similar function to categorize the time into the blocks: :::code language="csharp" source="./snippets/patterns/finished/toll-calculator/TollCalculator.cs" ID="GetTimeBand"::: -You add a private `enum` to convert each range of time to a discrete value. Then, the `GetTimeBand` method uses [relational patterns](../../../language-reference/operators/patterns.md#relational-patterns), and [conjunctive `or` patterns](../../../language-reference/operators/patterns.md#logical-patterns), both added in C# 9.0. A relational pattern lets you test a numeric value using `<`, `>`, `<=`, or `>=`. The `or` pattern tests if an expression matches one or more patterns. You can also use an `and` pattern to ensure that an expression matches two distinct patterns, and a `not` pattern to test that an expression doesn't match a pattern. +You add a private `enum` to convert each range of time to a discrete value. Then, the `GetTimeBand` method uses [relational patterns](../../language-reference/operators/patterns.md#relational-patterns), and [conjunctive `or` patterns](../../language-reference/operators/patterns.md#logical-patterns), both added in C# 9.0. A relational pattern lets you test a numeric value using `<`, `>`, `<=`, or `>=`. The `or` pattern tests if an expression matches one or more patterns. You can also use an `and` pattern to ensure that an expression matches two distinct patterns, and a `not` pattern to test that an expression doesn't match a pattern. After you create those methods, you can use another `switch` expression with the **tuple pattern** to calculate the pricing premium. You could build a `switch` expression with all 16 arms: @@ -385,5 +385,5 @@ You can download the finished code from the [dotnet/samples](https://github.com/ ## See also -- [Patterns](../../../language-reference/operators/patterns.md) -- [`switch` expression](../../../language-reference/operators/switch-expression.md) +- [Patterns](../../language-reference/operators/patterns.md) +- [`switch` expression](../../language-reference/operators/switch-expression.md) diff --git a/docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md b/docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md index a981e6461d53c..5a6e06348673c 100644 --- a/docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md +++ b/docs/csharp/fundamentals/tutorials/safely-cast-using-pattern-matching-is-and-as-operators.md @@ -9,15 +9,15 @@ helpviewer_keywords: --- # How to safely cast by using pattern matching and the is and as operators -Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](/dotnet/csharp/fundamentals/types). To access the derived type's instance members, it's necessary to [cast](../../../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](/dotnet/csharp/fundamentals/functional/pattern-matching) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../../../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../../../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. +Because objects are polymorphic, it's possible for a variable of a base class type to hold a derived [type](/dotnet/csharp/fundamentals/types). To access the derived type's instance members, it's necessary to [cast](../../programming-guide/types/casting-and-type-conversions.md) the value back to the derived type. However, a cast creates the risk of throwing an . C# provides [pattern matching](../functional/pattern-matching.md) statements that perform a cast conditionally only when it will succeed. C# also provides the [is](../../language-reference/operators/type-testing-and-cast.md#is-operator) and [as](../../language-reference/operators/type-testing-and-cast.md#as-operator) operators to test if a value is of a certain type. The following example shows how to use the pattern matching `is` statement: :::code language="csharp" source="./snippets/safelycast/patternmatching/Program.cs" ID="PatternMatchingIs"::: -The preceding sample demonstrates a few features of pattern matching syntax. The `if (a is Mammal m)` statement combines the test with an initialization assignment. The assignment occurs only when the test succeeds. The variable `m` is only in scope in the embedded `if` statement where it has been assigned. You can't access `m` later in the same method. The preceding example also shows how to use the [`as` operator](../../../language-reference/operators/type-testing-and-cast.md#as-operator) to convert an object to a specified type. +The preceding sample demonstrates a few features of pattern matching syntax. The `if (a is Mammal m)` statement combines the test with an initialization assignment. The assignment occurs only when the test succeeds. The variable `m` is only in scope in the embedded `if` statement where it has been assigned. You can't access `m` later in the same method. The preceding example also shows how to use the [`as` operator](../../language-reference/operators/type-testing-and-cast.md#as-operator) to convert an object to a specified type. -You can also use the same syntax for testing if a [nullable value type](../../../language-reference/builtin-types/nullable-value-types.md) has a value, as shown in the following example: +You can also use the same syntax for testing if a [nullable value type](../../language-reference/builtin-types/nullable-value-types.md) has a value, as shown in the following example: :::code language="csharp" source="./snippets/safelycast/nullablepatternmatching/Program.cs" ID="PatternMatchingNullable"::: From f2fcfb9fcdf579125fbe8f920b897440a9f514ec Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 11:03:56 -0400 Subject: [PATCH 52/60] Apply suggestions from code review Co-authored-by: Tom Dykstra --- docs/csharp/fundamentals/functional/pattern-matching.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/csharp/fundamentals/functional/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md index 93274795b2cf6..d697b30bd3aea 100644 --- a/docs/csharp/fundamentals/functional/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -6,7 +6,7 @@ ms.date: 05/14/2021 # Pattern matching overview -*Pattern matching* is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The "[`is`](../../language-reference/operators/is.md) expression" now supports pattern matching to test an expression and conditionally declare a new variable to the result of that expression. The "[`switch`](../../language-reference/operators/switch-expression.md) expression" enables you to perform actions based on the first matching pattern for an expression. These two expressions support a rich vocabulary of [*patterns*](../../language-reference/operators/patterns.md). You have a rich vocabulary to express your algorithms. +*Pattern matching* is a technique where you test an expression to determine if it has certain characteristics. C# pattern matching provides more concise syntax for testing expressions and taking action when an expression matches. The "[`is`](../../language-reference/operators/is.md) expression" supports pattern matching to test an expression and conditionally declare a new variable to the result of that expression. The "[`switch`](../../language-reference/operators/switch-expression.md) expression" enables you to perform actions based on the first matching pattern for an expression. These two expressions support a rich vocabulary of [*patterns*](../../language-reference/operators/patterns.md). This article provides an overview of scenarios where you can use pattern matching. These techniques can improve the readability and correctness of your code. For a full discussion of all the patterns you can apply, see the article on [patterns](../../language-reference/operators/patterns.md) in the language reference. @@ -74,7 +74,7 @@ The first two arms examine two properties of the `Order`. The third examines onl The preceding code demonstrates the [*positional pattern*](../../language-reference/operators/patterns.md#positional-pattern) where the properties are deconstructed for the expression. -This article provided a tour of the kinds of code you can write with pattern matching in C#. The articles below show more examples of using patterns in scenarios, and the full vocabulary of patterns available to use. +This article provided a tour of the kinds of code you can write with pattern matching in C#. The following articles show more examples of using patterns in scenarios, and the full vocabulary of patterns available to use. ## See also From 377243975dc840e48eacc4dd6689e24afc721342 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 11:04:45 -0400 Subject: [PATCH 53/60] Update docs/csharp/fundamentals/types/index.md Co-authored-by: Tom Dykstra --- docs/csharp/fundamentals/types/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 9dfb8d7c77dc5..8ecd77fc2fa82 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -34,7 +34,7 @@ The compiler uses type information to make sure all operations that are performe The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory. -### Specifying types in variable declarations +## Specifying types in variable declarations When you declare a variable or constant in a program, you must either specify its type or use the [`var`](../../language-reference/keywords/var.md) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types: From 231564d03b564e1dfc6f076ed2c15f2d9c058e80 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 11:04:55 -0400 Subject: [PATCH 54/60] Update docs/csharp/fundamentals/types/interfaces.md Co-authored-by: Tom Dykstra --- docs/csharp/fundamentals/types/interfaces.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/fundamentals/types/interfaces.md b/docs/csharp/fundamentals/types/interfaces.md index 9d7ee51b0bc78..4de89241dfe74 100644 --- a/docs/csharp/fundamentals/types/interfaces.md +++ b/docs/csharp/fundamentals/types/interfaces.md @@ -1,6 +1,6 @@ --- title: "Interfaces - define behavior for multiple types" -description: An interface in C# contains definitions for a group of related functionalities that a non-abstract class or a struct must implement. It describes the behavior of a type that implements the interface. +description: An interface in C# contains definitions for a group of related functionalities that a non-abstract class or a struct must implement. It specifies the members and their signatures for a type that implements the interface. ms.date: 05/14/2021 helpviewer_keywords: - "interfaces [C#]" From 5387151d4350e100cbbf83ed47f7e45eecc6d959 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 11:12:08 -0400 Subject: [PATCH 55/60] Apply suggestions from code review Co-authored-by: Tom Dykstra --- .../exceptions/compiler-generated-exceptions.md | 4 ++-- docs/csharp/fundamentals/object-oriented/objects.md | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md b/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md index 30e3c262b88ac..03f02657e8fba 100644 --- a/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md +++ b/docs/csharp/fundamentals/exceptions/compiler-generated-exceptions.md @@ -1,11 +1,11 @@ --- -title: "Compiler-Generated Exceptions" +title: "Compiler-generated exceptions" description: Learn about compiler-generated exceptions. Review a list of automatically thrown exceptions and their error conditions. ms.date: 05/14/2021 helpviewer_keywords: - "exceptions [C#], compiler-generated" --- -# Compiler-Generated Exceptions +# Compiler-generated exceptions Some exceptions are thrown automatically by the .NET runtime when basic operations fail. These exceptions and their error conditions are listed in the following table. diff --git a/docs/csharp/fundamentals/object-oriented/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md index a962759177478..b31ad7aded220 100644 --- a/docs/csharp/fundamentals/object-oriented/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -15,7 +15,7 @@ A class or struct definition is like a blueprint that specifies what the type ca ## Struct Instances vs. Class Instances -Because classes are reference types, a variable of a class object holds a reference to the address of the object on the managed heap. If a second object of the same type is assigned to the first object, then both variables refer to the object at that address. This point is discussed in more detail later in this article. +Because classes are reference types, a variable of a class object holds a reference to the address of the object on the managed heap. If a second variable of the same type is assigned to the first variable, then both variables refer to the object at that address. This point is discussed in more detail later in this article. Instances of classes are created by using the [`new` operator](../../language-reference/operators/new-operator.md). In the following example, `Person` is the type and `person1` and `person2` are instances, or objects, of that type. @@ -34,12 +34,12 @@ The memory for both `p1` and `p2` is allocated on the thread stack. That memory When you compare two objects for equality, you must first distinguish whether you want to know whether the two variables represent the same object in memory, or whether the values of one or more of their fields are equivalent. If you're intending to compare values, you must consider whether the objects are instances of value types (structs) or reference types (classes, delegates, arrays). -- To determine whether two class instances refer to the same location in memory (which means that they have the same *identity*), use the static method. ( is the implicit base class for all value types and reference types, including user-defined structs and classes.) +- To determine whether two class instances refer to the same location in memory (which means that they have the same *identity*), use the static method. ( is the implicit base class for all value types and reference types, including user-defined structs and classes.) - To determine whether the instance fields in two struct instances have the same values, use the method. Because all structs implicitly inherit from , you call the method directly on your object as shown in the following example: [!code-csharp[csProgGuideStatements#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#32)] - The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) + The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md). - To determine whether the values of the fields in two class instances are equal, you might be able to use the method or the [== operator](../../language-reference/operators/equality-operators.md#equality-operator-). However, only use them if the class has overridden or overloaded them to provide a custom definition of what "equality" means for objects of that type. The class might also implement the interface or the interface. Both interfaces provide methods that can be used to test value equality. When designing your own classes that override `Equals`, make sure to follow the guidelines stated in [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) and . From 49b6b914907dc6b6ee10ecbd6ee850ed32c05296 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 11:22:29 -0400 Subject: [PATCH 56/60] Apply suggestions from code review Co-authored-by: Tom Dykstra --- docs/csharp/toc.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 81fdf46d1762c..9fd50f8cbb2fb 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -87,7 +87,7 @@ items: # - language features: # - Lambdas # - Positional records as DTOs - - name: Pattern Matching + - name: Pattern matching href: fundamentals/functional/pattern-matching.md - name: Discards href: fundamentals/functional/discards.md @@ -98,19 +98,19 @@ items: - name: Overview displayName: exceptions href: fundamentals/exceptions/index.md - - name: Using Exceptions + - name: Using exceptions href: fundamentals/exceptions/using-exceptions.md - - name: Exception Handling + - name: Exception handling href: fundamentals/exceptions/exception-handling.md - - name: Creating and Throwing Exceptions + - name: Creating and throwing exceptions href: fundamentals/exceptions/creating-and-throwing-exceptions.md - - name: Compiler-Generated Exceptions + - name: Compiler-generated exceptions href: fundamentals/exceptions/compiler-generated-exceptions.md - name: Coding style items: - name: Identifier names href: fundamentals/coding-style/identifier-names.md - - name: C# Coding Conventions + - name: C# coding conventions href: fundamentals/coding-style/coding-conventions.md - name: Tutorials items: From 36ae5847df3ae11c96144c75f6cffb76333aef22 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 11:39:27 -0400 Subject: [PATCH 57/60] respond to feedback. --- .../fundamentals/functional/deconstruct.md | 2 +- .../fundamentals/object-oriented/index.md | 18 +------- .../object-oriented/inheritance.md | 2 +- .../fundamentals/object-oriented/objects.md | 2 +- .../object-oriented/snippets/index/class1.cs | 44 ------------------- .../snippets/index/index.csproj | 8 ---- docs/csharp/fundamentals/types/index.md | 10 +++++ docs/csharp/fundamentals/types/namespaces.md | 2 +- docs/csharp/toc.yml | 7 ++- 9 files changed, 20 insertions(+), 75 deletions(-) delete mode 100644 docs/csharp/fundamentals/object-oriented/snippets/index/class1.cs delete mode 100644 docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj diff --git a/docs/csharp/fundamentals/functional/deconstruct.md b/docs/csharp/fundamentals/functional/deconstruct.md index 7c44f192fe892..3fa4626a6ab29 100644 --- a/docs/csharp/fundamentals/functional/deconstruct.md +++ b/docs/csharp/fundamentals/functional/deconstruct.md @@ -54,7 +54,7 @@ Often when deconstructing a tuple, you're interested in the values of only some The following example illustrates the use of tuples with discards. The `QueryCityDataForYears` method returns a six-tuple with the name of a city, its area, a year, the city's population for that year, a second year, and the city's population for that second year. The example shows the change in population between those two years. Of the data available from the tuple, we're unconcerned with the city area, and we know the city name and the two dates at design-time. As a result, we're only interested in the two population values stored in the tuple, and can handle its remaining values as discards. -:::code language="csharp" source="./snippets/deconstructing-tuples/deconstruct-tuple1.cs"::: +:::code language="csharp" source="./snippets/deconstructing-tuples/discard-tuple1.cs"::: ## User-defined types diff --git a/docs/csharp/fundamentals/object-oriented/index.md b/docs/csharp/fundamentals/object-oriented/index.md index 027fe4c8525db..3cb2c775ef3a8 100644 --- a/docs/csharp/fundamentals/object-oriented/index.md +++ b/docs/csharp/fundamentals/object-oriented/index.md @@ -12,24 +12,8 @@ helpviewer_keywords: - "objects [C#]" - "C# language, classes" --- -# Classes, structs, and records +# Object oriented programming -Classes and structs are two of the basic constructs of the common type system in .NET. C# 9 adds records, which are a kind of class. Each is essentially a data structure that encapsulates a set of data and behaviors that belong together as a logical unit. The data and behaviors are the *members* of the class, struct, or record, and they include its methods, properties, events, and so on, as listed later in this article. - -A class, struct, or record declaration is like a blueprint that is used to create instances or objects at run time. If you define a class, struct, or record named `Person`, `Person` is the name of the type. If you declare and initialize a variable `p` of type `Person`, `p` is said to be an object or instance of `Person`. Multiple instances of the same `Person` type can be created, and each instance can have different values in its properties and fields. - -A class or a record is a reference type. When an object of the type is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data. - -A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it's copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy don't affect the other copy. - -In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that isn't intended to be modified after the struct is created. Record types are for larger data structures that contain primarily data that isn't intended to be modified after the object is created. - -## Example - - In the following example, `CustomClass` in the `ProgrammingGuide` namespace has three members: an instance constructor, a property named `Number`, and a method named `Multiply`. The `Main` method in the `Program` class creates an instance (object) of `CustomClass`, and the object's method and property are accessed by using dot notation. - - :::code language="csharp" source="./snippets/index/class1.cs" interactive="try-dotnet"::: - ## Encapsulation *Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that aren't intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see [Object-oriented programming](../tutorials/oop.md). diff --git a/docs/csharp/fundamentals/object-oriented/inheritance.md b/docs/csharp/fundamentals/object-oriented/inheritance.md index f5d8869bb573f..194995a2529f1 100644 --- a/docs/csharp/fundamentals/object-oriented/inheritance.md +++ b/docs/csharp/fundamentals/object-oriented/inheritance.md @@ -20,7 +20,7 @@ Inheritance, together with encapsulation and polymorphism, is one of the three p Conceptually, a derived class is a specialization of the base class. For example, if you have a base class `Animal`, you might have one derived class that is named `Mammal` and another derived class that is named `Reptile`. A `Mammal` is an `Animal`, and a `Reptile` is an `Animal`, but each derived class represents different specializations of the base class. -Interface declarations may define a default implementation for its members. These implementations are inherited by derived interfaces, and by classes that implement those interfaces. For more information on default interface methods, see the article on [interfaces](../../language-reference/keywords/interface.md) in the language reference section. +Interface declarations may define a default implementation for its members. These implementations are inherited by derived interfaces, and by classes that implement those interfaces. For more information on default interface methods, see the article on [interfaces](../types/interfaces.md). When you define a class to derive from another class, the derived class implicitly gains all the members of the base class, except for its constructors and finalizers. The derived class reuses the code in the base class without having to reimplement it. You can add more members in the derived class. The derived class extends the functionality of the base class. diff --git a/docs/csharp/fundamentals/object-oriented/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md index b31ad7aded220..d41a53a574c90 100644 --- a/docs/csharp/fundamentals/object-oriented/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -39,7 +39,7 @@ When you compare two objects for equality, you must first distinguish whether yo [!code-csharp[csProgGuideStatements#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#32)] - The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md). + The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md). Records are reference types that use value semantics for equality. - To determine whether the values of the fields in two class instances are equal, you might be able to use the method or the [== operator](../../language-reference/operators/equality-operators.md#equality-operator-). However, only use them if the class has overridden or overloaded them to provide a custom definition of what "equality" means for objects of that type. The class might also implement the interface or the interface. Both interfaces provide methods that can be used to test value equality. When designing your own classes that override `Equals`, make sure to follow the guidelines stated in [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md) and . diff --git a/docs/csharp/fundamentals/object-oriented/snippets/index/class1.cs b/docs/csharp/fundamentals/object-oriented/snippets/index/class1.cs deleted file mode 100644 index c3b29cca1966a..0000000000000 --- a/docs/csharp/fundamentals/object-oriented/snippets/index/class1.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; - -namespace Fundamentals -{ - // Class definition. - public class CustomClass - { - // Class members. - // - // Property. - public int Number { get; set; } - - // Method. - public int Multiply(int num) - { - return num * Number; - } - - // Instance Constructor. - public CustomClass() - { - Number = 0; - } - } - - // Another class definition that contains Main, the program entry point. - class Program - { - static void Main(string[] args) - { - // Create an object of type CustomClass. - CustomClass custClass = new CustomClass(); - - // Set the value of the public property. - custClass.Number = 27; - - // Call the public method. - int result = custClass.Multiply(4); - Console.WriteLine($"The result is {result}."); - } - } -} -// The example displays the following output: -// The result is 108. diff --git a/docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj b/docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj deleted file mode 100644 index 2082704286ba4..0000000000000 --- a/docs/csharp/fundamentals/object-oriented/snippets/index/index.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - Exe - net5.0 - - - diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 8ecd77fc2fa82..10c3fb8e6a0a0 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -70,6 +70,16 @@ The following illustration shows the relationship between value types and refere > [!NOTE] > You can see that the most commonly used types are all organized in the namespace. However, the namespace in which a type is contained has no relation to whether it is a value type or reference type. +Classes and structs are two of the basic constructs of the common type system in .NET. C# 9 adds records, which are a kind of class. Each is essentially a data structure that encapsulates a set of data and behaviors that belong together as a logical unit. The data and behaviors are the *members* of the class, struct, or record, and they include its methods, properties, events, and so on, as listed later in this article. + +A class, struct, or record declaration is like a blueprint that is used to create instances or objects at run time. If you define a class, struct, or record named `Person`, `Person` is the name of the type. If you declare and initialize a variable `p` of type `Person`, `p` is said to be an object or instance of `Person`. Multiple instances of the same `Person` type can be created, and each instance can have different values in its properties and fields. + +A class or a record is a reference type. When an object of the type is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data. + +A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it's copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy don't affect the other copy. + +In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that isn't intended to be modified after the struct is created. Record types are for larger data structures that contain primarily data that isn't intended to be modified after the object is created. + ### Value types Value types derive from , which derives from . Types that derive from have special behavior in the CLR. Value type variables directly contain their values, which means that the memory is allocated inline in whatever context the variable is declared. There's no separate heap allocation or garbage collection overhead for value-type variables. diff --git a/docs/csharp/fundamentals/types/namespaces.md b/docs/csharp/fundamentals/types/namespaces.md index 325818dd47703..a12a51c92bbe3 100644 --- a/docs/csharp/fundamentals/types/namespaces.md +++ b/docs/csharp/fundamentals/types/namespaces.md @@ -6,7 +6,7 @@ helpviewer_keywords: - "C# language, namespaces" - "namespaces [C#]" --- -# Declare namespaces to organize types in packages +# Declare namespaces to organize types Namespaces are heavily used in C# programming in two ways. First, .NET uses namespaces to organize its many classes, as follows: diff --git a/docs/csharp/toc.yml b/docs/csharp/toc.yml index 9fd50f8cbb2fb..b3f7409941e0d 100644 --- a/docs/csharp/toc.yml +++ b/docs/csharp/toc.yml @@ -36,6 +36,8 @@ items: - name: List collections href: tour-of-csharp/tutorials/arrays-and-collections.md - name: Fundamentals + # TODO: https://github.com/dotnet/docs/pull/23464#discussion_r633857533 + # Use "title suffix" in docfx.json for "fundamentals", and edit H1s to be unique items: - name: Program structure items: @@ -52,10 +54,11 @@ items: - name: Namespaces href: fundamentals/types/namespaces.md # TODO: tuples - - name: Records - href: fundamentals/types/records.md - name: Classes href: fundamentals/types/classes.md + # TODO: Edit Records article so it doesn't depend on classes. + - name: Records + href: fundamentals/types/records.md # TODO: structs - name: Interfaces href: fundamentals/types/interfaces.md From fc63544ce229de06b5c545f8a52a6a242b41cf00 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 11:50:20 -0400 Subject: [PATCH 58/60] missed a bad merge --- .../fundamentals/program-structure/main-command-line.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index 8c225643d10c2..278a7a32965d7 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -29,11 +29,7 @@ For information about how to write application code with an implicit entry point ## Overview - The `Main` method is the entry point of an executable program; it is where the program control starts and ends. -<<<<<<< HEAD -- `Main` is declared inside a class or struct. `Main` must be [static](../../language-reference/keywords/static.md) and it doesn't need to be [public](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [private](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. -======= - `Main` is declared inside a class or struct. `Main` must be [`static`](../../language-reference/keywords/static.md) and it need not be [`public`](../../language-reference/keywords/public.md). (In the earlier example, it receives the default access of [`private`](../../language-reference/keywords/private.md).) The enclosing class or struct is not required to be static. ->>>>>>> cd631f0b85 (edit program structure) - `Main` can either have a `void`, `int`, or, starting with C# 7.1, `Task`, or `Task` return type. - If and only if `Main` returns a `Task` or `Task`, the declaration of `Main` may include the [`async`](../../language-reference/keywords/async.md) modifier. This specifically excludes an `async void Main` method. - The `Main` method can be declared with or without a `string[]` parameter that contains command-line arguments. When using Visual Studio to create Windows applications, you can add the parameter manually or else use the method to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument in the `args` array, but it is the first element of the method. From 8ee05791a3d62161a922c1837399bb485dc5b4e9 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 15:28:04 -0400 Subject: [PATCH 59/60] move remaining samples. --- .../fundamentals/object-oriented/objects.md | 2 +- .../snippets/objects/Equality.cs | 33 +++++++ .../program-structure/main-command-line.md | 2 +- .../snippets/main-command-line/Factorial.cs | 61 +++++++++++++ .../how-to-display-command-line-arguments.md | 2 +- .../command-line-arguments/Program.cs | 21 +++++ .../command-line-arguments.csproj | 9 ++ docs/csharp/fundamentals/types/interfaces.md | 4 +- docs/csharp/fundamentals/types/namespaces.md | 8 +- .../types/snippets/interfaces}/interfaces.cs | 0 .../snippets/interfaces/interfaces.csproj | 7 ++ .../types/snippets/namespaces/Program.cs | 31 +++++++ .../snippets/namespaces/namespaces.csproj | 8 ++ .../csProgGuide/CS/progGuide.cs | 8 -- .../csProgGuide/CS/using.cs | 4 - .../csProgGuideMain/CS/Class1.cs | 88 ------------------- .../csProgGuideNamespaces/CS/Namespaces.cs | 18 ---- .../csProgGuideStatements/CS/Statements.cs | 34 ------- 18 files changed, 179 insertions(+), 161 deletions(-) create mode 100644 docs/csharp/fundamentals/object-oriented/snippets/objects/Equality.cs create mode 100644 docs/csharp/fundamentals/program-structure/snippets/main-command-line/Factorial.cs create mode 100644 docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/Program.cs create mode 100644 docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/command-line-arguments.csproj rename {samples/snippets/csharp/objectoriented => docs/csharp/fundamentals/types/snippets/interfaces}/interfaces.cs (100%) create mode 100644 docs/csharp/fundamentals/types/snippets/interfaces/interfaces.csproj create mode 100644 docs/csharp/fundamentals/types/snippets/namespaces/Program.cs create mode 100644 docs/csharp/fundamentals/types/snippets/namespaces/namespaces.csproj delete mode 100644 samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs diff --git a/docs/csharp/fundamentals/object-oriented/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md index d41a53a574c90..a14a783b86afe 100644 --- a/docs/csharp/fundamentals/object-oriented/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -37,7 +37,7 @@ When you compare two objects for equality, you must first distinguish whether yo - To determine whether two class instances refer to the same location in memory (which means that they have the same *identity*), use the static method. ( is the implicit base class for all value types and reference types, including user-defined structs and classes.) - To determine whether the instance fields in two struct instances have the same values, use the method. Because all structs implicitly inherit from , you call the method directly on your object as shown in the following example: - [!code-csharp[csProgGuideStatements#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs#32)] + :::code language="csharp" source="./snippets/objects/Equality.cs" ID="Snippets32"::: The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md). Records are reference types that use value semantics for equality. diff --git a/docs/csharp/fundamentals/object-oriented/snippets/objects/Equality.cs b/docs/csharp/fundamentals/object-oriented/snippets/objects/Equality.cs new file mode 100644 index 0000000000000..28eaf76e443b6 --- /dev/null +++ b/docs/csharp/fundamentals/object-oriented/snippets/objects/Equality.cs @@ -0,0 +1,33 @@ + +using System; + +public class equality +{ + public static void EqualityTest() + { + // + // Person is defined in the previous example. + + //public struct Person + //{ + // public string Name; + // public int Age; + // public Person(string name, int age) + // { + // Name = name; + // Age = age; + // } + //} + + Person p1 = new Person("Wallace", 75); + Person p2 = new Person("", 42); + p2.Name = "Wallace"; + p2.Age = 75; + + if (p2.Equals(p1)) + Console.WriteLine("p2 and p1 have the same values."); + + // Output: p2 and p1 have the same values. + // + } +} diff --git a/docs/csharp/fundamentals/program-structure/main-command-line.md b/docs/csharp/fundamentals/program-structure/main-command-line.md index 278a7a32965d7..708d07105bce1 100644 --- a/docs/csharp/fundamentals/program-structure/main-command-line.md +++ b/docs/csharp/fundamentals/program-structure/main-command-line.md @@ -194,7 +194,7 @@ To compile and run the application from a command prompt, follow these steps: 1. Paste the following code into any text editor, and then save the file as a text file with the name *Factorial.cs*. - [!code-csharp[csProgGuideMain#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#16)] + :::code language="csharp" source="./snippets/main-command-line/Factorial.cs"::: 2. From the **Start** screen or **Start** menu, open a Visual Studio **Developer Command Prompt** window, and then navigate to the folder that contains the file that you created. diff --git a/docs/csharp/fundamentals/program-structure/snippets/main-command-line/Factorial.cs b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/Factorial.cs new file mode 100644 index 0000000000000..277d34133bcf5 --- /dev/null +++ b/docs/csharp/fundamentals/program-structure/snippets/main-command-line/Factorial.cs @@ -0,0 +1,61 @@ +using System; + +public class Functions +{ + public static long Factorial(int n) + { + // Test for invalid input. + if ((n < 0) || (n > 20)) + { + return -1; + } + + // Calculate the factorial iteratively rather than recursively. + long tempResult = 1; + for (int i = 1; i <= n; i++) + { + tempResult *= i; + } + return tempResult; + } +} + +class MainClass +{ + static int Main(string[] args) + { + // Test if input arguments were supplied. + if (args.Length == 0) + { + Console.WriteLine("Please enter a numeric argument."); + Console.WriteLine("Usage: Factorial "); + return 1; + } + + // Try to convert the input arguments to numbers. This will throw + // an exception if the argument is not a number. + // num = int.Parse(args[0]); + int num; + bool test = int.TryParse(args[0], out num); + if (!test) + { + Console.WriteLine("Please enter a numeric argument."); + Console.WriteLine("Usage: Factorial "); + return 1; + } + + // Calculate factorial. + long result = Functions.Factorial(num); + + // Print result. + if (result == -1) + Console.WriteLine("Input must be >= 0 and <= 20."); + else + Console.WriteLine($"The Factorial of {num} is {result}."); + + return 0; + } +} +// If 3 is entered on command line, the +// output reads: The factorial of 3 is 6. + diff --git a/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md b/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md index f14351aaa3dd8..ff9094ce1e5af 100644 --- a/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md +++ b/docs/csharp/fundamentals/tutorials/how-to-display-command-line-arguments.md @@ -24,4 +24,4 @@ Arguments provided to an executable on the command line are accessible in [top-l This example displays the command-line arguments passed to a command-line application. The output shown is for the first entry in the table above. - [!code-csharp[csProgGuideMain#9](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs#9)] + :::code language="csharp" source="./snippets/command-line-arguments/Program.cs" ::: diff --git a/docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/Program.cs b/docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/Program.cs new file mode 100644 index 0000000000000..02a0bea7241ee --- /dev/null +++ b/docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/Program.cs @@ -0,0 +1,21 @@ +using System; + +class CommandLine +{ + static void Main(string[] args) + { + // The Length property provides the number of array elements. + Console.WriteLine($"parameter count = {args.Length}"); + + for (int i = 0; i < args.Length; i++) + { + Console.WriteLine($"Arg[{i}] = [{args[i]}]"); + } + } +} +/* Output (assumes 3 cmd line args): + parameter count = 3 + Arg[0] = [a] + Arg[1] = [b] + Arg[2] = [c] +*/ diff --git a/docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/command-line-arguments.csproj b/docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/command-line-arguments.csproj new file mode 100644 index 0000000000000..7d4be0d3ce25f --- /dev/null +++ b/docs/csharp/fundamentals/tutorials/snippets/command-line-arguments/command-line-arguments.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + command_line_arguments + + + diff --git a/docs/csharp/fundamentals/types/interfaces.md b/docs/csharp/fundamentals/types/interfaces.md index 4de89241dfe74..1843ed3b9116f 100644 --- a/docs/csharp/fundamentals/types/interfaces.md +++ b/docs/csharp/fundamentals/types/interfaces.md @@ -14,7 +14,7 @@ By using interfaces, you can, for example, include behavior from multiple source You define an interface by using the [`interface`](../../language-reference/keywords/interface.md) keyword as the following example shows. -:::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" ID="Equatable"::: +:::code language="csharp" source="./snippets/interfaces/interfaces.cs" ID="Equatable"::: The name of an interface must be a valid C# [identifier name](../coding-style/identifier-names.md). By convention, interface names begin with a capital `I`. @@ -32,7 +32,7 @@ When a class or struct implements an interface, the class or struct must provide The following example shows an implementation of the interface. The implementing class, `Car`, must provide an implementation of the method. -:::code language="csharp" source="~/samples/snippets/csharp/objectoriented/interfaces.cs" ID="ImplementEquatable"::: +:::code language="csharp" source="./snippets/interfaces/interfaces.cs" ID="ImplementEquatable"::: Properties and indexers of a class can define extra accessors for a property or indexer that's defined in an interface. For example, an interface might declare a property that has a [get](../../language-reference/keywords/get.md) accessor. The class that implements the interface can declare the same property with both a `get` and [set](../../language-reference/keywords/set.md) accessor. However, if the property or indexer uses explicit implementation, the accessors must match. For more information about explicit implementation, see [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md) and [Interface Properties](../../programming-guide/classes-and-structs/interface-properties.md). diff --git a/docs/csharp/fundamentals/types/namespaces.md b/docs/csharp/fundamentals/types/namespaces.md index a12a51c92bbe3..77dea50f236b5 100644 --- a/docs/csharp/fundamentals/types/namespaces.md +++ b/docs/csharp/fundamentals/types/namespaces.md @@ -10,19 +10,19 @@ helpviewer_keywords: Namespaces are heavily used in C# programming in two ways. First, .NET uses namespaces to organize its many classes, as follows: -[!code-csharp[csProgGuide#22](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/progGuide.cs#22)] +:::code language="csharp" source="snippets/namespaces/Program.cs" range="Snippet22"::: is a namespace and is a class in that namespace. The `using` keyword can be used so that the complete name isn't required, as in the following example: -[!code-csharp[csProgGuide#1](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/using.cs#1)] +:::code language="csharp" source="snippets/namespaces/Program.cs" range="Snippet1"::: -[!code-csharp[csProgGuide#23](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/progGuide.cs#23)] +:::code language="csharp" source="snippets/namespaces/Program.cs" range="Snippet22"::: For more information, see the [using Directive](../../language-reference/keywords/using-directive.md). Second, declaring your own namespaces can help you control the scope of class and method names in larger programming projects. Use the [namespace](../../language-reference/keywords/namespace.md) keyword to declare a namespace, as in the following example: -[!code-csharp[csProgGuideNamespaces#6](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs#6)] +:::code language="csharp" source="snippets/namespaces/Program.cs" range="Snippet6"::: The name of the namespace must be a valid C# [identifier name](../coding-style/identifier-names.md). diff --git a/samples/snippets/csharp/objectoriented/interfaces.cs b/docs/csharp/fundamentals/types/snippets/interfaces/interfaces.cs similarity index 100% rename from samples/snippets/csharp/objectoriented/interfaces.cs rename to docs/csharp/fundamentals/types/snippets/interfaces/interfaces.cs diff --git a/docs/csharp/fundamentals/types/snippets/interfaces/interfaces.csproj b/docs/csharp/fundamentals/types/snippets/interfaces/interfaces.csproj new file mode 100644 index 0000000000000..f208d303c9811 --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/interfaces/interfaces.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + + diff --git a/docs/csharp/fundamentals/types/snippets/namespaces/Program.cs b/docs/csharp/fundamentals/types/snippets/namespaces/Program.cs new file mode 100644 index 0000000000000..5f05031962c57 --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/namespaces/Program.cs @@ -0,0 +1,31 @@ +// +using System; +// + +namespace namespaces +{ + class Program + { + static void Main(string[] args) + { + // + System.Console.WriteLine("Hello World!"); + // + } + } +} + +// +namespace SampleNamespace +{ + class SampleClass + { + public void SampleMethod() + { + System.Console.WriteLine( + "SampleMethod inside SampleNamespace"); + } + } +} +// + diff --git a/docs/csharp/fundamentals/types/snippets/namespaces/namespaces.csproj b/docs/csharp/fundamentals/types/snippets/namespaces/namespaces.csproj new file mode 100644 index 0000000000000..2082704286ba4 --- /dev/null +++ b/docs/csharp/fundamentals/types/snippets/namespaces/namespaces.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/progGuide.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/progGuide.cs index df3107085be65..594d002b827dc 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/progGuide.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/progGuide.cs @@ -31,14 +31,6 @@ class HelloAgain { static void Main() { - // - System.Console.WriteLine("Hello World!"); - // - - // - Console.WriteLine("Hello World!"); - // - // System.Console.WriteLine("Hello"); System.Console.WriteLine("World!"); diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/using.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/using.cs index d3398329a49a1..7dbacf633565e 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/using.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuide/CS/using.cs @@ -1,9 +1,5 @@ namespace n0 { -// -using System; -// - // using System.Text; // diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs deleted file mode 100644 index 2e32bf463248d..0000000000000 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideMain/CS/Class1.cs +++ /dev/null @@ -1,88 +0,0 @@ -namespace CsCsrefProgrammingMain -{ - - // - using System; - - public class Functions - { - public static long Factorial(int n) - { - // Test for invalid input. - if ((n < 0) || (n > 20)) - { - return -1; - } - - // Calculate the factorial iteratively rather than recursively. - long tempResult = 1; - for (int i = 1; i <= n; i++) - { - tempResult *= i; - } - return tempResult; - } - } - - class MainClass - { - static int Main(string[] args) - { - // Test if input arguments were supplied. - if (args.Length == 0) - { - Console.WriteLine("Please enter a numeric argument."); - Console.WriteLine("Usage: Factorial "); - return 1; - } - - // Try to convert the input arguments to numbers. This will throw - // an exception if the argument is not a number. - // num = int.Parse(args[0]); - int num; - bool test = int.TryParse(args[0], out num); - if (!test) - { - Console.WriteLine("Please enter a numeric argument."); - Console.WriteLine("Usage: Factorial "); - return 1; - } - - // Calculate factorial. - long result = Functions.Factorial(num); - - // Print result. - if (result == -1) - Console.WriteLine("Input must be >= 0 and <= 20."); - else - Console.WriteLine($"The Factorial of {num} is {result}."); - - return 0; - } - } - // If 3 is entered on command line, the - // output reads: The factorial of 3 is 6. - // - - // - class CommandLine - { - static void Main(string[] args) - { - // The Length property provides the number of array elements. - Console.WriteLine($"parameter count = {args.Length}"); - - for (int i = 0; i < args.Length; i++) - { - Console.WriteLine($"Arg[{i}] = [{args[i]}]"); - } - } - } - /* Output (assumes 3 cmd line args): - parameter count = 3 - Arg[0] = [a] - Arg[1] = [b] - Arg[2] = [c] - */ - // -} diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs index 32051ea81f2ff..09ab6191d1c8d 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideNamespaces/CS/Namespaces.cs @@ -34,24 +34,6 @@ static void Main() } // -//----------------------------------------------------------------------------- -namespace WrapSampleNamespace -{ - // - namespace SampleNamespace - { - class SampleClass - { - public void SampleMethod() - { - System.Console.WriteLine( - "SampleMethod inside SampleNamespace"); - } - } - } - // -} - //----------------------------------------------------------------------------- // namespace SampleNamespace diff --git a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs index e362c8a2dc86b..dfc84937ccfe9 100644 --- a/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs +++ b/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideStatements/CS/Statements.cs @@ -651,38 +651,4 @@ string S() } } } - - namespace ObjectsTopic2 - { - class Equality - { - static void Main() - { - // - // Person is defined in the previous example. - - //public struct Person - //{ - // public string Name; - // public int Age; - // public Person(string name, int age) - // { - // Name = name; - // Age = age; - // } - //} - - Person p1 = new Person("Wallace", 75); - Person p2; - p2.Name = "Wallace"; - p2.Age = 75; - - if (p2.Equals(p1)) - Console.WriteLine("p2 and p1 have the same values."); - - // Output: p2 and p1 have the same values. - // - } - } - } } From cf26e6dfcb84f40212a057c891fdc88ce054c332 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 18 May 2021 15:32:36 -0400 Subject: [PATCH 60/60] fix misspelling --- docs/csharp/fundamentals/object-oriented/objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/fundamentals/object-oriented/objects.md b/docs/csharp/fundamentals/object-oriented/objects.md index a14a783b86afe..d2fdd5bb45a6b 100644 --- a/docs/csharp/fundamentals/object-oriented/objects.md +++ b/docs/csharp/fundamentals/object-oriented/objects.md @@ -37,7 +37,7 @@ When you compare two objects for equality, you must first distinguish whether yo - To determine whether two class instances refer to the same location in memory (which means that they have the same *identity*), use the static method. ( is the implicit base class for all value types and reference types, including user-defined structs and classes.) - To determine whether the instance fields in two struct instances have the same values, use the method. Because all structs implicitly inherit from , you call the method directly on your object as shown in the following example: - :::code language="csharp" source="./snippets/objects/Equality.cs" ID="Snippets32"::: + :::code language="csharp" source="./snippets/objects/Equality.cs" ID="Snippet32"::: The implementation of `Equals` uses boxing and reflection in some cases. For information about how to provide an efficient equality algorithm that is specific to your type, see [How to define value equality for a type](../../programming-guide/statements-expressions-operators/how-to-define-value-equality-for-a-type.md). Records are reference types that use value semantics for equality.