Skip to content

Commit d935ac6

Browse files
Update InlineArrayAttribute.xml (#10036)
* Update InlineArrayAttribute.xml Add note about behavior of Equals() and GetHashCode() when InlineArrayAttribute is applied to a ValueType. --------- Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
1 parent 523b75c commit d935ac6

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

xml/System.Runtime.CompilerServices/InlineArrayAttribute.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@
2626
</Attributes>
2727
<Docs>
2828
<summary>Indicates that the instance's storage is sequentially replicated <see cref="P:System.Runtime.CompilerServices.InlineArrayAttribute.Length" /> times.</summary>
29-
<remarks>This attribute can be used to annotate a <see langword="struct" /> type with a single field. The runtime replicates that field in the actual type layout as many times as is specified.</remarks>
29+
<remarks>
30+
<format type="text/markdown"><![CDATA[
31+
32+
## Remarks
33+
34+
This attribute can be used to annotate a `struct` type with a single field. The runtime replicates that field in the actual type layout as many times as is specified.
35+
36+
> [!IMPORTANT]
37+
> .NET 9 and later, the default implementations of `Equals()` and `GetHashCode()` for types marked with this attribute throw <xref:System.NotSupportedException>. You must override both <xref:System.ValueType.Equals(System.Object)> and <xref:System.ValueType.GetHashCode%2A> if they will be used.
38+
39+
]]></format>
40+
</remarks>
3041
<example>The following example shows how to declare an inline array type with eight <see langword="float" /> values.
3142
<code lang="csharp">
3243
[InlineArray(8)]

xml/System/ValueType.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ The <xref:System.ValueType.Equals(System.Object)?displayProperty=nameWithType> m
200200
201201
The default implementation calls <xref:System.Object.Equals(System.Object)?displayProperty=nameWithType> on each field of the current instance and `obj` and returns `true` if all fields are equal.
202202
203-
> [!TIP]
203+
.NET 9 and later, the default implementation of <xref:System.ValueType.Equals(System.Object)?displayProperty=nameWithType> throws <xref:System.NotSupportedException> if <xref:System.Runtime.CompilerServices.InlineArrayAttribute> is applied to the type.
204+
205+
> [!IMPORTANT]
204206
> Particularly if your value type contains fields that are reference types, you should override the <xref:System.ValueType.Equals(System.Object)> method. This can improve performance and enable you to more closely represent the meaning of equality for the type.
205207
206208
## Notes for the Windows Runtime
@@ -274,6 +276,8 @@ The <xref:System.ValueType.GetHashCode%2A> method applies to types derived from
274276
275277
For more information, see <xref:System.Object.GetHashCode%2A?displayProperty=nameWithType>, and <xref:System.Collections.Hashtable?displayProperty=nameWithType>.
276278
279+
.NET 9 and later, the default implementation of <xref:System.ValueType.GetHashCode%2A?displayProperty=nameWithType> throws <xref:System.NotSupportedException> if <xref:System.Runtime.CompilerServices.InlineArrayAttribute> is applied to the type.
280+
277281
## Notes for the Windows Runtime
278282
279283
When you call the <xref:System.ValueType.GetHashCode%2A> method on a Windows Runtime structure, it provides the default behavior for value types that don't override <xref:System.ValueType.GetHashCode%2A>. This is part of the support that .NET provides for the Windows Runtime (see [.NET Support for Windows Store Apps and Windows Runtime](/dotnet/standard/cross-platform/support-for-windows-store-apps-and-windows-runtime)). Windows Runtime structures can't override <xref:System.ValueType.GetHashCode%2A>, even if they're written with C# or Visual Basic, because they can't have methods. (In addition, structures in the Windows Runtime itself don't inherit <xref:System.ValueType>.) However, they appear to have <xref:System.ValueType.ToString%2A>, <xref:System.ValueType.Equals%2A>, and <xref:System.ValueType.GetHashCode%2A> methods when you use them in your C# or Visual Basic code, and .NET provides the default behavior for these methods.

0 commit comments

Comments
 (0)