Using system collections generic c что это
Перейти к содержимому

Using system collections generic c что это

  • автор:

One step beyond by using .NET Collections to its fullest

In .NET, we can create and manage collections (i.e., groups of related objects) that can dynamically grow and shrink based on our needs. Alternatively, we could use arrays of objects. However, arrays can be used when we need a fixed number of strongly typed objects.

As we can understand, collections can provide great flexibility when working with groups of objects. So let’s take it from the start. The .NET offers several kinds of collections. Each collection is a class that we should instantiate, and then we can manage (e.g., add, remove, filter, etc.) its items using the provided methods.

For example, we may want items of the same data type (e.g., string) or a collection with key/value pairs (e.g., an integer key for a string value). In such a case, we can use the System.Collections.Generic namespace (e.g. List<string> , Dictionary<int, string> ).

In this article, we will learn about the following collection namespaces, their most frequently used classes, when we can use them, make some comparisons and learn about the yield keyword.

  • System.Collections : Include the legacy types of ArrayList, Hashtable, Queue, and Stack that stores elements as Object type.
  • System.Collections.Generic : To create generic ( <T> ) collections for a specific data type.
  • System.Collections.Concurrent : Provide thread-safe collection classes to access the collection items from multiple threads (concurrently).
  • System.Collections.Immutable : Provide immutable collection interfaces and classes (thread-safe collections that cannot be changed).

The System.Collections namespace contains interfaces and classes that define various collections of objects. The defined types of this namespace are legacy and, thus, are not recommended to be used for new development. The following table shows the frequently used classes and their recommended alternative in the System.Collections.Generic namespace.

Legacy Class Description Recommended Class
ArrayList An array of objects whose size is dynamically increased as required (implements the IList interface). List
Hashtable A collection of key/value pairs that are organized based on the key’s hash code. A Hashtable is slower than a dictionary because it requires boxing and unboxing. Dictionary
Queue A first-in, first-out (FIFO) collection of objects. Queue
Stack A last-in, first-out (LIFO) collection of objects. Stack

The System.Collections.Generic namespace provides interfaces and generic collection classes to create collections with the same data type (by enforcing strong typing). So, when creating an instance of a generic collection class, such as List<T> , Dictionary<TKey, TValue> , etc., we should replace the T parameter with the type of our objects.

For example, we could keep a list of string values ( List<string> ), a list of custom User objects ( List< User> ), a dictionary of integer keys with string values ( Dictionary<int, string> ), etc.

Each generic collection class has its purpose and usage. For example, in a Dictionary<TKey, TValue> , we can add items (objects or value types) paired with a unique key and quickly retrieve the item by using the key. In the following section, we can see some frequently used generic collection classes.

A Dictionary provide a collection of a paired key to value items.

  • Each key must be unique in the collection (no duplicate keys).
  • It is implemented as a hash table. Thus, retrieving a value by using its key is very fast (close to O(1)).
  • A key cannot be null , but a value can be.
  • For enumeration purposes, each item is represented as a KeyValuePair structure.
  • For an immutable dictionary class, see ImmutableDictionary.
  • For a read-only dictionary, see the ReadOnlyDictionary.

Code Example:

A List is a strongly typed list of objects that can be accessed by its index (see the code example below).

  • We can add items using the Add or AddRange methods.
  • It is not guaranteed to be sorted.
  • We can access an item using an integer index (zero-based).
  • Accepts null values.
  • Allows duplicate items.
  • For an immutable list class, see ImmutableList.
  • For a read-only list, see the IReadOnlyCollection.

Code Example:

Queue and Stack

Queues and stacks are maybe one of the first things you learn when learning a computer language in computer science class. In general, queues and stacks are used to temporarily store information to be used (accessed) in a specific order.

