Looking at Whats New in the .NET Framework 2.0


Looking at What’s New in the .NET Framework 2.0

The first version of the .NET Framework (1.0) was released in 2002 to much enthusiasm. The latest version, the .NET Framework 2.0, was introduced in 200 5 and is considered a major release of the Framework.

With each release of the Framework, Microsoft has always tried to ensure that there were minimal breaking changes to code developed. Thus far, they have been very successful at this goal.

Important 

Make sure that you create a staging server to completely test the upgrading of your applications to the .NET Framework 2.0 as opposed to just upgrading a live application.

The following section details some of the changes that are new to the .NET Framework 2.0 as well as new additions to Visual Studio 2005 - the development environment for the .NET Framework 2.0.

SQL Server integration

After a long wait, the latest version of SQL Server has finally been released. This version, SQL Server 2005, is quite special in so many ways. Most importantly for the .NET developer is that SQL Server 2005 is now hosting the CLR. Microsoft has developed its .NET offering for developers so that the .NET Framework 2.0, Visual Studio 2005, and SQL Server 2005 are all now tied together - meaning that these three products are now released in unison. This is quite important, as it is rather well known that most applications built use all three of these pieces and that they all need to be upgraded together in such a way that they work with each other in a seamless manner.

Due to the fact that SQL Server 2005 now hosts the CLR, this means that you can now avoid building database aspects of your application using the T-SQL programming language. Instead, you can now build items such as your stored procedures, triggers, and even data types in any of the .NET-compliant languages, such as C#.

SQL Server Express is the 2005 version of SQL Server that replaces MSDE. This version doesn’t have the strict limitations MSDE had.

64-Bit Support

Most programming today is done on 32-bit machines. It was a monumental leap forward in application development when computers went from 16-bit to 32-bit. More and more enterprises are moving to the latest and greatest 64-bit servers from companies such as Intel (Itanium chips) and AMD (x64 chips) and the .NET Framework 2.0 has now been 64-bit- enabled for this great migration.

Microsoft has been working hard to make sure that everything you build in the 32-bit world of .NET will run in the 64-bit world. This means that everything you do with SQL Server 2005 or ASP.NET will not be affected by moving to 64-bit. Microsoft themselves made a lot of changes to the CLR in order to get a 64-bit version of .NET to work. Changes where made to things such as garbage collection (to handle larger amounts of data), the JIT compilation process, exception handling, and more.

Moving to 64-bit gives you some powerful additions. The most important (and most obvious reason) is that 64-bit servers give you a larger address space. Going to 64-bit also allows for things like larger primitive types. For instance, an integer value of 2^32 will give you 4,294,967,296 - while an integer value of 2^64 will give you 18,446,744,073,709,551,616. This comes in quite handy for those applications that need to calculate things such as the U.S. debt or other large numbers.

Companies such as Microsoft and IBM are pushing their customers to take a look at 64-bit. One of the main areas of focus are on database and virtual storage capabilities as this is seen as an area in which it makes a lot of sense to move to 64-bit for.

Visual Studio 2005 can install and run on a 64-bit computer. This IDE has both 32-bit and 64-bit compilers on it. One final caveat is that the 64-bit .NET Framework is meant only for Windows Server 2003 SP1 or better as well as other 64-bit Microsoft operating systems that might come our way.

When you build your applications in Visual Studio 2005, you can change the build properties of your application so that it compiles specifically for 64-bit computers. To find this setting, you will need to pull up your application’s properties and click on the Build tab from within the properties page. On the Build page, click on the Advanced button and this will pull up the Advanced Compiler Setting dialog. From this dialog, you can change the target CPU from the bottom of the dialog. From here, you can establish your application to be built for either an Intel 64-bit computer or an AMD 64-bit computer.

Generics

In order to make collections a more powerful feature and also increase their efficiency and usability, generics were introduced to the .NET Framework 2.0. This introduction to the underlying framework means that languages such as C# and Visual Basic 2005 can now build applications that use generic types. The idea of generics is nothing new. They look similar to C++ templates but are a bit different. You can also find generics in other languages, such as Java. Their introduction into the .NET Framework 2.0 languages is a huge benefit for the user.

Generics enable you to make a generic collection that is still strongly typed - providing fewer chances for errors (because they occur at runtime), increasing performance, and giving you IntelliSense features when you are working with the collections.

