Skip to content

Commit 995a0ec

Browse files
authored
Merge branch 'dotnet:main' into main
2 parents 8ba2760 + 7f30eb5 commit 995a0ec

File tree

22 files changed

+240
-563
lines changed

22 files changed

+240
-563
lines changed

.github/workflows/whats-new.yml

Lines changed: 0 additions & 60 deletions
This file was deleted.

.openpublishing.redirection.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,20 @@
245245
"redirect_url": "/dotnet/samples-and-tutorials/"
246246
},
247247
{
248-
"source_path_from_root": "/docs/whats-new/dotnet-docs-2022-08-01.md",
249-
"redirect_url": "/dotnet/whats-new/dotnet-docs-mod2"
248+
"source_path_from_root": "/docs/whats-new/dotnet-docs-mod0",
249+
"redirect_url": "/dotnet/whats-new"
250250
},
251251
{
252-
"source_path_from_root": "/docs/whats-new/dotnet-docs-2022-09-01.md",
253-
"redirect_url": "/dotnet/whats-new/dotnet-docs-mod0"
252+
"source_path_from_root": "/docs/whats-new/dotnet-docs-mod1",
253+
"redirect_url": "/dotnet/whats-new"
254254
},
255255
{
256-
"source_path_from_root": "/docs/whats-new/dotnet-docs-2022-10-01.md",
257-
"redirect_url": "/dotnet/whats-new/dotnet-docs-mod1"
256+
"source_path_from_root": "/docs/whats-new/dotnet-docs-mod2",
257+
"redirect_url": "/dotnet/whats-new"
258+
},
259+
{
260+
"source_path_from_root": "/docs/whats-new/dotnet-9-release.md",
261+
"redirect_url": "/dotnet/whats-new"
258262
}
259263
]
260264
}

docs/azure/includes/dotnet-all.md

Lines changed: 12 additions & 12 deletions
Large diffs are not rendered by default.

docs/azure/includes/dotnet-new.md

Lines changed: 10 additions & 10 deletions
Large diffs are not rendered by default.

docs/core/deploying/deploy-with-cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ In .NET 9 and later versions, you can configure the .NET installation search pat
137137

