Explore the essentials of Generics and Collections in C#, covering their usage, advantages, and common types. This quiz is designed to reinforce your understanding of generic lists, dictionaries, collections methods, and type safety in C# programming.
Which C# collection allows you to store elements of a specific type and provides dynamic resizing as shown in: Listu003Cintu003E numbers = new Listu003Cintu003E();?
Explanation: Listu003CTu003E is a generic collection that can hold elements of a specified type and automatically resizes as elements are added or removed. ArrayList is a non-generic collection that stores objects, not specific types, leading to possible type safety issues. Queue is used for first-in, first-out operations and does not focus on dynamic resizing in the same context. Hashtable is a non-generic collection used for key-value storage without type enforcement.
Why are generics important for collections like Listu003CTu003E with respect to type safety in C#?
Explanation: Generics enforce type safety by restricting the collection to one type, preventing runtime errors due to invalid types. While generics do not automatically sort items, they allow you to specify sorting methods if needed. Generics do not inherently reduce the overall memory usage or make the items immutable; those are separate concerns.
Which generic collection would you use to store pairs of keys and values, such as assigning names to ages, in C#?
Explanation: Dictionaryu003CTKey, TValueu003E is designed for key-value pairs, making it suitable for scenarios like mapping names to ages. Stacku003CTu003E is a last-in, first-out collection and does not store pairs. Listu003CTu003E is for single-value collections. Queueu003CTu003E operates on a first-in, first-out basis, but does not pair keys with values.
What is the correct way to declare a generic method in C# that accepts a parameter of any type?
Explanation: The correct syntax is public void ShowValueu003CTu003E(T value), specifying the type parameter u003CTu003E and using it as the method parameter. The option public void ShowValue(T value) is incorrect because T would not be recognized without being declared as a generic type. public void ShowValueu003CTu003E() accepts no arguments, and public T ShowValueu003CTu003E(value) has a syntax error as the parameter lacks a type.
Which method would you use to remove an element by value from a Listu003CTu003E collection in C#?
Explanation: The Remove method is specifically designed to remove an element by its value from a Listu003CTu003E in C#. Delete and Discard are not valid methods in this context. Pop is used with stack collections, not lists. Hence, Remove is the correct and only valid option here.
When declaring a generic class with two type parameters in C#, which syntax is appropriate?
Explanation: The correct way to declare a generic class with two type parameters is class Pairu003CT, Uu003E {}. The syntax Pairu003Cu003E lacks type parameters and is invalid. Pairu003CT-Uu003E uses a dash instead of a comma, which is incorrect. Pair[] does not declare generic type parameters at all.
Which built-in method would you use to sort a Listu003Cstringu003E in C# alphabetically?
Explanation: Sort is the built-in method that orders elements in a generic list, such as Listu003Cstringu003E, in ascending order. Arrange and Order are not methods available to Listu003CTu003E. Shuffle is a concept for randomizing order, not sorting alphabetically, and is not a default List method.
Which operation retrieves and removes the top element from a Stacku003CTu003E collection in C#?
Explanation: Pop is the method used for retrieving and removing the top element of a Stacku003CTu003E, following the last-in, first-out principle. Peek returns the top element without removing it. Enqueue is for adding elements to a queue, and RemoveAt is a method for lists, not stacks.
How do you safely determine if a Dictionaryu003Cint, stringu003E contains a specific key before accessing its value?
Explanation: The ContainsKey method allows you to check if a key exists before attempting to access its value in a dictionary. Exists and HasValue are not valid methods for dictionaries in this context. Accessing the value directly risks an exception if the key is missing, so it is less safe compared to using ContainsKey.
In a generic method, what does the default keyword return for a value type parameter T in C#?
Explanation: For value types, default returns the zero-equivalent, such as 0 for integers and false for bool. For reference types, default returns null, but this is not the case for all types as suggested in the second option. It does not produce random values or throw exceptions.