7. Collections || C# 10 Flashcards
What the IEnumerator interface do?
Defines the basic low-level protocol by which elements in a collection are traversed in a forward-only manner
~~~
public interface IEnumerator{
bool MoveNext();
object Current {get; }
void Reset();
}
~~~
It also inherits from IDisposable
which ensure that resources are released once the enumeration is completed
When would you implement IEnumerable<T> interface?</T>
- To support the foreach statement
- To interoperate with anything expecting a standard collection
- To meet the requirements of a more sophisticated collection interface
- To support collection initializers
To implement IEnumerable
/IEnumerable<T>
you must provide an enumerator. How would you do that?
You can do it in one of 3 ways:
1. If the class is “wrapping” another collection, by returning the wrapped collection’s enumerator
2. Via an iterator using yield return
3. By instantiating your own IEnumerator
/IEnumerator<T>
implementation
Can you name at least 5 types that resides under Sysyem.Collection.Generic
?
Dictionary<TKey,TValue>
HashSet<T>
List<T>
LinkedList<T>
SortedDictionary<TKey,TValue>
SortedList<TKey,TValue>
SortedSet<T>
Stack<T>
What is ICollection<T>
and ICollection
? How does the methods differ between them?
ICollection<T>
is the standart interface for countable collections of objects. It provides the ability to determine the size of a collection (Count), copy the collection into an array (ToArray) and determine whether the collection is read-only (IsReadOnly). Since it extends IEnumerable<T>
it can also be traversed via foreach.
For writable collection you can also use Add, Remove and Clear methods
ICollection
is nongeneric interface which is simillar in providing a countable collection, but it doesnt provide functionality for altering the list or checking for element membership:
Has only 1 method - CopyTo ant 3 parameters
How would you define IList<T>
type?
IList<T>
is the standart interface for collections indexable by position. In addition to the functionality inherited from ICollection<T>
and IEnumerable<T>
it provides the ability to read or write an element by position (via indexer) and insert/remove by position.
Nongerenic IList has more methods than IList<T>
because it inherits less from ICollection
IList<T>
:
int IndexOf(T item)
void Insert (int index, T item)
void RemoveAt (int index)
IList
:
bool IsFixedSize {get; }
bool IsReadOnly {get; }
int Add (object value)
void Clear ()
bool Contains (object value)
int IndexOf (object value)
void Insert (int index, object value)
void Remove (object value)
void RemoveAt (int index)
Why do we need IReadOnlyColletion<T>
and IReadOnlyList<T>
?
This collection and list interfaces are designed for exposing the members required for read-only operations
What is the differences between an array and List?
Size: An array has a fixed size, meaning its size cannot be changed once it has been created. A List, on the other hand, has a dynamic size and can grow or shrink as needed.
Type: An array must be of a single data type, while a List can hold elements of any object type.
Indexing: Both arrays and Lists allow you to access elements using an index, but a List provides additional methods for inserting, removing, and searching elements.
Performance: Arrays have faster index-based access than Lists. However, Lists are generally faster for inserting and removing elements, especially in the middle of the collection.
Syntax: The syntax for working with arrays and Lists is slightly different, with Lists providing a more user-friendly and convenient API for common collection operations.
In general, if you need a collection of elements with a fixed size and a specific data type, use an array. If you need a dynamic-size collection that can hold elements of any type, use a List.
What are the differences between these array methods: Clone() and CopyTo() ?
The Clone() and CopyTo() methods in C# are both used to copy arrays, but they have some key differences:
Clone(): The Clone() method creates a shallow copy of the original array, meaning that a new array is created with the same elements as the original, but the elements themselves are not copied. Instead, the new array holds references to the same elements as the original. This means that changes to the elements in the new array will affect the elements in the original array.
CopyTo(): The CopyTo() method creates a shallow or deep copy of an array, depending on the type of elements in the array. If the elements are value types, a deep copy of the elements is made, meaning that changes to the elements in the new array will not affect the elements in the original array. If the elements are reference types, a shallow copy of the elements is made, meaning that changes to the elements in the new array will affect the elements in the original array.
In general, if you need to make a new array with independent elements, use CopyTo() with value types. If you need to create a new array that shares elements with the original array, use Clone() or CopyTo() with reference types.
How does the array method Resize() work?
Changes the number of elements of a one-dimensional array to the specified new size
public void Resize<T> (ref T[]? array, int newSize);
The Resize method works by creating a new array and copying over the elements, returning the new array via the reference parameter. Any references to the original array in other objects will remain unchanged.</T>
Let’s say we have a List and ArrayList with value type elements inside. Which is faster? Why?
List<T>
will be faster with value type elements because List<T>
(if T is value type) avoids the overhead of boxing and unboxing elements
What is a LinkedList<T>
? What does it implement?
LinkedList<T>
is a generic doubly linked list. A doubly linked list is a chain of nodes in which each references the node before, the node after and the actual element. Its main benefit is that an element can always be inserted efficiently anywhere in the list, because it just involves creating a new node and updating a few references, but the task of finding the place where to insert the node can be slow, because there is no indexing in linkedList; each node must be traversedLinkedList<T>
implements IEnumerable<T>
and ICollection<T>
What does ‘concurrent’ mean?
The term “concurrent” refers to the ability of multiple processes or threads to execute simultaneously, either interleaved on a single processor or running on multiple processors. In computer science, “concurrent” is often used to describe operations that can run at the same time, independently, and without affecting each other’s results.
In the context of data structures, “concurrent modification” refers to the ability to modify the structure (e.g., add or remove elements) while it is being used or enumerated by another process or thread. A data structure that supports concurrent modification is designed to be safe to modify even while it is being used by other processes or threads, without causing errors or inconsistent results.
What is Queue? What are the main methods?
Queue is a First-In-First-Out (FIFO) data structure;
Main methods:
1. Enqueue (add element to the tail(end) if queue)
2. Dequeue (reveal and remove an element from the head(start) of the queue
3. Peek (reveal an element at the head, without removing it)
Queue does not implement IList interface since we can’t access items directly by indexes
What is a Stack? What is the main methods?
Stack is a Last-In First-Out data structure.
1. Push (add element at the top of the stack)
2. Pop (Reveal and remove an element at the top of the stack)
3. Peek (reveal an element at the top of the stack without removing it