Skip to content

Commit 6d25c7b

Browse files
Merge pull request #44055 from dotnet/main
Merge main into live
2 parents 2c45abe + 5d01b47 commit 6d25c7b

16 files changed

+532
-747
lines changed

docs/csharp/language-reference/configure-language-version.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ ms.date: 09/17/2024
77

88
# Configure C# language version
99

10+
The information in this article applies to .NET 5 and above. For UWP projects, see this information in the article on [Choosing a UWP version](/windows/uwp/updates-and-versions/choose-a-uwp-version).
11+
12+
In Visual Studio, the option to change the language version through the UI is disabled because the default version is aligned with the project's target framework (`TFM`). This default configuration ensures compatibility between language features and runtime support. To change the language version in Visual Studio, change the project's target framework.
13+
14+
For example, changing the target `TFM` (for example, from [.NET 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) to [.NET 9](https://dotnet.microsoft.com/en-us/download/dotnet/9.0)) updates the language version accordingly, from C# 10 to C# 13. This approach prevents issues with runtime compatibility and minimizes unexpected build errors due to unsupported language features.
15+
16+
If you need a specific language version that differs from the one automatically selected, refer to the methods in this article to override the default settings directly in the project file.
17+
1018
> [!WARNING]
1119
>
1220
> Setting the `LangVersion` element to `latest` is discouraged. The `latest` setting means the installed compiler uses its latest version. That can change from machine to machine, making builds unreliable. In addition, it enables language features that may require runtime or library features not included in the current SDK.
@@ -20,15 +28,7 @@ If you must specify your C# version explicitly, you can do so in several ways:
2028
> [!TIP]
2129
> You can see the language version in Visual Studio in the project properties page. Under the *Build* tab, the *Advanced* pane displays the version selected.
2230
>
23-
> To know what language version you're currently using, put `#error version` (case sensitive) in your code. This makes the compiler report a compiler error, CS8304, with a message containing the compiler version being used and the current selected language version. See [#error (C# Reference)](preprocessor-directives.md#error-and-warning-information) for more information.
24-
25-
## Why you can't select a different C# version in Visual Studio
26-
27-
In Visual Studio, the option to change the language version through the UI might be disabled because the default version is aligned with the project's target framework (`TFM`). This default configuration ensures compatibility between language features and runtime support.
28-
29-
For example, changing the target `TFM` (for example, from [.NET 6](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) to [.NET 9](https://dotnet.microsoft.com/en-us/download/dotnet/9.0)) will update the language version accordingly, from C# 10 to C# 13. This approach prevents issues with runtime compatibility and minimizes unexpected build errors due to unsupported language features.
30-
31-
If you need a specific language version that differs from the one automatically selected, refer to the methods below to override the default settings directly in the project file.
31+
> To know what language version you're currently using, put `#error version` (case sensitive) in your code. This makes the compiler report a compiler error, CS8304, with a message containing the compiler version being used and the current selected language version. For more information about this pragma, see [#error (C# Reference)](preprocessor-directives.md#error-and-warning-information).
3232
3333
## Edit the project file
3434

@@ -44,7 +44,7 @@ The value `preview` uses the latest available preview C# language version that y
4444

4545
## Configure multiple projects
4646

47-
To configure multiple projects, you can create a *Directory.Build.props* file, typically in your solution directory, that contains the `<LangVersion>` element. Add the following setting to the *Directory.Build.props* file:
47+
To configure multiple C# projects, you can create a *Directory.Build.props* file, typically in your solution directory, that contains the `<LangVersion>` element. Add the following setting to the *Directory.Build.props* file:
4848

4949
```xml
5050
<Project>
@@ -56,6 +56,10 @@ To configure multiple projects, you can create a *Directory.Build.props* file, t
5656

5757
Builds in all subdirectories of the directory containing that file now use the preview C# version. For more information, see [Customize your build](/visualstudio/msbuild/customize-your-build).
5858

59+
> [!NOTE]
60+
>
61+
> The versions for C# and VB are different. Don't use the *Directory.Build.Props* file for a folder where subdirectories contain projects for both languages. The versions won't match.
62+
5963
## C# language version reference
6064

6165
> [!IMPORTANT]

docs/csharp/programming-guide/delegates/delegates-with-named-vs-anonymous-methods.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
---
22
title: "Delegates with Named vs. Anonymous Methods"
3-
description: Learn about delegates with named vs. anonymous methods. See code examples and view additional available resources.
4-
ms.date: 11/22/2024
3+
description: Learn about delegates with named vs. anonymous methods. See code examples and view other available resources.
4+
ms.date: 12/20/2024
55
helpviewer_keywords:
66
- "delegates [C#], with named vs. anonymous methods"
77
- "methods [C#], in delegates"
8-
ms.assetid: 98fa8c61-66b6-4146-986c-3236c4045733
98
---
109
# Delegates with Named vs. Anonymous Methods (C# Programming Guide)
1110

1211
A [delegate](../../language-reference/builtin-types/reference-types.md) can be associated with a named method. When you instantiate a delegate by using a named method, the method is passed as a parameter, for example:
1312

14-
[!code-csharp[csProgGuideDelegates#1](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#1)]
13+
:::code language="csharp" source="./snippets/NamedAnonymousDelegates.cs" id="NamedDelegate":::
1514

16-
This is called using a named method. Delegates constructed with a named method can encapsulate either a [static](../../language-reference/keywords/static.md) method or an instance method. Named methods are the only way to instantiate a delegate in earlier versions of C#. However, in a situation where creating a new method is unwanted overhead, C# enables you to instantiate a delegate and immediately specify a code block that the delegate will process when it is called. The block can contain either a [lambda expression](../../language-reference/operators/lambda-expressions.md) or an [anonymous method](../../language-reference/operators/delegate-operator.md).
15+
The preceding example uses a named method. Delegates constructed with a named method can encapsulate either a [static](../../language-reference/keywords/static.md) method or an instance method. Named methods are the only way to instantiate a delegate in earlier versions of C#. C# enables you to instantiate a delegate and immediately specify a code block that the delegate processes when called. The block can contain either a [lambda expression](../../language-reference/operators/lambda-expressions.md) or an [anonymous method](../../language-reference/operators/delegate-operator.md), as shown in the following example:
1716

18-
The method that you pass as a delegate parameter must have the same signature as the delegate declaration. A delegate instance may encapsulate either static or instance method.
17+
:::code language="csharp" source="./snippets/NamedAnonymousDelegates.cs" id="SnippetAnonymousMethod":::
18+
19+
The method that you pass as a delegate parameter must have the same signature as the delegate declaration. A delegate instance can encapsulate either static or instance method.
1920

2021
> [!NOTE]
2122
> Although the delegate can use an [out](../../language-reference/keywords/method-parameters.md#out-parameter-modifier) parameter, we do not recommend its use with multicast event delegates because you cannot know which delegate will be called.
2223
23-
Method groups with a single overload have a *natural type*. This means the compiler can infer the return type and parameter types for the delegate type:
24+
Method groups with a single overload have a *natural type*. The compiler can infer the return type and parameter types for the delegate type:
2425

2526
```csharp
2627
var read = Console.Read; // Just one overload; Func<int> inferred
@@ -29,13 +30,13 @@ var write = Console.Write; // ERROR: Multiple overloads, can't choose
2930

3031
## Examples
3132

32-
The following is a simple example of declaring and using a delegate. Notice that both the delegate, `MultiplyCallback`, and the associated method, `MultiplyNumbers`, have the same signature
33+
The following example is a simple example of declaring and using a delegate. Notice that both the delegate, `MultiplyCallback`, and the associated method, `MultiplyNumbers`, have the same signature
3334

34-
[!code-csharp[csProgGuideDelegates#2](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#2)]
35+
:::code language="csharp" source="./snippets/NamedAnonymousDelegates.cs" id="DeclareAndUse":::
3536

3637
In the following example, one delegate is mapped to both static and instance methods and returns specific information from each.
3738

38-
[!code-csharp[csProgGuideDelegates#3](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#3)]
39+
:::code language="csharp" source="./snippets/NamedAnonymousDelegates.cs" id="AnotherSample":::
3940

4041
## See also
4142

docs/csharp/programming-guide/delegates/how-to-combine-delegates-multicast-delegates.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
---
22
title: "How to combine delegates (Multicast Delegates)"
3-
description: Learn how to combine delegates to create multicast delegates. See a code example and view additional available resources.
3+
description: Learn how to combine delegates to create multicast delegates. See a code example and view more available resources.
44
ms.topic: how-to
5-
ms.date: 07/20/2015
5+
ms.date: 12/20/2024
66
helpviewer_keywords:
77
- "delegates [C#], combining"
88
- "multicast delegates [C#]"
9-
ms.assetid: 4e689450-6d0c-46de-acfd-f961018ae5dd
109
---
1110
# How to combine delegates (Multicast Delegates) (C# Programming Guide)
1211

13-
This example demonstrates how to create multicast delegates. A useful property of [delegate](../../language-reference/builtin-types/reference-types.md) objects is that multiple objects can be assigned to one delegate instance by using the `+` operator. The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined.
14-
15-
The `-` operator can be used to remove a component delegate from a multicast delegate.
16-
17-
## Example
12+
This example demonstrates how to create multicast delegates. A useful property of [delegate](../../language-reference/builtin-types/reference-types.md) objects is that multiple objects can be assigned to one delegate instance by using the `+` operator. The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined. The `-` operator can be used to remove a component delegate from a multicast delegate.
13+
14+
:::code language="csharp" source="./snippets/MulticastExample.cs":::
1815

19-
[!code-csharp[csProgGuideDelegates#11](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#11)]
20-
2116
## See also
2217

2318
- <xref:System.MulticastDelegate>

docs/csharp/programming-guide/delegates/how-to-declare-instantiate-and-use-a-delegate.md

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,41 @@
11
---
22
title: "How to declare, instantiate, and use a delegate"
3-
description: Learn how to declare, instantiate, and use a delegate. See examples that cover C# 1.0, 2.0, and 3.0 and later.
3+
description: Learn how to declare, instantiate, and use a delegate. This article provides several examples of declaring, instantiating, and invoking delegates.
44
ms.topic: how-to
5-
ms.date: 07/20/2015
5+
ms.date: 12/20/2024
66
helpviewer_keywords:
77
- "delegates [C#], declaring and instantiating"
8-
ms.assetid: 61c4895f-f785-48f8-8bfe-db73b411c4ae
98
---
109
# How to declare, instantiate, and use a Delegate (C# Programming Guide)
1110

1211
You can declare delegates using any of the following methods:
1312

1413
- Declare a delegate type and declare a method with a matching signature:
15-
16-
[!code-csharp[csProgGuideDelegates#13](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#13)]
17-
18-
[!code-csharp[csProgGuideDelegates#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#14)]
19-
14+
15+
:::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="DeclareNamedDelegate":::
16+
17+
:::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="CreateNamedInstance":::
18+
2019
- Assign a method group to a delegate type:
21-
22-
[!code-csharp[csProgGuideDelegates#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#32)]
23-
24-
- Declare an anonymous method:
25-
26-
[!code-csharp[csProgGuideDelegates#15](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#15)]
27-
20+
21+
:::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="MethodGroup":::
22+
23+
- Declare an anonymous method
24+
25+
:::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="AnonymousMethod":::
26+
2827
- Use a lambda expression:
29-
30-
[!code-csharp[csProgGuideDelegates#31](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#31)]
31-
32-
For more information, see [Lambda Expressions](../../language-reference/operators/lambda-expressions.md).
33-
34-
The following example illustrates declaring, instantiating, and using a delegate. The `BookDB` class encapsulates a bookstore database that maintains a database of books. It exposes a method, `ProcessPaperbackBooks`, which finds all paperback books in the database and calls a delegate for each one. The `delegate` type that is used is named `ProcessBookCallback`. The `Test` class uses this class to print the titles and average price of the paperback books.
35-
36-
The use of delegates promotes good separation of functionality between the bookstore database and the client code. The client code has no knowledge of how the books are stored or how the bookstore code finds paperback books. The bookstore code has no knowledge of what processing is performed on the paperback books after it finds them.
37-
38-
## Example
39-
40-
[!code-csharp[csProgGuideDelegates#12](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#12)]
41-
42-
## Robust Programming
43-
44-
- Declaring a delegate.
45-
46-
The following statement declares a new delegate type.
47-
48-
[!code-csharp[csProgGuideDelegates#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#16)]
49-
50-
Each delegate type describes the number and types of the arguments, and the type of the return value of methods that it can encapsulate. Whenever a new set of argument types or return value type is needed, a new delegate type must be declared.
51-
52-
- Instantiating a delegate.
53-
54-
After a delegate type has been declared, a delegate object must be created and associated with a particular method. In the previous example, you do this by passing the `PrintTitle` method to the `ProcessPaperbackBooks` method as in the following example:
55-
56-
[!code-csharp[csProgGuideDelegates#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#17)]
57-
58-
This creates a new delegate object associated with the [static](../../language-reference/keywords/static.md) method `Test.PrintTitle`. Similarly, the non-static method `AddBookToTotal` on the object `totaller` is passed as in the following example:
59-
60-
[!code-csharp[csProgGuideDelegates#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#18)]
61-
62-
In both cases a new delegate object is passed to the `ProcessPaperbackBooks` method.
63-
64-
After a delegate is created, the method it is associated with never changes; delegate objects are immutable.
65-
66-
- Calling a delegate.
67-
68-
After a delegate object is created, the delegate object is typically passed to other code that will call the delegate. A delegate object is called by using the name of the delegate object, followed by the parenthesized arguments to be passed to the delegate. Following is an example of a delegate call:
69-
70-
[!code-csharp[csProgGuideDelegates#19](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#19)]
71-
72-
A delegate can be either called synchronously, as in this example, or asynchronously by using `BeginInvoke` and `EndInvoke` methods.
73-
28+
29+
:::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="LambdaExpression":::
30+
31+
For more information, see [Lambda Expressions](../../language-reference/operators/lambda-expressions.md).
32+
33+
The following example illustrates declaring, instantiating, and using a delegate. The `BookDB` class encapsulates a bookstore database that maintains a database of books. It exposes a method, `ProcessPaperbackBooks`, which finds all paperback books in the database and calls a delegate for each one. The `delegate` type is named `ProcessBookCallback`. The `Test` class uses this class to print the titles and average price of the paperback books.
34+
35+
The use of delegates promotes good separation of functionality between the bookstore database and the client code. The client code has no knowledge of how the books are stored or how the bookstore code finds paperback books. The bookstore code has no knowledge of what processing is performed on the paperback books after it finds them.
36+
37+
:::code language="csharp" source="./snippets/BookStore.cs":::
38+
7439
## See also
7540

7641
- [Events](../events/index.md)

docs/csharp/programming-guide/delegates/index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
---
22
title: "Delegates"
33
description: A delegate in C# is a type that refers to methods with a parameter list and return type. Delegates are used to pass methods as arguments to other methods.
4-
ms.date: 02/02/2021
4+
ms.date: 12/20/2024
55
helpviewer_keywords:
66
- "C# language, delegates"
77
- "delegates [C#]"
8-
ms.assetid: 97de039b-c76b-4b9c-a27d-8c1e1c8d93da
98
---
109
# Delegates (C# Programming Guide)
1110

1211
A [delegate](../../language-reference/builtin-types/reference-types.md) is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
1312

1413
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs. The following example shows a delegate declaration:
1514

16-
[!code-csharp[csProgGuideDelegates#20](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#20)]
15+
:::code language="csharp" source="./snippets/Overview.cs" id="DelegateDeclaration":::
1716

1817
Any method from any accessible class or struct that matches the delegate type can be assigned to the delegate. The method can be either static or an instance method. This flexibility means you can programmatically change method calls, or plug new code into existing classes.
1918

@@ -28,7 +27,6 @@ This ability to refer to a method as a parameter makes delegates ideal for defin
2827

2928
Delegates have the following properties:
3029

31-
- Delegates are similar to C++ function pointers, but delegates are fully object-oriented, and unlike C++ pointers to member functions, delegates encapsulate both an object instance and a method.
3230
- Delegates allow methods to be passed as parameters.
3331
- Delegates can be used to define callback methods.
3432
- Delegates can be chained together; for example, multiple methods can be called on a single event.

0 commit comments

Comments
 (0)