Tip: To increase the performance of your application when using queues and stacks, set a high initial capacity depending on your case. Heavy processes are performed when their capacity needs to be increased (e.g., reallocating the internal array in queues, and the stacks Push method becomes an O(n) operation).

  • Queues are used to access the information in the same order stored in the collection (Figure 1), i.e., first-in, first-out (FIFO).
  • To easily remember the queue concept, imagine people waiting in a line to get their coffee (i.e., a queue of people). The one that is first in the line will be the first that will be served.
  • Use the Enqueue method to add a T item at the end of the Queue<T> .
  • Use the Dequeue method to get and remove the oldest T item from the start of the queue.
  • Use the Peek method to get (peek) the next T item to be dequeued.
  • Stacks are used to access the information in the reverse order that is stored (Figure 2), i.e., last-in, first-out (LIFO).
  • To easily remember the stack concept, imagine a pile of boxes (on top of each other). If you need to move them, you will probably get one on top (i.e., the last you stored).
  • Use the Push method to add a T item at the top of the Stack<T> .
  • Use the Pop method to get and remove the T item from the top of the Stack<T> .
  • Use the Peek method to get (peek) the next T item that would be popped.

In .NET 6.0, the PriorityQueue collection was introduced in the System.Collections.Generic namespace, in which we can add (i.e. Enqueue) new items with a value and a priority. On dequeuing, the item with the lowest priority value will be removed. The .NET documentation notes that first-in-first-out semantics are not guaranteed in cases of equal priority.

The Priority Queue commands illustration.Figure 3. — The Priority Queue commands illustration.

The System.Collections.Concurrent namespace provides several thread-safe collection classes that we should use instead of the corresponding types in the System.Collections.Generic and System.Collections namespaces whenever multiple threads access the collection concurrently. In the following table, we can see some frequently used concurrent collection classes.

Concurrent Class Collection Description
ConcurrentDictionary Provides a thread-safe collection of paired TKey to TValue items.
ConcurrentQueue Provides thread-safe Queues, i.e., first-in, first-out (FIFO).
ConcurrentStack Provides thread-safe Stacks, i.e., last-in, first-out (LIFO).

The System.Collections.Immutable namespace provides immutable collections that can assure its consumer that the collection never changes. In addition, they provide implicit thread safety. Thus, we do not need locks to access the collections. In the following table, we can see some frequently used immutable collection classes.

Immutable Class Collection Description
ImmutableDictionary Provides an immutable collection of paired TKey to TValue items.
ImmutableList Provides an immutable list (of strongly typed objects accessed by index).
ImmutableArray Provides methods to create immutable arrays (cannot be changed after they are created).

One Step Beyond

Yield Contextual Keyword

As the .NET documentation states, yield is a contextual keyword used in a statement to indicate an iterator (for example, when used in a method). By using yield , we can define an iterator (e.g., an IEnumerable<T> ) without creating a temporary collection (e.g., a List<T> ) to hold the state of the enumerator.

In the following code examples, we can see that we do not need a temporary collection when using yield return . Okay, the example functions may be silly, but you get the point of the yield keyword ��. If we want to state the end of the iteration, we can use the yield break statement.

Immutable VS ReadOnly Collections

In previous sections, we saw that we could “convert” our collections to ReadOnly or Immutable . From their names, we can assume that we will not be able to perform changes in the collections in both cases. So, what’s their difference? Let’s start with the following code examples to learn how we can “convert” our Dictionary or List collections to either ReadOnly or Immutable.

In C#, a read-only collection is just a wrapper of the actual collection (e.g., the aList in the example), which prevents being modified by not providing the related methods. However, if the actual collection is modified, the ReadOnlyCollection will also be changed. In addition, it’s important to note that read-only collections are not thread-safe.

The System.Collections.Immutable namespace contains interfaces and classes that define immutable collections, such as ImmutableList<T> , ImmutableDictionary<TKey,TValue> , etc. We can assure our consumers that the collection never changes by using immutable collections. In addition, they provide implicit thread safety. Thus, we do not need locks to access the collections. It’s important to notice that the immutable collections provide modification methods (e.g., Add), but they will not make any modifications and create a new set instance.

