Why to use Generics? Why did .NET introduce generic collections?
The immediate benefit of using Generics is Type Safety. They are also improved in performance as compared to collections without generics.
Give the mapping of similar classes from Collections to Generic Collections?
· List<T> -> Arraylist
· Dictionary<TKey, TValue> and ConcurrentDictionary<TKey, TValue> are the generic classes that correspond to Hashtable.
· Collection<T> is the generic class that corresponds to CollectionBase. Collection<T> can be used as a base class, but unlike CollectionBase, it is not abstract. This makes it much easier to use.
· ReadOnlyCollection<T> is the generic class that corresponds to ReadOnlyCollectionBase. ReadOnlyCollection<T> is not abstract, and has a constructor that makes it easy to expose an existing List<T> as a read-only collection.
· The Queue<T>, ConcurrentQueue<T>, Stack<T>, ConcurrentStack<T>, and SortedList<TKey, TValue> generic classes correspond to the respective nongeneric classes with the same names.
Collections are derived from which Classes?
To manage a collection, use the System.Array class and the classes in the System.Collections, System.Collections.Generic, and System.Collections.Concurrent namespaces to add, remove, and modify either individual elements or a range of elements in the collection.
Collections are based on the ICollection interface, the IList interface, the IDictionary interface, or their generic counterparts.
How to have efficient Thread Safe add, removing of items in Collections from Multiple Threads?
This can be achieved by using System.Collections.Concurrent namespace
What is Collection?
Collection is similar type of objects grouped together.
What are three types of Collection categorization?
· Commonly used collections: These are the common variations of data collections, such as hash tables, queues, stacks, dictionaries, and lists. Many commonly used collections have non-generic versions, generic versions, and thread-safe generic versions.
· Bit collections: These are collections whose elements are bit flags. They behave slightly differently from other collections.
· Specialized collections: These are collections with highly specific purposes, usually to handle a specific type of element, such as StringDictionary.
What collection to use when you need a sequential list where the element is typically discarded after its value is retrieved?
Queue or Stack
If need random access to Items?
Use LinkedList
If Need access to elements by Index?
ArrayList, StringCollection
If Need access to elements by Key?
Use Dictionary.
If you have a requirement to have following:
One Value only: List
One Key and One Value: Dictionary
One Key and Multiple Values: NameValueCollection class
If required to sort elements?
HashTable sorted by hash values
SortedList
ArrayList and List<T> provide for sort method.
What If you need fastsearch and retrival of information?
ListDictionary is faster than Hashtable for small collections (10 items or fewer). The Dictionary<TKey, TValue> generic class provides faster lookup than the SortedDictionary<TKey, TValue> generic class. The multi-threaded implementation is ConcurrentDictionary<TKey, TValue>. ConcurrentBag<T> provides fast multi-threaded insertion for unordered data.
What If collection only string?
Use StringCollection.
Explain Enumeration in Collection?
Every enumerator is based on the IEnumerator interface or the IEnumerator<T> generic interface, which requires the following members:
· The Current property points to the current member in the collection.
· The MoveNext property moves the enumerator to the next member in the collection.
· The Reset property moves the enumerator back to the beginning of the collection. Current is positioned before the first element. Reset is not available in the generic IEnumerator<T> interface.
In List<T> how is the equality compared decided?
If T implements the IEquatable<T> Interface than it uses the Equals(T) method else its uses the Object.Equals(Object) method.
Is performance of ArrayList and List identical for reference type and why?
Yes the performance of using an ArrayList and List is identical for both as there is boxing or unboxing involved in Arraylist.
For value types why is List better than ArrayList?
First, its type safe. Every value type which is entered in the ArrayList is boxed to the parent type of Object. While retrieving the same element the Object has to be unboxed. This causes an unwanted overhead. This can be removed by using the List<T>.
Is a concurrent list type provided by System.Collections.Concurrent?
The System.Collections.Concurrent namespace does not provide a concurrent list type, but it does provide a ConcurrentQueue<T> and ConcurrentStack<T> type.
What are advantadges of using Arrays over ArrayList and List<T>?
· You can set the lower bound of Array, the lower bound of ArrayList and List is always zero
· Multiple dimension are supported by Arrays.
· It provides better performance than the others as Arraylist is trapped in the boxing, unboxing fundas. List can compete with performance if the Capacity attribute is good approximation of the upper bound of the List
What is a HashTable?
A Hashtable object consists of buckets that contain the elements of the collection. A bucket is a virtual subgroup of elements within the Hashtable, which makes searching and retrieving easier and faster than in most collections. Each bucket is associated with a hash code, which is generated using a hash function and is based on the key of the element.
Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be null, but a value can be.
// Create a new hash table.
//
Hashtable openWith = new Hashtable();
// Add some elements to the hash table. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The default Item property can be used to change the value
// associated with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
foreach( DictionaryEntry de in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
openWith.Remove("doc");
What is HashSet<T>?
The HashSet<T> class is based on the model of mathematical sets and provides high-performance set operations similar to accessing the keys of the Dictionary<TKey, TValue> or Hashtable collections. In simple terms, the HashSet<T> class can be thought of as a Dictionary<TKey, TValue> collection without values.
Is Array part of System.Collections?
No. But it’s a collection as it’s based on IList interface.
What is Rank of an Array?
Rank is the dimensions of Array.
1 comment:
Collections & Generic Collections are the most important concept in dotnet and the statement which is given as To manage a collection in C# Online Training, use the System.Array class and the classes in the System.Collections, System.Collections.Generic, and System.Collections.Concurrent namespaces to add, remove, and modify either individual elements or a range of elements in the collection.
Post a Comment