1.5 8_Collections_and_Generics_Descriptive_Questions.pdf
1.5 8_Collections_and_Generics_Descriptive_Questions.pdf
1. What is Collection?
Answer: A collection is a set of similarly typed objects that are grouped together. A collection is an object by
itself, which would manage other objects eg: ArrayList, HashTable, Stack, Queue, LinkedList, SortedList etc.
8. What is NamedValueCollection?
Answer: NameValueCollection allows many values for one key. It is found in System.Collections.Specialized. It
does not provide excellent performance.
1. We used NameValueCollection in the C# language. The collection is located in the
System.Collections.Specialized namespace. It allows you to associate one string key with multiple string values.
2. The collection concatenates values with commas, and returns string arrays.
Examples In Usage: QueryString,appSettings.
9. What is StringDictionary?
Answer: StringDictionary is a specialized collection. It is found in the System.Collections Specialized namespace. It
only allows string keys and string values. It is limited.
Drawbacks:
1. The key is handled in a case-insensitive manner. it is translated to lowercase before it is used with the string
dictionary.
2. To sort (or iterate) over a StringDictionary in order of Value (not Key)
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________
2. Yield is not a feature of the .Net runtime. It is just a C# language feature. During compilation Compiler converts
yield method and its class to instances of IEnumerable and IEnumerator implemented classes.
3. You use a yield return statement to return each element one at a time.
4. You can use a yield break statement to end the iteration.
5. You consume an iterator method by using a foreach statement or LINQ query. Each iteration of the foreach
loop calls the iterator method. When a yield return statement is reached in the iterator method, expression is
returned, and the current location in code is retained. Execution is restarted from that location the next time that
the iterator function is called.
ArrayList:
1. They are resizable and variable length.
2. They are flexible and can accommodate any data types.
3. In arraylist lots of boxing and unboxing are done there for its performance is slower.
42. Which method we used to the One ArrayList elements copy to another ArrayList at particular index ?
Answer: InsertRange();//Based on index
43. Which method we used to the One ArrayList elements copy to another ArrayList?
Answer: AddRange();//Not based on index it copy the one arraylist elements into another arralist.
47. Can we create Hastable object declaration and initialization at the same time?
Answer: Yes, You can also assign key and value at the time of initialization using object initializer
Example: Hashtable ht = new Hashtable()
{
{ 1, "One" }
});
Answer: Yes, Add() will throw an exception if you try to add a key that already exists in the Hashtable. So always
check the key using the Contains() or ContainsKey() method before adding a key-value pair into the Hashtable.
49. Can retrive the value of an existing key from the Hashtable using indexer.?
Answer: Yes.
51. Hashtable stores key-value pairs of any datatype where the Key must be unique. ?
Answer: Yes
52. The Hashtable key can be null whereas the value cannot be null.
Answer: No
58. What method we used to find the top most value in Stack?
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________
Answer: Peek();
63. Which class must be used as for implementing a custom dictionary non generic collection class?
Answer: DictionaryBase
1. If operands are Value Types and their values are equal, it returns true else false. 2. If operands are Reference
Types with exception of string and both refer to same object, it returns true else false. 3. If operands are string
type and their values are equal, it returns true else false.
.Equals:
1. If operands are Reference Types, it performs Reference Equality that is if both refer to same object, it returns
true else false. 2. If Operands are Value Types then unlike == operator it checks for their type first and If their
types are same it performs == operator else it returns false.
69. Which collection class is suitable for storing large number of key value pairs?
Answer: Hashtable
--
71. What are the Generic Collections?
Answer: C# includes generic collection classes in the System.Collections.Generic namespace. The following are
widely used generic collections:
1. List<T>
2. Dictionary<TKey,TValue>
3. Queue<T>
4. Stack<T>
5. Hashset<T>
74. How to create Generic interface,Generic method and Generic Class in C#?
Answer:
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________
75. How to create an object for List<T> and Dictionary<Tkey,Tvalue> Class in C#?
Answer: List<T>: List<int> intList = new List<int>();
IList<int> intList = new List<int>();
Dictionary<Tkey,Tvalue>: Dictionary<int, string> dict = new Dictionary<int, string>();
IDictionary<int, string> dict = new Dictionary<int, string>();
90. which keyword is used to for specifying constraints for generics type parameters?
Answer: Where