📜  C#Collections(1)

📅  最后修改于: 2023-12-03 15:00:14.279000             🧑  作者: Mango

C# Collections

In C#, a collection is a group of related objects of a given type. Collections provide a way to store, retrieve, and manipulate objects in a structured way. The C# language provides several built-in collection types, as well as interfaces and classes that you can use to create your own collections.

Built-in Collections

The .NET Framework provides several built-in collection types, including:

1. Array

An array is a fixed-size collection of elements of the same type. Elements in an array are accessed by their index, which starts at 0. Arrays have a fixed length, which cannot be changed once the array is created.

// Declare and initialize an int array with 5 elements
int[] numbers = { 1, 2, 3, 4, 5 }; 

// Access an element in the array by its index
int firstNumber = numbers[0];
2. List

A List is a dynamic collection of elements of the same type. Elements in a List are accessed by their index, which starts at 0. Unlike arrays, Lists can grow or shrink in size dynamically.

// Declare and initialize a List of integers
List<int> numbers = new List<int>();

// Add elements to the List
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);

// Access an element in the List by its index
int firstNumber = numbers[0];
3. Dictionary

A Dictionary is a collection of key-value pairs. Each key in the Dictionary must be unique. Elements in a Dictionary are accessed by their key.

// Declare and initialize a Dictionary of string keys and int values
Dictionary<string, int> ages = new Dictionary<string, int>();

// Add key-value pairs to the Dictionary
ages.Add("John", 25);
ages.Add("Jane", 30);
ages.Add("Bob", 20);

// Access a value in the Dictionary by its key
int janesAge = ages["Jane"];
Collection Interfaces

The .NET Framework also provides collection interfaces, which define the common functionality of related collection types. Some common collection interfaces include:

1. IEnumerable

IEnumerable is the base interface for all non-generic collections that can be enumerated. It defines a method for iterating over the elements in a collection.

// Declare and initialize an array of integers
int[] numbers = { 1, 2, 3, 4, 5 }; 

// Iterate over the elements in the array using IEnumerable
foreach (int number in numbers)
{
    Console.WriteLine(number);
}
2. ICollection

ICollection is the base interface for all non-generic collections that have a Count property and can be modified.

// Declare and initialize a List of integers
List<int> numbers = new List<int>();

// Add elements to the List using ICollection
ICollection<int> collection = numbers;
collection.Add(1);
collection.Add(2);
collection.Add(3);

// Access the number of elements in the collection using Count
int count = collection.Count;
3. IDictionary

IDictionary is the base interface for all non-generic collections of key-value pairs.

// Declare and initialize a Dictionary of string keys and int values
Dictionary<string, int> ages = new Dictionary<string, int>();

// Add key-value pairs to the Dictionary using IDictionary
IDictionary<string, int> dictionary = ages;
dictionary.Add("John", 25);
dictionary.Add("Jane", 30);
dictionary.Add("Bob", 20);

// Access a value in the Dictionary by its key
int janesAge = dictionary["Jane"];
Custom Collections

In addition to the built-in collection types and interfaces, you can create your own custom collections. To do this, you can implement one or more of the collection interfaces or derive from one of the built-in collection classes.

// Define a custom collection that implements the ICollection interface
public class MyCollection<T> : ICollection<T>
{
    private List<T> _items = new List<T>();

    // Implement the ICollection interface methods
    public void Add(T item)
    {
        _items.Add(item);
    }

    public bool Contains(T item)
    {
        return _items.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _items.CopyTo(array, arrayIndex);
    }

    public int Count => _items.Count;

    public bool IsReadOnly => false;

    public bool Remove(T item)
    {
        return _items.Remove(item);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return _items.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
Conclusion

In conclusion, collections are an important part of the C# language and provide a way to store, retrieve, and manipulate objects in a structured way. The .NET Framework provides several built-in collection types and interfaces, and you can create your own custom collections by implementing the collection interfaces or deriving from the built-in collection classes.