You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# How to: Improve Performance with Generics (C++/CLI)
9
+
# How to: Improve performance with generics (C++/CLI)
10
10
11
11
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).
12
12
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.
14
14
15
15
## Example: Two main drawbacks of .NET Framework collections
16
16
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.
18
18
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.
20
20
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.
22
22
23
23
The following code example shows the two main drawbacks of the .NET Framework collections before generics.
24
24
@@ -71,7 +71,7 @@ Popped an int: 7
71
71
72
72
## Example: Benefit of using generic collection
73
73
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.
75
75
76
76
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.
0 commit comments