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..
Free Download Chapter 10: Understanding Generics
Download chapter

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:

  • Introducing C# 2005 and the .NET 2.0 Platform explains the mechanics of .NET programming and the philosophy that lies behind it
  • The C# Programming Language outlines everything you need to know to start using C# 2005 quickly and efficiently. Object lifetimes, exception handling, generics-it's all here
  • Programming with .NET Assemblies deals with one of the most important aspects of .NET programming: reusing code. This book shows you how to capture your code in reuseable external assemblies that you can call upon throughout your applications
  • Programming with the .NET Libraries guides you through them. Of course you're not expected to write every function yourself. The .NET Framework provides a vast array of .NET Libraries containing functionality that allows you to do everything from opening a file-stream to rendering graphical data to the screen
  • Web Applications and XML Web Services concludes the book by taking you away from console-based C# applications and investigating the myriad possibilities that become available when you blend C# 2005 with ASP.NET 2.0 to launch your applications onto the Internet

< < prev next > >

Understanding Generics

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

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

}