C# Workshop - Week 10 Review Questions

Started by
0 comments, last by popcorn 16 years, 7 months ago
Chapters 27 - 28 Review Questions Greetings everyone. Each week I will post review questions and exercises in the weekly thread. Please try and answer them by yourself; either as you're reading over the chapters, or afterwards if you prefer. Once you've done so, feel free to look over the answers provided by others and submit your own answers if you've not yet seen them posted. Discussion about the quiz questions and answers is encouraged for clarification. Finally, experienced C# programmers may feel free to post quiz-like questions and exercises of their own.
  1. How do generics make classes more versatile?
  2. What's the syntax for making a class generic?
  3. True or False, the main difficulty when implementing generics is making sure the code implemented in the generic class can be evaluated for all types.
  4. What is the purpose of constraints?
  5. What are the different types of constraints?
  6. What can you do to make sure your templated class only works with objects which have a specific set of functions.
  7. True or False, all basic types (and some other types) implement the IConvertable Interface?
  8. What does the 'default' operator do?
  9. Why might you want to use the 'default' operator with unconstrained generics?
  10. What is the benefit of the members of the System.Collection.Generic namespace over those in the System.Collections. Namespace?
  11. True or False, it is possible to have a generic type which uses multiple generic values? Give an example.
  12. True or False, a value type cannot be assigned the value 'null'?
  13. True or False, nullable types are a C# 2.0 construct which allows value types to appear as though they can be assigned the value 'null'?
  14. Do nullable types actually allow value types to be assigned a null value?
  15. What were the 3 changes required to the C# language in order to make Nullables possible?
  16. What are the two public properties of nullable types that identify whether the variable is null or not, and what the value is?
  17. What does the implicit operator of a nullable type allow?
  18. Does the GetValueOrDefault method throw an exception?
  19. What does GetValueOrDefault return if the HasValue property is false?
  20. What has to be true in order for two nullable objects to be equal?
  21. What actually happens when you assign a nullable object the value 'null'?
  22. What is the class used to compare two nullable objects, and get their underlying type?
  23. When you box a nullable object with the HasValue property set to false to an object, what reference is assigned to the object?
Good Luck! [Edited by - JWalsh on August 7, 2007 3:50:26 PM]
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Advertisement
1. How do generics make classes more versatile?
Generics make classes more versatile by allowing classes to be customised for different types.
2. What's the syntax for making a class generic?
The syntax for making a class generic is: class cName<T>
3. True or False, the main difficulty when implementing generics is making sure the code implemented in the generic class can be evaluated for all types.
True.
4. What is the purpose of constraints?
The purpose of constraints is to restrict the types that can be used with a generic class.
5. What are the different types of constraints?

6. What can you do to make sure your templated class only works with objects which have a specific set of functions.
You use the where keyword followed by specifying an interface or multiple interfaces.
7. True or False, all basic types (and some other types) implement the IConvertable Interface?
True all basic types (and some other types) implement the IConvertable Interface.
8. What does the 'default' operator do?
The ‘default’ operator sets value types to their zero values and reference types to null.
9. Why might you want to use the 'default' operator with unconstrained generics?
Because you don’t know what type the user of the class will use and therefore cannot be certain what the default value might have to be.
10. What is the benefit of the members of the System.Collection.Generic namespace over those in the System.Collections. Namespace?
Type safety.
11. True or False, it is possible to have a generic type which uses multiple generic values? Give an example.
True. The Dictionary data structure – public class Dictionary<TKey, TValue>
12. True or False, a value type cannot be assigned the value 'null'?
True.
13. True or False, nullable types are a C# 2.0 construct which allows value types to appear as though they can be assigned the value 'null'?
True.
14. Do nullable types actually allow value types to be assigned a null value?
They do.
15. What were the 3 changes required to the C# language in order to make Nullables possible?
The 3 changes are:
1. A Nullable generic structure was added to the System namespace.
2. C# needed to recognize nullable types in some cases.
3. The CLR needed to recognize nullable types for boxing.
16. What are the two public properties of nullable types that identify whether the variable is null or not, and what the value is?
HasValue and Value.
17. What does the implicit operator of a nullable type allow?
The implicit operator of a nullable type allows you to set an object of a nullable type directly from the underlying type. i.e ndt = DateTime.Now;
18. Does the GetValueOrDefault method throw an exception?
No it does not.
19. What does GetValueOrDefault return if the HasValue property is false?
It returns a default value that the class designer deems appropriate.
20. What has to be true in order for two nullable objects to be equal?
For two nullable objects to be equal their HasValue properties must be the same and if HasValue is true their value properties are the same.
21. What actually happens when you assign a nullable object the value 'null'?
The HasValue property is set to false.
22. What is the class used to compare two nullable objects, and get their underlying type?
The Nullable class.
23. When you box a nullable object with the HasValue property set to false to an object, what reference is assigned to the object?
Null.

Whats the answer to question 5?
How about them apples?

This topic is closed to new replies.

Advertisement