138138
- `AppLocal`: app executable's folder
139139
- `AppRelative`: path relative to the app executable
140-
- `EnvironmentVariables`: value of [`DOTNET_ROOT[_<arch>]`](../tools/dotnet-environment-variables.md#dotnet_root-dotnet_rootx86-dotnet_root_x86-dotnet_root_x64) environment variables
140+
- `EnvironmentVariable`: value of [`DOTNET_ROOT[_<arch>]`](../tools/dotnet-environment-variables.md#dotnet_root-dotnet_rootx86-dotnet_root_x86-dotnet_root_x64) environment variables
141141
- `Global`: [registered](https://github.com/dotnet/designs/blob/main/accepted/2020/install-locations.md#global-install-to-custom-location) and [default](https://github.com/dotnet/designs/blob/main/accepted/2020/install-locations.md#global-install-to-default-location) global install locations
142142

143143
`AppHostRelativeDotNet` specifies the path relative to the executable that will be searched when `AppHostDotNetSearch` contains `AppRelative`.

docs/core/deploying/trimming/trimming-options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Several feature areas of the framework libraries come with trimmer directives th
7171
| `EnableUnsafeBinaryFormatterSerialization` | When set to `false`, removes BinaryFormatter serialization support. For more information, see [BinaryFormatter serialization methods are obsolete](../../compatibility/serialization/5.0/binaryformatter-serialization-obsolete.md) and [In-box BinaryFormatter implementation removed and always throws](../../compatibility/serialization/9.0/binaryformatter-removal.md). |
7272
| `EnableUnsafeUTF7Encoding` | When set to `false`, removes insecure UTF-7 encoding code. For more information, see [UTF-7 code paths are obsolete](../../compatibility/core-libraries/5.0/utf-7-code-paths-obsolete.md). |
7373
| `EventSourceSupport` | When set to `false`, removes EventSource-related code and logic. |
74+
| `Http3Support` (.NET 10+) | When set to `false`, removes code related to support for HTTP/3 in <xref:System.Net.Http>. |
7475
| `HttpActivityPropagationSupport` | When set to `false`, removes code related to diagnostics support for <xref:System.Net.Http>. |
7576
| `InvariantGlobalization` | When set to `true`, removes globalization-specific code and data. For more information, see [Invariant mode](../../runtime-config/globalization.md#invariant-mode). |
7677
| `MetadataUpdaterSupport` | When set to `false`, removes metadata update&ndash;specific logic related to hot reload. |

docs/core/project-sdk/msbuild-props.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ The following table lists valid values. You can specify multiple values, separat
16281628
| --- | --- |
16291629
| `AppLocal` | App executable's folder |
16301630
| `AppRelative` | Path relative to the app executable as specified by [AppHostRelativeDotNet](#apphostrelativedotnet) |
1631-
| `EnvironmentVariables` | Value of [`DOTNET_ROOT[_<arch>]`](../tools/dotnet-environment-variables.md#dotnet_root-dotnet_rootx86-dotnet_root_x86-dotnet_root_x64) environment variables |
1631+
| `EnvironmentVariable` | Value of [`DOTNET_ROOT[_<arch>]`](../tools/dotnet-environment-variables.md#dotnet_root-dotnet_rootx86-dotnet_root_x86-dotnet_root_x64) environment variables |
16321632
| `Global` | [Registered](https://github.com/dotnet/designs/blob/main/accepted/2020/install-locations.md#global-install-to-custom-location) and [default](https://github.com/dotnet/designs/blob/main/accepted/2020/install-locations.md#global-install-to-default-location) global install locations |
16331633

16341634
This property was introduced in .NET 9.

docs/csharp/language-reference/compiler-messages/cs8170.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class Other
3535
public void Method()
3636
{
3737
var p = new Program();
38-
var d = p.M();
38+
ref int d = ref p.M();
3939
}
4040
}
4141
```
@@ -83,3 +83,31 @@ public class Other
8383
}
8484
}
8585
```
86+
87+
Another approach is to use the <xref:System.Diagnostics.CodeAnalysis.UnscopedRefAttribute?displayProperty=nameWithType> attribute. It will mark the reference to be allowed to escape the scope.<br/>
88+
Use this only when you know that it is safe for the reference to leave the scope.
89+
Below is the example of applying <xref:System.Diagnostics.CodeAnalysis.UnscopedRefAttribute?displayProperty=nameWithType> to `int M()` method, which fixes the CS8170:
90+
91+
```csharp
92+
using System.Diagnostics.CodeAnalysis;
93+
94+
struct Program
95+
{
96+
public int d;
97+
98+
[UnscopedRef]
99+
public ref int M()
100+
{
101+
return ref d; // No error - ref is valid to escape the scope in this line of that method
102+
}
103+
}
104+
105+
public class Other
106+
{
107+
public void Method()
108+
{
109+
var p = new Program();
110+
ref int d = ref p.M();
111+
}
112+
}
113+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using System;
2+
3+
namespace ContravarianceExample
4+
{
5+
// <snippet1>
6+
// Custom EventArgs classes to demonstrate the hierarchy
7+
public class KeyEventArgs(string keyCode) : EventArgs
8+
{
9+
public string KeyCode { get; set; } = keyCode;
10+
}
11+
12+
public class MouseEventArgs(int x, int y) : EventArgs
13+
{
14+
public int X { get; set; } = x;
15+
public int Y { get; set; } = y;
16+
}
17+
18+
// Define delegate types that match the Windows Forms pattern
19+
public delegate void KeyEventHandler(object sender, KeyEventArgs e);
20+
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
21+
22+
// A simple class that demonstrates contravariance with events
23+
public class Button
24+
{
25+
// Events that expect specific EventArgs-derived types
26+
public event KeyEventHandler? KeyDown;
27+
public event MouseEventHandler? MouseClick;
28+
29+
// Method to simulate key press
30+
public void SimulateKeyPress(string key)
31+
{
32+
Console.WriteLine($"Simulating key press: {key}");
33+
KeyDown?.Invoke(this, new KeyEventArgs(key));
34+
}
35+
36+
// Method to simulate mouse click
37+
public void SimulateMouseClick(int x, int y)
38+
{
39+
Console.WriteLine($"Simulating mouse click at ({x}, {y})");
40+
MouseClick?.Invoke(this, new MouseEventArgs(x, y));
41+
}
42+
}
43+
44+
public class Form1
45+
{
46+
private Button button1;
47+
48+
public Form1()
49+
{
50+
button1 = new Button();
51+
52+
// Event handler that accepts a parameter of the base EventArgs type.
53+
// This method can handle events that expect more specific EventArgs-derived types
54+
// due to contravariance in delegate parameters.
55+
56+
// You can use a method that has an EventArgs parameter,
57+
// although the KeyDown event expects the KeyEventArgs parameter.
58+
button1.KeyDown += MultiHandler;
59+
60+
// You can use the same method for an event that expects
61+
// the MouseEventArgs parameter.
62+
button1.MouseClick += MultiHandler;
63+
}
64+
65+
// Event handler that accepts a parameter of the base EventArgs type.
66+
// This works for both KeyDown and MouseClick events because:
67+
// - KeyDown expects KeyEventHandler(object sender, KeyEventArgs e)
68+
// - MouseClick expects MouseEventHandler(object sender, MouseEventArgs e)
69+
// - Both KeyEventArgs and MouseEventArgs derive from EventArgs
70+
// - Contravariance allows a method with a base type parameter (EventArgs)
71+
// to be used where a derived type parameter is expected
72+
private void MultiHandler(object sender, EventArgs e)
73+
{
74+
Console.WriteLine($"MultiHandler called at: {DateTime.Now:HH:mm:ss.fff}");
75+
76+
// You can check the actual type of the event args if needed
77+
switch (e)
78+
{
79+
case KeyEventArgs keyArgs:
80+
Console.WriteLine($" - Key event: {keyArgs.KeyCode}");
81+
break;
82+
case MouseEventArgs mouseArgs:
83+
Console.WriteLine($" - Mouse event: ({mouseArgs.X}, {mouseArgs.Y})");
84+
break;
85+
default:
86+
Console.WriteLine($" - Generic event: {e.GetType().Name}");
87+
break;
88+
}
89+
}
90+
91+
public void DemonstrateEvents()
92+
{
93+
Console.WriteLine("Demonstrating contravariance in event handlers:");
94+
Console.WriteLine("Same MultiHandler method handles both events!\n");
95+
96+
button1.SimulateKeyPress("Enter");
97+
button1.SimulateMouseClick(100, 200);
98+
button1.SimulateKeyPress("Escape");
99+
button1.SimulateMouseClick(50, 75);
100+
}
101+
}
102+
// </snippet1>
103+
104+
// <snippet2>
105+
// Demonstration of how contravariance works with delegates:
106+
//
107+
// 1. KeyDown event signature: KeyEventHandler(object sender, KeyEventArgs e)
108+
// where KeyEventArgs derives from EventArgs
109+
//
110+
// 2. MouseClick event signature: MouseEventHandler(object sender, MouseEventArgs e)
111+
// where MouseEventArgs derives from EventArgs
112+
//
113+
// 3. Our MultiHandler method signature: MultiHandler(object sender, EventArgs e)
114+
//
115+
// 4. Contravariance allows us to use MultiHandler (which expects EventArgs)
116+
// for events that provide more specific types (KeyEventArgs, MouseEventArgs)
117+
// because the more specific types can be safely treated as their base type.
118+
//
119+
// This is safe because:
120+
// - The MultiHandler only uses members available on the base EventArgs type
121+
// - KeyEventArgs and MouseEventArgs can be implicitly converted to EventArgs
122+
// - The compiler knows that any EventArgs-derived type can be passed safely
123+
// </snippet2>
124+
125+
class Program
126+
{
127+
static void Main()
128+
{
129+
var form = new Form1();
130+
form.DemonstrateEvents();
131+
}
132+
}
133+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)