Skip to content

Commit 8416aad

Browse files
Merge pull request #4557 from corob-msft/learn/corob/cpp-docs-4200
Update Generics example per 4200
2 parents e433899 + 1f26402 commit 8416aad

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

docs/extensions/how-to-improve-performance-with-generics-visual-cpp.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
---
2-
description: "Learn more about: How to: Improve Performance with Generics (C++/CLI)"
3-
title: "How to: Improve Performance with Generics (C++/CLI)"
4-
ms.date: "10/12/2018"
2+
description: "Learn more about: How to: Improve performance with generics (C++/CLI)"
3+
title: "How to: Improve performance with generics (C++/CLI)"
4+
ms.date: 10/03/2022
55
ms.topic: "reference"
66
helpviewer_keywords: ["performance, C++", "C++, performance", "C++, generics", "generics [C++], performance"]
77
ms.assetid: f14a175b-301f-46cc-86e4-c82d35f9aa3e
88
---
9-
# How to: Improve Performance with Generics (C++/CLI)
9+
# How to: Improve performance with generics (C++/CLI)
1010

1111
With generics, you can create reusable code based on a type parameter. The actual type of the type parameter is deferred until called by client code. For more information on generics, see [Generics](generics-cpp-component-extensions.md).
1212

13-
This article will discuss how generics can help increase the performance of an application that uses collections.
13+
This article discusses how generics can help increase the performance of an application that uses collections.
1414

1515
## Example: Two main drawbacks of .NET Framework collections
1616

17-
The .NET Framework comes with many collection classes in the <xref:System.Collections?displayProperty=fullName> namespace. Most of these collections operate on objects of type <xref:System.Object?displayProperty=fullName>. This allows collections to store any type, since all types in the .NET Framework, even value types, derive from <xref:System.Object?displayProperty=fullName>. However, there are two drawbacks to this approach.
17+
The .NET Framework comes with many collection classes in the <xref:System.Collections?displayProperty=fullName> namespace. Most of these collections operate on objects of type <xref:System.Object?displayProperty=fullName>. Collections can store any type, since all types in the .NET Framework, even value types, derive from <xref:System.Object?displayProperty=fullName>. However, there are two drawbacks to this approach.
1818

19-
First, if the collection is storing value types such as integers, the value must be boxed before being added to the collection and unboxed when the value is retrieved from the collection. These are expensive operations.
19+
First, if the collection is storing value types such as integers, the value must be boxed before being added to the collection and unboxed when the value is retrieved from the collection. These operations are expensive.
2020

21-
Second, there is no way to control which types can be added to a collection. It is perfectly legal to add an integer and a string to the same collection, even though this is probably not what was intended. Therefore, in order for your code to be type safe, you have to check that the type retrieved from the collection really is what was expected.
21+
Second, there's no way to control which types can be added to a collection. It's perfectly legal to add an integer and a string to the same collection, even though it's probably not what was intended. Therefore, in order for your code to be type safe, you have to check that the type retrieved from the collection really is what was expected.
2222

2323
The following code example shows the two main drawbacks of the .NET Framework collections before generics.
2424

@@ -71,7 +71,7 @@ Popped an int: 7
7171

7272
## Example: Benefit of using generic collection
7373

74-
The new <xref:System.Collections.Generic?displayProperty=fullName> namespace contains many of the same collections found in the <xref:System.Collections?displayProperty=fullName> namespace, but they have been modified to accept generic type parameters. This eliminates the two drawbacks of non-generic collections: the boxing and unboxing of value types and the inability to specify the types to be stored in the collections. Operations on the two collections are identical; they differ only in how they are instantiated.
74+
The new <xref:System.Collections.Generic?displayProperty=fullName> namespace contains many of the same collections found in the <xref:System.Collections?displayProperty=fullName> namespace, but they've been modified to accept generic type parameters. This change eliminates the two drawbacks of non-generic collections: the boxing and unboxing of value types and the inability to specify the types to be stored in the collections. Operations on the two collections are identical; they differ only in how they're instantiated.
7575

7676
Compare the example written above with this example that uses a generic <xref:System.Collections.Generic.Stack%601> collection. On large collections that are frequently accessed, the performance of this example will be significantly greater than the preceding example.
7777

@@ -89,8 +89,8 @@ int main()
8989
// This Stack can only contain integers.
9090
Stack<int> ^s = gcnew Stack<int>();
9191

92-
// Push an integer to the Stack.
93-
// A boxing operation is performed here.
92+
// Push an integer to the Stack<int>.
93+
// No boxing operation is performed here.
9494
s->Push(7);
9595
s->Push(14);
9696

0 commit comments

Comments
 (0)