Tip: Immutable collections can be combined with records (immutable class or struct) to provide complete, immutable data.

So, as we can understand, ReadOnly and Immutable collections are quite different.

.NET provides several collections namespaces that provide great flexibility when working with groups of objects. However, the System.Collections namespace is considered a legacy, and thus, it’s not recommended for new development. The System.Collections.Generic namespace can be used as an alternative.

The System.Collections.Generic namespace provides interfaces and generic collection classes to create collections with the same data type (by enforcing strong typing). It’s the most used collection namespace that provides among others, dictionaries, lists, queues, stacks, and priority queues.

An essential (must-know) namespace when working with concurrent requests (multiple threads) is the System.Collections.Concurrent namespace. It provides a concurrent (thread-safe) version of the known dictionaries, queues, and stacks.

ReadOnly and Immutable collections are quite different, but both provide necessary functionality when we need collections that cannot be changed.

.NET provides awesome collections namespaces (tools) that we can use based on our needs ��.

If you liked this article (or not), do not hesitate to leave comments, questions, suggestions, complaints, or just say Hi in the section below. Don’t be a stranger ��!

Dont’t forget to follow my feed and be a .NET Nakama. Have a nice day ��.

What are Generic Collections in C#? [duplicate]

I’m trying to build my first generic list and have run into some problems. I understand the declaration looks like » List<T> «, and I have using System.Collections.Generic; at the top of my page. However, Visual Studio doesn’t recognize the T variable.

What am I missing?

8 Answers 8

List<T> means List<WhateverTypeYouWantItToBeAListOf> . So for example:

If I have an Employee Class, and I wanted a collection of Employees, I could say:

I could then add Employee Objects to that list, and have it be Type-safe and extend to however many employee objects I put in it.

employeeList now holds emp1 and emp2 as objects.

