0% found this document useful (0 votes)
15 views13 pages

1.5 8_Collections_and_Generics_Descriptive_Questions.pdf

Uploaded by

mosalah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

1.5 8_Collections_and_Generics_Descriptive_Questions.pdf

Uploaded by

mosalah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Deccansoft Software Services-MS.

NET Collections and Generics


____________________________________________________________________________________

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.

2. What is the use of Collections?


Answer: Instead of writing separate code to handle each individual object, you can use the same code to process
all the elements of a collection.

3. How many ways Collection classes can be categorized?


Answer: Collections classes can generally be categorized into 5 types:
1. Commonly used collections.
2. Bit collections.
3. Specialized collections.
4. Generic collections.
5. Concurrent collections.

4. What are the Commonly used collections.?


Answer: These are the common variations of data collections, such as hash tables, queues, stacks,ArrayList.

5. What are the Bit collections?


Answer: These are collections whose elements are bit flags. They behave slightly differently from other
collections.
Example: BitArray Class

6. When we will use BitArrayClass Collection?


Answer: The BitArray class manages a compact array of bit values, which are represented as Booleans, where
true indicates that the bit is on (1) and false indicates the bit is off (0). It is used when you need to store the bits
but do not know the number of bits in advance. You can access items from the BitArray collection by using an
integer index, which starts from zero.

7. What are the Specialized collections?


Answer: These are collections with highly specific purposes, usually to handle a specific type of element, such as
NameValueCollection, StringDictionary and StringCollection.

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
____________________________________________________________________________________

Example: Key - Value


1-X
2-A
3-O
Output:
2-A
3-O
1-X

10. What is StringCollection?


Answer: StringCollection class defined in the System.Collections.Specialized namespace represents a collection of
strings and provides functionality to manage the collection.
Drawbacks:
1. StringCollection accepts null as a valid value and allows duplicate elements.
2. String comparisons are case-sensitive.
3. Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.

11. What is IEnumerable Interface?


Answer: IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator
interface, this allows readonly access to a collection then collection that implements IEnumerable can be used
with a for-each statement.
1. IEnumerable interface contains in the System.Collections.Generic namespace.
2. IEnumerable interface is generic interface which allows to looping over generic or non-generic list.
3. IEnumerable interface also works with linq query expression.
4. IEnumerable interface Returns an enumerator that iterates through the collection.

12. What is GetEnumerator?


Answer: GetEnumerator which returns an IEnumerator interface, this allows readonly access to a collection then
collection that implements IEnumerable.

13. What is IEnumerator Interface?


Answer: IEnumerator is an interface which helps to get current element from the collection,its has following two
methods
1. MoveNext()
2. Reset()

14. What is MoveNext?


Answer: Sets the enumerator to the next element of the collection, it Returns true if the enumerator was
successfully sets to the next element and false if the enumerator has ed the end of the collection.

15. What is Reset?


Answer: Sets the enumerator to its initial position, which is before the first element in the collection.

16. What is the use of Current property in IEnumerator?


Answer: Gets the current element in the collection.

17. What is the Yield Keyword?


Answer: 1. Yield keyword is introduced by C# 2.0 in order to simplify implementation iterator pattern in your
custom objects.
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.

18. What are the Concurrent Collections?


Answer: The .NET framework offers some collection classes specifically used in multithreading. These collections
are internally used synchronization hence we can call them thread safe collections. These collections can be
accessed by multiple threads at a time hence they are called concurrent collections.
1. ConcurrentStack<T>
2. ConcurrentQueue<T>
3. BlockingCollection<T>
4. ConcurrentBag<T>
5. ConcurrentDictionary<TKey,T>

19. What is the use of Concurrent Collections?


Answer: Earlier, .NET had collections to represent data in memory but these collections were not type safe; a
collection when defined can have any possible type of object in it, which meant we had to explicitly type cast
back to the original type before performing any operation. .NET2.0 introduced Generics in collections and type
safety was ensured. Supplying the proper type is ensured at compile time only, it will not wait until run time blow
up. It also has one glitch, it is type safe but not thread safe - when two or three threads simultaneously access a
collection, then we need to explicitly lock the collection so that only one thread can be entered at a particular
point of time for data modification. After the introduction of the new System.Collections.Concurrent namespace,
developers no longer have to worry about locking and unlocking objects. Concurrent collections are meant for
thread safety - all the locking mechanism is handled inside the collections.Concurrent collections allow us to
write thread safe code without actually worrying about the technical handling of synchronization.

20. What is ICollection Interface?


Answer: The ICollection interface is inherited from the IEnumerable interface which means that any class that
implements the ICollection interface can also be enumerated using a foreach loop. In the IEnumerable interface
we don't know how many elements there are in the collection whereas the ICollection interface gives us this
extra property for getting the count of items in the collection.

21. What are the properties and methods contains ICollection?


Answer: The ICollection interface contains the following:
1. Count Property
2. IsSynchronized Property
3. SyncRoot Property
4. CopyTo Method

22. What is the use of Count Property?


Answer: The Count property is used for maintaining the count of elements in the list.

23. What is the use of IsSynchronized Property and SyncRoot Property?


Answer: IsSysnchronized and SyncRoot properties help to make the collection thread-safe.
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

24. What is the use of CopyTo method?


Answer: The CopyTo method copies the entire collection into an array.

25. What is IList Interface?


Answer: The IList interface implements both ICollection and IEnumerable interfaces. This interface allows us to
add items to and remove items from the collection. It also provides support for accessing the items from the
index. This interface has more power than the preceding two interfaces.
1. The IList interface has one indexer by which we can access any element by its position and can insert an
element and remove an element at any position.

26. What are the property's and methods in IList?


Answer: The IList interface contains the following:
1. IsFixedSize Property
2. IsReadOnly Property
3. Indexer
4. Add Method
5. Clear Method
6. Contains Method
7. Indexof Method
8. Insert Method
9. Remove Method
10. RemoveAt Method

27. What is the difference between IEnumerable and ICollection?


Answer: The basic difference between ICollection and IEnumerable is that ICollection is a write operation and
IEnumerable is a read-only operation only meant for iteration.

28. When to use IEnumerableI?


Answer:
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
1. The only thing you want is to iterate over the elements in a collection. You only need read-only access to that
collection.
Example: It is important to know that the C# language foreach keyword works with all types that implement the
IEnumerable interface. Only in C# it also works with things that don’t explicitly implement IEnumerable or
IEnumerable<T>.

29. When to use ICollection?


Answer: You want to modify the collection or you care about its size.
public interface ICollection<T> : IEnumerable<T>, IEnumerable
{
int Count { get; }
bool IsReadOnly { get; }

void Add(T item);


void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
bool Remove(T item);
}
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

30. When to use IList?


Answer: You want to modify the collection and you care about the ordering and / or positioning of the elements
in the collection.
public interface IList : ICollection, IEnumerable
{
bool IsFixedSize { get; }
bool IsReadOnly { get; }
Object this[int index] { get; set; }
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);
}

31. Description of ICollection Methods and Properties?


Answer:
1. Count – Count gives number of elements in collection.
2. IsReadOnly – States whether collection is read only or not if collection is read only than add, remove and clear
through exception
3. Add- Add element to collection.
4. Clear- Removes all elements from collection.
5. Contains- Check whether an element exits in collection or not (return true or false).
6. CopyTo- Copy element to an array.
7. Remove- Removes an element from collection.

32. Description of IList Methods and Properties?


Answer:
1. this- It will return element through indexing.
2. IsFixedSize –Tells collection is fixed size or not.
3. IsReadOnly- States whether collection is read only or not if collection is read only than add ,remove and clear
through exception.
4. Add- Add element to collection and return index of that element.
5. Clear- Removes all element from collection
6. Contains- Check an element in a collection exits or not.
7. IndexOf- Return index of an element.
8. Insert- Insert element at a index.
9. Remove- Remove an element
10. RemoveAt- Remove element from an index.

33. What is an ArrayList?


Answer: 1. It is basically an alternative to an array. However, unlike array you can add and remove items from a
list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory
allocation, adding, searching and sorting items in the list.
2. Implements the IList interface using an array whose size is dynamically increased as required.
3. Elements in this collection can be accessed using an integer index.
4. More thread safety.

34. What are the Properties of ArrayList Class?


Answer: 1. Capacity: Gets or sets the number of elements that the ArrayList can contain.
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

2. Count: Gets the number of elements actually contained in the ArrayList.


3. IsFixedSize: Gets a value indicating whether the ArrayList has a fixed size.
4. IsReadOnly: Gets a value indicating whether the ArrayList is read-only.
5. Item: Gets or sets the element at the specified index.

35. Explain an Important Methods of ArrayList Class?


Answer:Add(object value): Adds an object to the end of the ArrayList.
AddRange(ICollection c): Adds the elements of an ICollection to the end of the ArrayList.
Contains(object item): Determines whether an element is in the ArrayList.
GetRange(int index, int count): Returns an ArrayList which represents a subset of the elements in the source
ArrayList.
IndexOf(object): Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of
it.
Insert(int index, object value): Inserts an element into the ArrayList at the specified index.
InsertRange(int index, ICollection c): Inserts the elements of a collection into the ArrayList at the specified index.
Remove(object obj): Removes the first occurrence of a specific object from the ArrayList.
RemoveAt(int index): Removes the element at the specified index of the ArrayList.
SetRange(int index, ICollection c): Copies the elements of a collection over a range of elements in the ArrayList.
Sort(): Sorts the elements in the ArrayList.
TrimToSize(): Sets the capacity to the actual number of elements in the ArrayList.

36. How to create an ArrayList?


Answer: To create the ArrayList the following code is used:
ArrayList arr = new ArrayList();
Note: The datatype of an ArrayList is object type, so we can add the elements having the datatype string, integer
and any other.

37. What are the limitations of ArrayList?


Answer: Follow the below example code.
ArrayList myList = new ArrayList ();
myList.Add (22);
myList.Add ("C# Generics");
myList.Add (22.45);
1. Any reference or value type that is stored or added in the ArrayList (myList) is implicitly upcasted to
System.Object type.
2. You do not have the type safety with ArrayList.
3. The performance issue is in the need to cast objects back to the original
4. It allows duplicate elements.
5. Comipler checks at runtime.

38. How to check an element is existed or not in ArrayList?


Answer: Using Contains() method.

39. What is the difference between Compiletime and Runtime errors?


Answer: Compile-time and run-time usually refers to when checks occur or when errors can happen. For
example, in a statically typed language like C# the static type checks are made at compile time. That means that
you cannot compile the application if you for example try to assign a string to an int-variable. Run-time on the
other hand refers to the time when the code is actually executed. For example exceptions are always thrown at
run-time.

40. What is the method to sort the elements in Arraylist?


Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

Answer: Sort() Method.

41. What is the difference between Array and ArrayList?


Answer: Array:
1. They are fixed length. .
2. They are compiled strong type collection.
3. performance is faster.

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.

44. What is HashTable?


Answer: 1. Represents a collection of key/value pairs that are organized based on the hash code of the key.
2. Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be a null reference, but a
value can be.
3. The capacity of a Hashtable is the number of elements the Hashtable can hold. The default initial capacity for a
Hashtable is 0. As elements are added to a Hashtable, the capacity is automatically increased as required through
reallocation with the default capacity on adding first item set to 4. In HashTable Key must be unique but value
need not be unique.
5. C# includes Hashtable collection in System.Collections namespace, which is similar to generic Dictionary
collection. The Hashtable collection stores key-value pairs. It optimizes lookups by computing the hash code of
each key and stores it in a different bucket internally and then matches the hash code of the specified key at the
time of accessing values.

45. How to create an Hashtable object?


Answer: Example: Hashtable ht=new Hashtable();
ht.Add(key object,value object);
Note: The Add() method adds an item with a key and value into the Hashtable. Key and value can be of any data
type. Key cannot be null whereas value can be null.

46. What is keys and values property use in Hashtable?


Answer: Keys: Gets an ICollection of keys in the Hashtable. Values: Gets an ICollection of values in the Hashtable.

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" }
});

48. Hashtable can include all the elements of Dictionary ?


Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

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.

50. What is DictionaryEntry?


Answer : Hashtable elements are key-value pairs stored in DictionaryEntry. DictionaryEntry is a struct which is
capable of storing a key and value. DictionaryEntry is useful while retrieving data from Dictionary based collection
classes.

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

53. What is the GetHashCode method?


Answer: Serves as the default hash function. In Hashtable, the hashCode method only gets invoked when you
use the Object as the key to a Hashtable. It is not used when the Object is a Hashtable value.

54. Can we do explicitly key should be unique in custom classes?


