Free Programming Books
Free download ebooks on computer and programming | |||
Free Ebook "Pro C# 2005 and the .NET 2.0 Platform, Third Edition" Sample Chapter
Pro C# 2005 and the .NET 2.0..
Download chapter
Free Download Chapter 10: Understanding Generics C# 2005 has changed the .NET 2.0 landscape. To name just a few improvements, C# now supports generics, partial types and nullable types when you're coding. And the Framework boasts improved speed, data access, security, and scalability. This book has been written to help you assimilate all this new information and realize its potential to make your coding life easier. It will keep you ahead of the curve. To make it easy for you to access this information, the book has been loosely grouped into five sections:
Understanding GenericsWith the release of .NET 2.0, the C# programming language has been enhanced to support a new feature of the CTS termed generics. Simply put, generics provide a way for programmers to define "placeholders" (formally termed type parameters) for method arguments and type definitions, which are specified at the time of invoking the generic method or creating the generic type. To illustrate this new language feature, this chapter begins with an examination of the System.Collections.Generic namespace. Once you've seen generic support within the base class libraries, in the remainder of this chapter you'll examine how you can build your own generic members, classes, structures, interfaces, and delegates. Revisiting the Boxing, Unboxing, and System.Object RelationshipTo understand the benefits provided by generics, it is helpful to understand the "issues" programmers had without them. As you recall from Chapter 3, the .NET platform supports automatic conversion between stack-allocated and heap-allocated memory through boxing and unboxing. At first glance, this may seem like a rather uneventful language feature that is more academic than practical. In reality, the (un)boxing process is very helpful in that it allows us to assume everything can be treated as a System.Object, while the CLR takes care of the memory-related details on our behalf. To review the boxing process, assume you have created a System.Collections.ArrayList to hold numeric (stack-allocated) data. Recall that the members of ArrayList are all prototyped to receive and return System.Object types. However, rather than forcing programmers to manually wrap the stack-based integer in a related object wrapper, the runtime will automatically do so via a boxing operation:
static void Main(string[] args)
{
// Value types are automatically boxed when
// passed to a member requesting an object.
ArrayList myInts = new ArrayList();
myInts.Add(10);
Console.ReadLine();
}If you wish to retrieve this value from the ArrayList object using the type indexer, you must unbox the heap-allocated object into a stack-allocated integer using a casting operation:
static void Main(string[] args)
{
...
// Value is now unboxed...then reboxed!
Console.WriteLine("Value of your int: {0}",
(int)myInts[0]);
Console.ReadLine();
}
| |||