There are several facets to generic collections, the most important being they provide an object independent way of having a. well. collection of objects. They are type-safe; which means that any collection will consist of one type of object. You won’t have a Animal instance inside of List<Employee> (unless, of course, Employee is a base class that Animal inherits from. At that point, however, you have bigger problems.

Programming with Generics is its own topic, worthy of (at least) one chapter in a book. At a very high level, programming with generics provides another way to reuse code — independent of any class hierarchy or implementation details of a specific class.

Name already in use

docs / docs / csharp / programming-guide / concepts / collections.md

  • Go to file T
  • Go to line L
  • Copy path
  • Copy permalink
  • Open with Desktop
  • View raw
  • Copy raw contents Copy raw contents

Copy raw contents

Copy raw contents

For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects.

Arrays are most useful for creating and working with a fixed number of strongly typed objects. For information about arrays, see Arrays.

Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key.

A collection is a class, so you must declare an instance of the class before you can add elements to that collection.

If your collection contains elements of only one data type, you can use one of the classes in the xref:System.Collections.Generic?displayProperty=nameWithType namespace. A generic collection enforces type safety so that no other data type can be added to it. When you retrieve an element from a generic collection, you do not have to determine its data type or convert it.

[!NOTE] For the examples in this topic, include using directives for the System.Collections.Generic and System.Linq namespaces.

In this topic

Using a Simple Collection

The examples in this section use the generic xref:System.Collections.Generic.List%601 class, which enables you to work with a strongly typed list of objects.

The following example creates a list of strings and then iterates through the strings by using a foreach statement.

If the contents of a collection are known in advance, you can use a collection initializer to initialize the collection. For more information, see Object and Collection Initializers.

The following example is the same as the previous example, except a collection initializer is used to add elements to the collection.

You can use a for statement instead of a foreach statement to iterate through a collection. You accomplish this by accessing the collection elements by the index position. The index of the elements starts at 0 and ends at the element count minus 1.

The following example iterates through the elements of a collection by using for instead of foreach .

The following example removes an element from the collection by specifying the object to remove.

The following example removes elements from a generic list. Instead of a foreach statement, a for statement that iterates in descending order is used. This is because the xref:System.Collections.Generic.List%601.RemoveAt%2A method causes elements after a removed element to have a lower index value.

For the type of elements in the xref:System.Collections.Generic.List%601, you can also define your own class. In the following example, the Galaxy class that is used by the xref:System.Collections.Generic.List%601 is defined in the code.

Kinds of Collections

Many common collections are provided by .NET. Each type of collection is designed for a specific purpose.

Some of the common collection classes are described in this section:

You can create a generic collection by using one of the classes in the xref:System.Collections.Generic namespace. A generic collection is useful when every item in the collection has the same data type. A generic collection enforces strong typing by allowing only the desired data type to be added.

The following table lists some of the frequently used classes of the xref:System.Collections.Generic?displayProperty=nameWithType namespace:

Class Description
xref:System.Collections.Generic.Dictionary%602 Represents a collection of key/value pairs that are organized based on the key.
xref:System.Collections.Generic.List%601 Represents a list of objects that can be accessed by index. Provides methods to search, sort, and modify lists.
xref:System.Collections.Generic.Queue%601 Represents a first in, first out (FIFO) collection of objects.
xref:System.Collections.Generic.SortedList%602 Represents a collection of key/value pairs that are sorted by key based on the associated xref:System.Collections.Generic.IComparer%601 implementation.
xref:System.Collections.Generic.Stack%601 Represents a last in, first out (LIFO) collection of objects.

For additional information, see Commonly Used Collection Types, Selecting a Collection Class, and xref:System.Collections.Generic.

In .NET Framework 4 and later versions, the collections in the xref:System.Collections.Concurrent namespace provide efficient thread-safe operations for accessing collection items from multiple threads.

The classes in the xref:System.Collections.Concurrent namespace should be used instead of the corresponding types in the xref:System.Collections.Generic?displayProperty=nameWithType and xref:System.Collections?displayProperty=nameWithType namespaces whenever multiple threads are accessing the collection concurrently. For more information, see Thread-Safe Collections and xref:System.Collections.Concurrent.

Some classes included in the xref:System.Collections.Concurrent namespace are xref:System.Collections.Concurrent.BlockingCollection%601, xref:System.Collections.Concurrent.ConcurrentDictionary%602, xref:System.Collections.Concurrent.ConcurrentQueue%601, and xref:System.Collections.Concurrent.ConcurrentStack%601.

The classes in the xref:System.Collections?displayProperty=nameWithType namespace do not store elements as specifically typed objects, but as objects of type Object .

Whenever possible, you should use the generic collections in the xref:System.Collections.Generic?displayProperty=nameWithType namespace or the xref:System.Collections.Concurrent namespace instead of the legacy types in the System.Collections namespace.

The following table lists some of the frequently used classes in the System.Collections namespace:

Class Description
xref:System.Collections.ArrayList Represents an array of objects whose size is dynamically increased as required.
xref:System.Collections.Hashtable Represents a collection of key/value pairs that are organized based on the hash code of the key.
xref:System.Collections.Queue Represents a first in, first out (FIFO) collection of objects.
xref:System.Collections.Stack Represents a last in, first out (LIFO) collection of objects.

The xref:System.Collections.Specialized namespace provides specialized and strongly typed collection classes, such as string-only collections and linked-list and hybrid dictionaries.

Implementing a Collection of Key/Value Pairs

The xref:System.Collections.Generic.Dictionary%602 generic collection enables you to access to elements in a collection by using the key of each element. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is fast because the Dictionary class is implemented as a hash table.

The following example creates a Dictionary collection and iterates through the dictionary by using a foreach statement.

To instead use a collection initializer to build the Dictionary collection, you can replace the BuildDictionary and AddToDictionary methods with the following method.

The following example uses the xref:System.Collections.Generic.Dictionary%602.ContainsKey%2A method and the xref:System.Collections.Generic.Dictionary%602.Item%2A property of Dictionary to quickly find an item by key. The Item property enables you to access an item in the elements collection by using the elements[symbol] in C#.

The following example instead uses the xref:System.Collections.Generic.Dictionary%602.TryGetValue%2A method quickly find an item by key.

Using LINQ to Access a Collection

LINQ (Language-Integrated Query) can be used to access collections. LINQ queries provide filtering, ordering, and grouping capabilities. For more information, see Getting Started with LINQ in C#.

The following example runs a LINQ query against a generic List . The LINQ query returns a different collection that contains the results.

Sorting a Collection

The following example illustrates a procedure for sorting a collection. The example sorts instances of the Car class that are stored in a xref:System.Collections.Generic.List%601. The Car class implements the xref:System.IComparable%601 interface, which requires that the xref:System.IComparable%601.CompareTo%2A method be implemented.

Each call to the xref:System.IComparable%601.CompareTo%2A method makes a single comparison that is used for sorting. User-written code in the CompareTo method returns a value for each comparison of the current object with another object. The value returned is less than zero if the current object is less than the other object, greater than zero if the current object is greater than the other object, and zero if they are equal. This enables you to define in code the criteria for greater than, less than, and equal.

In the ListCars method, the cars.Sort() statement sorts the list. This call to the xref:System.Collections.Generic.List%601.Sort%2A method of the xref:System.Collections.Generic.List%601 causes the CompareTo method to be called automatically for the Car objects in the List .

Defining a Custom Collection

You can define a collection by implementing the xref:System.Collections.Generic.IEnumerable%601 or xref:System.Collections.IEnumerable interface.

Although you can define a custom collection, it is usually better to instead use the collections that are included in .NET, which are described in Kinds of Collections earlier in this article.

The following example defines a custom collection class named AllColors . This class implements the xref:System.Collections.IEnumerable interface, which requires that the xref:System.Collections.IEnumerable.GetEnumerator%2A method be implemented.

The GetEnumerator method returns an instance of the ColorEnumerator class. ColorEnumerator implements the xref:System.Collections.IEnumerator interface, which requires that the xref:System.Collections.IEnumerator.Current%2A property, xref:System.Collections.IEnumerator.MoveNext%2A method, and xref:System.Collections.IEnumerator.Reset%2A method be implemented.

An iterator is used to perform a custom iteration over a collection. An iterator can be a method or a get accessor. An iterator uses a yield return statement to return each element of the collection one at a time.

You call an iterator by using a foreach statement. Each iteration of the foreach loop calls the iterator. When a yield return statement is reached in the iterator, an expression is returned, and the current location in code is retained. Execution is restarted from that location the next time that the iterator is called.

For more information, see Iterators (C#).

The following example uses an iterator method. The iterator method has a yield return statement that is inside a for loop. In the ListEvenNumbers method, each iteration of the foreach statement body creates a call to the iterator method, which proceeds to the next yield return statement.

C# Collections

For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects.

Arrays are most useful for creating and working with a fixed number of strongly typed objects.

Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key.

A collection is a class, so you must declare an instance of the class before you can add elements to that collection.

If your collection contains elements of only one data type, you can use one of the classes in the ‘System.Collections.Generic’ namespace. A generic collection enforces type safety so that no other data type can be added to it. When you retrieve an element from a generic collection, you do not have to determine its data type or convert it.

1. Generic collections

Generic collections are the preferred type to use as long as every element in the collection is of the same data type. Only desired data types can be added to a generic collection and this is enforced by using strong typing which reduces the possibility of errors.

The .NET framework provides a number of generic collection classes, useful for storing and manipulating data. To access a generic collection in your code, you will need to include the statement:

1.1 List<T>

A list is similar to an array, but the elements in a list can be inserted and removed dynamically.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *