What is new in c#.net 2005

Here are new features introduced in c#.net 2005 version

1) Partial classes
2) Iterators
3) Nullable types
4)Generics
5)Anonymous methods
Partial Classes:
A partial class is one in which the class, struct, or interface definition is split between multiple source files. The biggest benefit is that it makes it easy to add code to an automatically generated class without having to modify the auto-generated source code. It also lets you modify classes that someone else wrote, or that come as part of a packaged component, without modifying the source code
in asp.net 2.0 code behind page automatically creates partial classes.

Iterators
C# 2.0 adds iterators, which are methods that let you use the foreach statement over an entire class. An iterator code lets you specify how return values will be generated when each pass through the foreach loop accesses each element of the collection. Putting it another way, the iterator makes it easier to implement IEnumerable or IEnumerator methods by automatically keeping track of the current element in the collection. It takes care of the plumbing, in other words, which not only keeps the source code simpler, but also reduces the possibility of coding errors in loops.

more details from the page http://www.ondotnet.com/pub/a/dotnet/2004/04/05/csharpwhidbeypt1.html

Iterators

Often, developers need to create classes that support enumeration. When a class supports enumeration, you can use the convenient foreach syntax to step through a group of items. For example, if you create an OrderCatalog class that contains a group of OrderItem instances, you might want to enumerate over all of the items using this code:

foreach (item OrderItem in catalog)
{
// (Process OrderItem here.)
}

This code gets translated by the C# compiler to use the GetEnumerator() method of the class. Technically, it looks like this:

Enumerator e = catalog.GetEnumerator();
while (e.MoveNext())
{
OrderItem item = e.Current;
// (Process OrderItem here.)
}

As a result, this pattern only works with classes that implement the IEnumerable interface. Creating a class that supports enumeration in C# 1.0 ranges from mildly inconvenient to annoyingly awkward. In C# 2.0, a new language feature known as iterators makes it much easer to create classes that support enumeration. Instead of building a state machine to keep track of your position in a collection, you create one public method named GetEnumerator() that returns all of the values directly using the new yield return keyword.

For example, here's a simple class that yields three pieces of information:

public class DecoratingColors
{
public string WallPaint;
public string Carpet;
public string Furniture;
public IEnumerator GetEnumerator()
{
yield return WallPaint;
yield return Carpet;
yield return Furniture;
}
}

When you use foreach on a DecoratingColors instance, you'll wind up with three strings, one after the other.

To see the real rewards of iterators, you need to consider a more realistic example. The following code shows the OrderCatalog class, which contains a private collection of OrderItem instances. In this example, the GetEnumerator() method traverses all the items in the private collection in a loop, returning one item each time.

public class OrderCatalog
{
private ArrayList orderItems = new ArrayList();

public void Load()
{
// Fill collection for a test.
orderItems.Clear();
orderItems.Add(new OrderItem("Item 1"));
orderItems.Add(new OrderItem("Item 2"));
orderItems.Add(new OrderItem("Item 3"));
}

public IEnumerator GetEnumerator()
{
foreach (OrderItem item in orderItems)
{
yield return item;
}
}
}

public class OrderItem
{
private string name;

public string Name
{
get { return name; }
}

public OrderItem(string name)
{
this.name = name;
}
}

Here's the code that puts this example to the test:

OrderCatalog catalog = new OrderCatalog();
catalog.Load();
foreach (OrderItem item in catalog)
{
MessageBox.Show(item.Name);
}

The beauty of iterators is that the enumerable class can concentrate on providing information, while the consumer can concentrate on retrieving it. Neither part needs to worry about the actual implementation details of how positioned is maintained.




Partial Classes

Partial classes give you the ability to split a single class into more than one source code (.cs) file. Here's what a partial class looks like when it's split over two files:

// Stored in file MyClass1.cs
public partial class MyClass
{
public MethodA()
{...}
}

// Stored in file MyClass2.cs
public partial class MyClass
{
public MethodB()
{...}
}

When you build the application, Visual Studio .NET tracks down each piece of MyClass and assembles it into a complete, compiled class with two methods, MethodA() and MethodB().

