What are collections and generics?
A collection can be defined as a group of related items that can be referred to as a single unit. The System.Collections namespace provides you with many classes and interfaces. Some of them are - ArrayList, List, Stack, ICollection, IEnumerable, and IDictionary.
Generics provide the type-safety to your class at the compile time. While creating a data structure, you never need to specify the data type at the time of declaration. The System.Collections.Generic namespace contains all the generic collections.
Generics provide the type-safety to your class at the compile time. While creating a data structure, you never need to specify the data type at the time of declaration. The System.Collections.Generic namespace contains all the generic collections.
What is the difference between arrays and collection?
Array:
Collection:
- You need to specify the size of an array at the time of its declaration. It cannot be resized dynamically.
- The members of an array should be of the same data type.
Collection:
- The size of a collection can be adjusted dynamically, as per the user's requirement. It does not have fixed size.
- Collection can have elements of different types.
What is a Hashtable?
Hashtable is a data structure that implements the IDictionary
interface. It is used to store multiple items and each of these items
is associated with a unique string key. Each item can be accessed using
the key associated with it. In short, hashtable is an object holding the
key-value pairs.
Example:-
Example:-
Output:-
No comments:
Post a Comment