What is an Array?
An array is a group of variables of the same type that are referred to by a common name. Each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. The length of the array specifies the number of elements present in the array. In C# the allocation of memory for the arrays is done dynamically.
**What is a List?
**A list is a class that represents a strongly typed list of objects that can be accessed by index.
It can be used to create a collection of different types like integers, strings etc. The List class also provides the methods to search, sort, and manipulate lists.
What is iEnumerable?
IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows readonly access to a collection then a collection that implements IEnumerable can be used with a for-each statement. This method is used to go through all the items in a given collection.
METHODS
1. ARRAYS
• Clear(Array) - Clears the contents of an array.
• Clone() - Creates a shallow copy of the Array.
• Empty() - Returns an empty array.
• Equals(Object) - Determines whether the specified object is equal to the current object.
• Fill(T[], T) - Assigns the given value of type T to each element of the specified array.
2. LISTS
• Add(T) - Adds an object to the end of the List.
• Clear() - Removes all elements from the List.
• Contains(T) - Determines whether an element is in the List.
• Remove(T) - Removes the first occurrence of a specific object from the List.
**
- IENUMERABLE** • GetEnumerator() - Returns an enumerator that iterates through a collection. • Cast(IEnumerable) - Casts the elements of an IEnumerable to the specified type. • OfType(IEnumerable) - Filters the elements of an IEnumerable based on a specified type. • AsParallel(IEnumerable) - Enables parallelization of a query. • AsQueryable(IEnumerable) - Converts an IEnumerable to an IQueryable.
SIMILARITIES BETWEEN ARRAYS, LISTS AND IENUMERABLES
- Both Array and IEnumerable are used to store collections of elements, and both can be used with the foreach loop to iterate through a collection.
- Both Array and IEnumerable have a fixed size, meaning the number of elements they can hold cannot be changed after they are created.
- The content of both Lists and Arrays can be modified(addition and deletion of variables)
- A List is a concrete implementation of the IEnumerable interface, which means that it includes the members defined by the interface and provides additional functionality such as adding and removing items
- Both Lists and IEnumerable can be used to iterate over a collection of items in a foreach loop, and they both output the same thing.
DIFFERENCES BETWEEN ARRAYS, LISTS AND IENUMERABLES
- A List can be resized dynamically but Arrays and IEnumerable cannot.
- Array is a data structure, a List is a class while IEnumerable is an interface.
- IEnumerable is a read-only interface, meaning you can’t add or remove items from the collection you can only iterate over the items while Lists and Arrays can be modified
- Array is a reference type that provides methods to access elements by index and the property called Length which gives the number of elements in the array. IEnumerable is an interface that defines a single method, GetEnumerator, to go through all the items in a collection.
- Arrays have a fixed size, which means they occupy a specific amount of memory, even if not all slots are used. Lists, being dynamic, use memory proportional to the number of elements they contain. If memory optimization is crucial, Arrays may have an advantage.
- Arrays are generally faster for accessing elements because they provide direct indexing. Lists, while still fast, involve a layer of abstraction due to their dynamic nature.
Choosing between Lists, Arrays and IEnumerable in C# coding depends on your specific requirements. Lists offer flexibility and ease of use, making it a solid choice for dynamic collections. Arrays provide predictable memory usage and quick access, making them suitable for fixed-size collections. IEnumerable is best for read-only collections that do not need modification and is best used with foreach loops.
Ultimately, the decision should be based on your needs. Whether you opt for Lists, Arrays, iEnumerables or a combination, understanding the strengths and weaknesses of these data structures is essential for optimizing your project’s performance and delivering a seamless experience to users.
Top comments (0)