Answer: Yes, If you override the GetHashCode method, you should also override Equals, and vice versa. If your
overridden Equals method returns true when two objects are tested for equality, your overridden GetHashCode
method must return the same value for the two objects.

55. What is the difference between Contains(), ContainsKey() and ContainsValue()?


Answer: Contains() and ContainsKey() check whether the specified key exists in the Hashtable collection.
ContainsValue() checks whether the specified value exists in the Hashtable.

56. How to sort Hashtable elements in ascending order?


Answer: There is no built-in function available for sort the elements in hashtable explicitly need to write code.

57. What is the difference between Stack and Queue?


Answer: Stack:
1. C# includes a special type of collection which stores elements in LIFO style(Last In First Out). C# includes a
generic and non-generic Stack. Here, you are going to learn about the non-generic stack.
2. Stack allows null value and also duplicate values. It provides a Push() method to add a value and Pop() or
Peek() methods to retrieve values.
Queue:
1. C# includes a Queue collection class in the System.Collection namespace. Queue stores the elements in FIFO
style (First In First Out), exactly opposite of the Stack collection. It contains the elements in the order they were
added.
2. Queue collection allows multiple null and duplicate values. Use the Enqueue() method to add values and the
Dequeue() method to retrieve the values from the Queue.

58. What method we used to find the top most value in Stack?
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

Answer: Peek();

59. Which method used to add elements into Queue ?


Answer: Enqueue();

60. Which method used to add elements into Queue ?


Answer: Dequeue();

61. What is SortedList?


Answer: The SortedList collection stores key-value pairs in the ascending order of key by default. SortedList class
implements IDictionary & ICollection interfaces, so elements can be accessed both by key and index.
Note: Internally, SortedList maintains two object[] array, one for keys and another for values. So when you add
key-value pair, it runs a binary search using the key to find an appropriate index to store a key and value in
respective arrays. It re-arranges the elements when you remove the elements from it.

62. What is the difference between SortedList and Hashtable?


Answer: The difference between them is that, in the SortedList, the key/value pairs are sorted by key and, since
they have a definite order, you can access them by index as well as by key.In the HashTable, the key/value pairs
are not sorted and you can only access them by key.This means that random insertions for the SortedList are
relatively slow because it needs to be resorted after each insertion. On the other hand, in some applications it is
useful to be able to iterate through the keys in sorted order.

63. Which class must be used as for implementing a custom dictionary non generic collection class?
Answer: DictionaryBase

64. How to insert an item to the ArrayList in a specific position?


Answer: Insert();

65. What is the difference between IComparble and IComparer Interface?


Answer:
IComparble:
1. This interface is used to sort elements and compare the current instance with another object of same type.
2. Method in IComparable interface is System.IComparable.CompareTo(System.Object)
3. The CompareTo method returns an int value that shows how two elements are related.
IComparer:
1. This interface is used to sort elements
2. Method in IComparable interface is System.IComparer.Compare(System.Object,System.Object)
3. This method compare two objects and returns a value indicating whether one is less than, equal to or greater
than other Returns zero if both are same
4. Less than zero if first object is less than zero
5. Greater than zero if first object is greater than zero

66. Can we override CompareTo method in custom classes?


Answer: Yes

67. What is the difference between EqualTo() and == operator?


Answer:
== Operator:
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

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.

68. What is the difference between CompareTo(String) and Equals(String)?


Answer:
string.CompareTo:
Compares this instance with a specified object or String and returns an integer that indicates whether this
instance precedes, follows, or appears in the same position in the sort order as the specified object or String.
string.Equals:
Determines whether two String objects have the same value.
Note: CompareTo is used for sorting. Equals is used to determine equality.

69. Which collection class is suitable for storing large number of key value pairs?
Answer: Hashtable

70. What are the interfaces is used to implement Hashtable collection?


Answer: IEnumerable,ICollection,IDictionary.

71. What is the problem with non-generic collection classes?


Answer: 1. Performance loss. 2. Typesafety

--
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>

72. What is List<T>?


Answer: An ArrayList resizes automatically as it grows. The List<T> collection is the same as an ArrayList except
that List<T> is a generic collection whereas ArrayList is a non-generic collection.

73. What are the advantages of Generic collections?