Partial classes don't offer much in the way of solving programming problems, but they can be useful if you have extremely large, unwieldy classes. (Of course, this might be a sign that you haven't properly factored your problem, in which case you should really break your class down into separate classes.) The real purpose of partial classes in .NET is to hide automatically generated designer code.



More details from the page http://www.yoda.arachsys.com/csharp/csharp2/nullable.html

Nullable types and the null coalescing operator
Nullable and T?

For as long as I can remember, people have been asking why they can't set an int variable to null, or why they can't return null from a method declared to return DateTime. Many who understand why they couldn't do so still wished they could, particularly when working with databases.

.NET 2.0 provides the generic struct System.Nullable with the constraint that T must be a value type. (If you know absolutely nothing about generics, now might be a good time to learn about the basics before reading further. You don't need to know a lot of the details however, and the basic concept is a lot simpler than full-blown generics sometimes gets, which is why I've put this page before the one on generics.) Nullable itself is still a value type, but it represents the same set of values as T plus the "null" value. It maintains a separate member in memory, which is exposed through the HasValue property. When this is true, the Value property represents the overall value. When it's false, the overall value is null.

C# provides language support for nullable types using a question mark as a suffix. For example, int? is the same type as Nullable (which is also the same type as Nullable in the normal way). C# then allows you to compare a nullable value with null, or set it to null, and these work in the obvious way. There's an implicit conversion (no cast required) from a non-nullable type to its equivalent nullable type, and there's an explicit conversion (cast requried) from a nullable type to its equivalent non-nullable type. The cast is compiled into a call to the Value property, and an InvalidOperationException is thrown if the value is null at that point. A nullable type can also be used as the right hand side of the as operator, with the natural consequences.

The boxed type of a nullable value is the boxed type of the equivalent non-nullable value. If you box a value which is already null (i.e. HasValue is false), the result is null. This was a late change to the behaviour, as it required CLR changes which Microsoft were hoping to avoid - you may therefore see some beta documentation which disagrees with this.

Note that unlike in SQL, two null values of the same type are equal. In other words, the following:

int? x = null;
int? y = null;
Console.WriteLine (x==y);

prints "True".

As well as the System.Nullable struct, there's the non-generic static class System.Nullable. This merely provides support for the System.Nullable struct, in terms of finding out the non-nullable type of a nullable type and performing comparisons.
Nullable logic

bool? has various binary logic operators, but not all of the ones available on bool. Importantly, the "shortcut" operators (&& and ||) aren't defined for bool?. A null value represents a sort of "don't know" value - so for instance, null | true is true, but null | false is null; similarly null & false is false but null & true is null.
The null coalescing operator

This is a really simple little operator which I suspect will come in quite handy - if it's widely known about. (It was a long time before I saw anything about it.) Basically, a ?? b is similar to a==null ? b : a. The type of a has to be a nullable type or a reference type, and the type of b has to be a suitable type, the details of which are best left to the spec. The result is the value of a if that's non-null, otherwise it takes the value of b. a is only evaluated once (contrary to the version presented above using the conditional operator), and b is only evaluated at all if a evaluates to null.

The operator is right-associative, so a ?? b ?? c is equivalent to a ?? (b ?? c) - in other words, if you provide a string of a1 ?? a2 ?? ... ?? an then the result is the first non-null one (or null if all of them are null).

One nice feature is that if the type of a is a nullable type and the type of b is the equivalent "non-nullable" type, the result of the expression is that non-nullable type. For example, you can do:

// GetSomeValueMaybe() is a method returning an int? value
int? possible = GetSomeValueMaybe();
int definite = possible ?? 5; // Default to 5

// Alternatively, just:
int value = GetSomeValueMaybe() ?? 5;

This is possible because the compiler knows that if the first expression evaluates to null it will use the second expression. In this case, we're effectively using GetSomeValueMaybe() with a kind of "default value" of 5.

The usual details about conversions and the precise rules which are applied can be found in the spec.


Generics

*

Use generic types to maximize code reuse, type safety, and performance.
*

The most common use of generics is to create collection classes.
*

The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace.
*

You can create your own generic interfaces, classes, methods, events and delegates.
*

Generic classes may be constrained to enable access to methods on particular data types.
*

Information on the types used in a generic data type may be obtained at run-time by means of reflection.

Comments

Popular Posts