To utilize generics in your code, you will need to make reference to the System.Collections.Generic namespace. This will give you access to generic versions of the Stack, Dictionary, SortedDictionary, List, and Queue classes. The following demonstrates the use of a generic version of the Stack class:

  void Page_Load(object sender, EventArgs e) {    System.Collections.Generic.Stack<string> myStack =       New System.Collections.Generic.Stack<string>();    myStack.Push("St. Louis Rams");    myStack.Push("Indianapolis Colts");    myStack.Push("Minnesota Vikings");    Array myArray;    myArray = myStack.ToArray();    foreach(string item in myArray)    {       Label1.Text += item + "<br />";    } } 

In the above example, the Stack class is explicitly cast to be of type string. Here, you specify the collection type with the use of brackets. This example, casts the Stack class to type string using Stack<string>. If you wanted to cast it to something other than a Stack class of type string (for instance, int), then you would specify Stack<int>.

Because the collection of items in the Stack class is cast to a specific type immediately as the Stack class is created, the Stack class no longer casts everything to type object and then later (in the foreach loop) to type string. This process is called boxing, and it is expensive. Because this code is specifying the types up front, the performance is increased for working with the collection.

In addition to just working with various collection types, you can also use generics with classes, delegates, methods, and more. Chapter 10 of this book covers generics in detail.

Anonymous Methods

Anonymous methods enable you to put programming steps within a delegate that you can then later execute instead of creating an entirely new method. For instance, if you were not using anonymous methods, you would use delegates in a manner similar to the following:

  public partial class Default_aspx {    void Page_Load(object sender, EventArgs e)    {       this.Button1.Click += ButtonWork;    }        void ButtonWork(object sender, EventArgs e)    {       Label1.Text = "You clicked the button!";    } } 

But using anonymous methods, you can now put these actions directly in the delegate, as shown in the following example:

  public partial class Default_aspx {    void Page_Load(object sender, EventArgs e)    {       this.Button1.Click += delegate(object myDelSender, EventArgs myDelEventArgs)       {          Label1.Text = "You clicked the button!";       };    } } 

When using anonymous methods, there is no need to create a separate method. Instead, you place the necessary code directly after the delegate declaration. The statements and steps to be executed by the delegate are placed between curly braces and closed with a semicolon.

Nullable Types

Due to the fact that generics has been introduced into the underlying .NET Framework 2.0, it is now possible to create nullable value types - using System.Nullable<T>. This is ideal for situations such as creating sets of nullable items of type int. Before this, it was always difficult to create an int with a null value from the get-go or to later assign null values to an int.

To create a nullable type of type int, you would use the following syntax:

  System.Nullable<int> x = new System.Nullable<int>; 

There is a new type modifier that you can also use to create a type as nullable. This is shown in the following example:

  int? salary = 800000 

This ability to create nullable types is not a C#-only item as this ability was built into .NET Framework itself and, as stated, is there due to the existence of the new generics feature in .NET. For this reason, you will also find nullable types in Visual Basic 2005 as well.

Iterators

Iterators enable you to use foreach loops on your own custom types. To accomplish this, you need to have your class implement the IEnumerable interface, as shown here:

  using System; using Systm.Collections; public class myList {    internal object[] elements;    internal int count;    public IEnumerator GetEnumerator()    {       yield return "St. Louis Rams";       yield return "Indianapolis Colts";       yield return "Minnesota Vikiings";    } } 

In order to use the IEnumerator interface, you will need to make a reference to the System.Collections namespace. With this all in place, you can then iterate through the custom class, as shown here:

  void Page_Load(object sender, EventArgs e) {    myList IteratorList = new myList();    foreach(string item in IteratorList)    {       Response.Write(item.ToString() + "<br />");    } } 

Partial Classes

Partial classes are a new feature to the .NET Framework 2.0 and again C# takes advantage of this addition. Partial classes allow you to divide up a single class into multiple class files, which are later combined into a single class when compiled.

To create a partial class, you simply need to use the partial keyword for any classes that are to be joined together with a different class. The partial keyword precedes the class keyword for the classes that are to be combined with the original class. For instance, you might have a simple class called Calculator, as shown here:

  public class Calculator {    public int Add(int a, int b)    {       return a + b;    } } 

From here, you can create a second class that attaches itself to this first class as shown in the following example:

  public partial class Calculator {    public int Subtract(int a, int b)    {       return a - b;    } } 

When compiled, these classes will be brought together into a single Calculator class instance as if they were built together to begin with.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net