Answer: 1. Type Safety -- Generic types enforce type compliance at compile-time, and not run-time (as in the
case of using Object). This reduces the chances of data-type conflict during run-time.
2. Performance -- The data types to be used in a Generic class are determined at compile-time, hence there is no
need to perform type casting (boxing and unboxing) during run-time, which is a computationally costly process.
3. Code reuse -- Since you only need to write the class once and customize it to use with the various data types,
there is a substantial amount of code-reuse.

74. How to create Generic interface,Generic method and Generic Class in C#?
Answer:
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

Generic Class: public class A<T1, T2>


{}
Generic interface: public interface A<T1, T2>
{}
Generic Method: public class A
{
public void M<T>
{
}
}

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>();

76. What is Dictionary<TKey, TValue>?


Answer: Dictionary<TKey, TValue> is a generic collection included in the System.Collection.Generics namespace.
TKey denotes the type of key and TValue is the type of TValue.

77. What is Dictionary Initializers?


Answer:Dictionary Initialization is not a new thing in C#. It was present since C# 3.0, but in C# 6.0 Microsoft
enhanced this by adding a new ability to use the key/indexer to map with the dictionary values directly during
the initialization itself.
Example: var sampleData = new Dictionary<string, string>()
{
["First Crazy Developer"] = "My Blog",
["Coding Almanac"] = "Developer Wall",
["Full Stack Developer"] = "Abhishek Kumar"
};

78. What is KeyValuePair?


Answer: KeyValuePair stores two values together. It is a single generic struct. The KeyValuePair type in
System.Collections.Generic is simple and always available. It is used internally in Dictionary.

79. What are the limitatioins in Dictionary?


Answer: 1. A Dictionary stores Key-Value pairs where the key must be unique.
2. Before adding a KeyValuePair into a dictionary, check that the key does not exist using the ContainsKey()
method.
3. Use the TryGetValue() method to get the value of a key to avoid possible runtime exceptions.
4. Use a foreach or for loop to iterate a dictionary.
5. Use dictionary indexer to access individual item.
6. Use custom class that derives IEqualityComparer to compare object of custom class with Contains() method.

80. What are the constraints in Generics?


Answer: C# includes Constraints to specify which type of placeholder type with the generic class is allowed. It will
give a compile time error if you try to instantiate a generic class using a placeholder type that is not allowed by a
constraints. For example, if the generic constraints specifies that only reference type can be used with the
generic class then you cannot use value type to create an object of generic type.
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

81. How to provide constraints on Generics?


Answer:
Example: class MyGenericClass<T> where T: class
{
public MyGenericClass(T value)
{
genericMemberVariable = value;
}
}
class program
{
static void Main()
{
MyGenericClass<int> intGenericClass = new MyGenericClass<int>(10);
}
}
Note: So now, you cannot use int as a placeholder type. The following would give a compile time error. String or
any class type is a valid type because it is a reference type.Constraints can be applied using the where keyword.

82. A generic class can have multiple constraints?


Answer: Yes,
Example:
class MyGenericClass<T, U> where T: class where U:struct
{
...
}

83. Can apply constraints on the generic methods?


Answer: Yes,
Example:
class MyGenericClass<T, U> where T: class where U:struct
{
public T genericMethod<U>(T genericParameter, U anotherGenericType) where U: struct
{
return genericMemberVariable;
}
}

84. What is Anonymous method?


Answer: As the name suggests, an anonymous method is a method without a name.

85. What are the benifites of Collections?


Answer:1. Built-in Sorting capabilities,
2. Most of them are automatically indexed to give best performance.
3. Memory management is handled automatically and Capacity of a collection is expanded as required.
4. Synchronization provides thread safety when accessing members of the collection.
5. Collections classes can generate wrappers that make the collection read-only or fixed-size.
6. Collections class can generate its own enumerator that makes it easy to iterate through the elements

86. What is unbound type parameters in c#?


Answer: A type parameter without any restrictions.
Deccansoft Software Services-MS.NET Collections and Generics
____________________________________________________________________________________

87. What is the use of default keyword ?


Answer: default keyword is used to the specify default value to a generic type parameter.

88. In which version Generics are introduced in C#?


Answer : c#2.0

89. What datatypes are not supported in Generics?


Answer: enum

90. which keyword is used to for specifying constraints for generics type parameters?
Answer: Where

You might also like