well , I am feeling HAPPY to answer your question , I would like to suggest you to access the source site if it's available , for more information.
There are dozens (hundreds, probably) of pages listing the new features of C# 2.0. However, I never know where to find a good one quickly, and they don't always tell me what I need to know at the time. I figured if I added my own set of pages, I could update them whenever I wanted to, and point other people at them when answering questions. Without further ado then, here are the new features of C# 2.0:
* Various "bits and bobs" described below
o Partial types
o Aliases
o Static classes
o Property access modifiers
* Nullable types and the null coalescing operator
* Delegate changes
* Implementing iterators with yield statements
* Generics
Partial types
Code generators have existed for a long time. In the past, they usually (depending on the language) either "owned" a whole type/module (creating a whole file which shouldn't or couldn't be edited) or reserved sections of files which shouldn't be edited manually. In some cases, where code was generated by a separate tool from something like a database schema, it could be very hard to make changes to the schema and regenerate the code without losing any additions made by hand.
C# 2.0 introduces the concept of a partial type declaration. This is quite simply a single type which spans multiple files, where each file declares the same type using the partial modifier. The files may refer to members declared within one another without problem (just as forward references within C# is already not a problem). Here's an example (which in itself is a complete program). This allows all the auto-generated code which either mustn't be touched on pain of brokenness or shouldn't be touched because you'll lose all your changes anyway to live in a completely separate file to the code you wish to add. It doesn't help much if you want to tweak the generated code, of course, but that's a less common issue.
Test1.cs:
partial class Test
{
string name;
static void Main()
{
Test t = new Test("C# 2.0");
t.SayHello();
}
}
Test2.cs:
using System;
partial class Test
{
Test(string name)
{
this.name = name;
}
void SayHello()
{
Console.WriteLi ne ("Hi there. My name is {0}.", name);
}
}
Compile with:
csc Test1.cs Test2.cs
Results:
Hi there. My name is C# 2.0.
A few little things to be aware of:
* using directives are only applied to the source file they occur in.
* Variable initializers (both instance and static) are executed in textual order, but there's no more guarantee made than that. Given four fields (a1, a2, b1, b2) appearing in files A.cs and B.cs (in the obvious files, with the obvious order), all that is guaranteed is that the initializer for a1 will be executed before the initializer for a2 and b1 before b2. A sequence of a1 b1 b2 a2 is acceptable, although I'd imagine that either a1 a2 b1 b2 or b1 b2 a1 a2 would be more likely.
* If a type has a modifier (abstract, public, static etc) applied to it in one place, it has effectively been applied everywhere. In particular, the modifiers on each part of the type definition must not clash - a class cannot be declared as protected in one place and public in another, for example.
Aliases
In previous versions of C#, it was impossible to use two different types which had the same name (including namespace) within the same assembly. (The types themselves would have to be defined in different assemblies anyway, of course, but you might want to use them both from the same assembly.)
Hope it will help you out.
Answered by
Uttam
, an ibibo Master,
at
8:59 PM on June 02, 2008