What are generics in C#?
Generics allow classes, interfaces, methods, delegates, and other types to operate with type parameters instead of being written for one specific data type.
C# interview questions covering generics & collections.
Open a question for the full answer, code and interview guidance.
Generics allow classes, interfaces, methods, delegates, and other types to operate with type parameters instead of being written for one specific data type.
Generic constraints restrict which types can be supplied for a generic type parameter. They allow generic code to safely use capabilities guaranteed by the constraint.
A collection is an object used to store and manage groups of values or objects. .NET provides generic collections in System.Collections.Generic for common data structures.
An array has a fixed length after creation, while List<T> is a dynamically sized generic collection.
List<T> is a generic, dynamically sized collection that stores elements of a specified type. It preserves insertion order and supports indexed access.
Dictionary<TKey,TValue> stores data as key-value pairs. Each key must be unique, and values are retrieved using their associated keys.
IEnumerable<T> represents a sequence that can be enumerated one element at a time. It is the fundamental generic interface used by foreach and many LINQ operations.
IEnumerable<T> provides basic sequential enumeration. ICollection<T> extends IEnumerable<T> and represents a collection that exposes additional operations and information.
IEnumerable<T> represents an enumerable sequence and its LINQ operations normally execute as .NET code over objects.
Stack<T> and Queue<T> are generic collections designed for different processing orders.