Effective C # Principle 40: Select a collection based on requirements

Source: Internet
Author: User
Tags foreach arrays data structures tostring

"What kind of collection is the best?" The answer is: "Depending on the situation." "Different sets have different performance and have different optimizations for different behaviors." NET Framework supports many similar collections: Lists, arrays, queues, stacks, and other collections. C # supports multidimensional arrays, and its performance differs from one-dimensional arrays and jagged arrays. NET Framework also contains a number of special collections, which you should see carefully before you create your own collection classes. You can see that a lot of collections are very fast because all of the collections implement the ICollection interface. All the collections that implement the ICollection interface are listed in the documentation, and you have nearly 20 collection classes available.

In order to select the set that is appropriate for you, you need to consider the operations that are used on that collection. To create a scalable program, you need to use a collection class that implements the interface, so that when you find that a hypothetical set is incorrect, you can replace it with a different set (see Principle 19).

. NET Framework has three different types of collections: arrays, collections of similar arrays, and collections based on hash values. Arrays are the simplest and fastest set in general, so we start with it. This is also the collection class that you will often use.

Your first choice should often be the System.Array class, which, exactly, should be an array-type class. The first and most important reason for selecting an array class is that the array is type-safe. All other collections are stored System.Object references until c#2.0 is introduced to the paradigm (see Principle 49). When you declare an array, the compiler creates a special System.Array derived class for your type. For example, this creates a declaration and creates an array of integers:

private int [] _numbers = new int[100];

This array stores an integral type, not a System.Object reference. This is important because you can avoid the performance loss of boxing and unboxing operations by adding, accessing, and deleting value types on an array (see Principle 17). The initialization above creates a one-dimensional array containing 100 integers. All the memory consumed by the array was cleared 0. The array of value types is all 0, and the reference array is null. All elements in the array can be accessed by index:

int j = _numbers[ 50 ];

Alternatively, you can use a foreach iteration when accessing an array, or an enumerator:

foreach ( int i in _numbers )
 Console.WriteLine( i.ToString( ) );
// or:
IEnumerator it = _numbers.GetEnumerator( );
while( it.MoveNext( ))
{
  int i = (int) it.Current;
 Console.WriteLine( i.ToString( ) );
}

If you are prepared to store objects in a single order, you should use an array. But in fact your data structures tend to be much more complex than that. This quickly lured us back to the C-style jagged array, which is to store another array in an array. Sometimes this is exactly what you want, and each outer element is an array of inner layers:

public class MyClass
{
 // Declare a jagged array:
 private int[] [] _jagged;
 public MyClass()
 {
  // Create the outer array:
  _jagged = new int[5][];
  // Create each inner array:
  _jagged[0] = new int[5];
  _jagged[1] = new int[10];
  _jagged[2] = new int[12];
  _jagged[3] = new int[7];
  _jagged[4] = new int[23];
 }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.