| |
| The next release of .NET Framework
2.0 and Visual Studio .NET platform code-named Whidbey brings a series
of new enhancements and altogether new advancements to all areas of
.NET framework and visual studio. With its enhancements, it also paves
the path to next generation Microsoft operating system "Longhorn".
All this enhancements are designed to help developers both be productivity
and take full advantage of the power of the .NET framework. |
| |
| This two-part article series describes
some of the Common Language Runtime (CLR) and language enhancements
of .NET Framework 2.0. First part of this article covers some of the
new features like Generic, Nullable Type, Partial Types and Iterators.
This article is available at the following link, |
| |
| http://www.microsoft.com/india/msdn/articles/252.aspx |
| |
| The new enhancements that are explored
in this part 2 are, |
| |
| 1. |
Anonymous Methods |
| 2. |
Static Methods |
| 3. |
Inline Warning Control |
| 4. |
Property and Index accessor accessibility |
| 5. |
Namespace Qualifier |
| 6. |
External Assembly Alias |
| 7. |
Security Enhancements |
| 8. |
Other CLR Enhancements |
|
| |
| Note: The information in this article
is based on a beta 1 version of .NET Framework 2.0, some of the features
may change in the final release. |
| |
| Anonymous methods |
| |
| In .NET Framework 1.x you were forced
to create a method just to use delegate. You can’t write your
code inline where delegate is used. For example, |
| |
public
class Form1:Form
{
private void
AnysMethod()
{
string
temp = "Anonymous Method";
this.button2.Click
+= new
System.EventHandler(this.button2_Click);
}
private
void button2_Click(object
sender, EventArgs e)
{
MessageBox.Show("hi");
}
} |
|
| |
| In this example, just to show one Messagebox
you need to create a method and then attach that method handler to
the required delegate. But in .NET Framework 2.0, Anonymous methods
allow the code associated with the delegate to be written in-line
where the delegate is used. For example, |
| |
public
class Form1:Form
{
private
void AnysMethod()
{
string
temp = "Anonymous Method";
this.button2.Click
+= delegate { MessageBox.Show("hi");
};
}
} |
|
| |
| You no need to create a method for
associating your code to delegate. C# compiler will generate the anonymous
method or class for you and associate that method or class with this
delegate internally. If you see the compiled code of this class, |
| |
public
class Form1:Form
{
private void
AnysMethod()
{
string
temp = "Anonymous Method";
Form1.<>c__DisplayClass1
class1 = new
Form1.<>c__DisplayClass1();
class1.temp = "Anonymous
Method";
this.button2.Click
+= new EventHandler(class1.<.ctor>b__0);
}
[CompilerGenerated]
private
sealed class <>c__DisplayClass1
{
//
Methods
public
<>c__DisplayClass1();
public
void <.ctor>b__0(object,
EventArgs)
{
MessageBox.Show(this.temp);
}
// Fields
public string
temp;
}
} |
|
| |
| A new class <>c__DisplayClass
is created by compiler and its reference is added to Anysmethod().
So compiler will do the work for us instead of we creating a method
and attaching to event handler. One more advantage of Anoymous method
is you can access local variable and parameters which are declared
outside the anoymous method but within scope where anonymous method
is declared. In this example itself, you can see local variable temp
is captured and passed to newly created class as property of that
class. Anonymous method is also available only in C# and not in VB.NET. |
| |
| Static Classes |
| |
| If you create a class in .NET Framework
1.x with only static methods and if you don’t want it to be
instantiated. You will declare that class to be a sealed class and
with a private constructor. So that nobody can instantiate it. But
by doing this, you can’t avoid adding instance methods to this
class. Similarly you can’t avoid declaring that class as type
variable or parameter. But in .NET Framework 2.0 you can specify a
class as static class by which compiler will take care of the following
checks |
| |
| • |
Static classes only contain static
members. |
| • |
Static classes cannot be instantiated.
|
| • |
Static classes are sealed. |
| • |
Static classes cannot contain
a constructor |
|
| |
| Example for static class, |
| |
public
static class StaticCls
{
static public
void StaticMethod
{
//
Implementation
}
} |
|
| |
| Inline Warning Control |
| |
| During testing you might come across
a situation where you need to hide some warning for one method or
for few line of code. In .NET Framework 1.x you can hide any warning
at project level using project setting or using command line parameters,
but it is not possible to hide at certain lower level. But in .NET
2.0 Framework, you can use following two statement to enable and disable
warning in your code. |
| |
| A #pragma warning disable
directive disables all or the given set of warnings. |
| |
| A #pragma warning restore
directive restores all or the given set of warnings to the state that
was in effect at the beginning of the compilation unit. |
| |
| Property and Index accessor
accessibility |
| |
| In .NET Framework 2.0 it is possible
to specify different accessibility for set and get accessor. This
will be very helpful in cases where you need to give get access to
a property but want to restrict set access to that property. For example, |
| |
public
class
MyClass
{
public
string Name
{
get
{
return
_Name;
}
protected
set
{
_Name
= value;
}
}
string
_Name;
} |
|
| |
| Namespace Qualifier |
| |
| In your development, if you come across
a situation where you need to write a class like this, |
| |
namespace
Test
{
namespace System
{
class
tstClass
{
public
void MyMethod()
{
System.Windows.Forms.MessageBox.Show("Hi");
}
}
}
} |
|
| |
| Compilation error will come when you
try to access Forms class in System.Windows namespace. Since compiler
will try to locate Forms class in immediate scope which has System
namespace but without Windows namespace. There is no way to avoid
this in .NET Framework 1.x. But in .NET Framework 2.0, you can mention
global namespace qualifier using :: syntax. In .NET Framework 2.0
you can access Forms class in System.Windows namespace like this |
| |
global::System.Windows.Forms.MessageBox.Show("Hi"); |
|
| |
| If you specify namespace alias using
global:: syntax, then namespace lookup will start from root namespace.
Now namespace qualifier consists of two parts |
| |
|
| |
| identifier1 – It can global or
any other namespace alias |
| |
| identifier2 – It can be namespace
or namespace alias |
| |
| Depending upon indentifier1 value,
scope of lookup of identifier2 will be determined. If identifier1
is global, then it is referring to root namespace. If it is other
than global, then its scope of lookup is namespace aliases only. |
| |
| External Assembly Alias |
| |
In a testing scenario you might come
across a situation where you need to refer two version of same assembly
in your class. Or you might need to refer two assemblies with same
namespace in your class. Both scenarios can’t be achieved in
.Net Framework 1.0, but in NET Framework 2.0 by using extern alias
you can achieve this.
You can refer two version of same assembly like this, |
| |
extern
alias Assembly10;
extern alias Assembly20;
public
static class StaticCls
{
//your code..
} |
|
| |
| In this class, you just need to create
two extern aliases. But when you are compiling your class, you can
map external assembly of different version (assembly 1.0 and assembly
2.0) to alias Assembly10 and Assemby20 like this, |
| |
| 1. |
In command line csc /target:winexe
file.cs /r:Assembly10=Assembly1_0.dll /r:Assembly2=Assembly2_0.dll |
| 2. |
In VS.NET
Just add the reference to the class in VS.NET using add reference
option. After that go to property page of added assemblies,
in the alias property change it to corresponding alias. |
|
| |
| Other CLR Enhancements |
| |
| 64 Bit Platform support |
| |
| The new generation of 64-bit computers
enables the creation of applications that can run faster and take
advantage of more memory than is available to 32-bit applications.
CLR 2.0 gives support to develop applications for 64-bit environment.
You can take an existing ASP.NET application and run it on a 64-bit
processor server without recompiling your application or modifying
a single line of code. |
| |
| Debugger Edit and Continue
Support |
| |
| .NET Framework 2.0 brings back it’s
Edit and Continue support in VS.NET 2005 to enable users to break
the execution and allows changing of source code while debugging.
After the code change, user resumes the execution of program. This
feature was there in visual studio, but .NET Framework 1.x versions
didn’t support this. This feature is only supported in VB.Net
and not in C#. This feature is not supported in ASP.NET applications
also. |
| |
| Base Class Library Changes |
| |
| 1. |
New class “NetworkChange”
has been added, this allows application to receive notification
when Internet Protocol (IP) address of a machine changes. |
| 2. |
The new Data Protection API (DPAPI)
includes four methods that allow applications to encrypt passwords,
keys, connections strings, and so on, without calling platform
invoke |
| 3. |
New members in Console class,
enables users to change the dimensions of Console window, cursor
properties, background and foreground colors of the console
window and frequency of console beep. |
| 4. |
In the System.Net namespace,
support has been added for FTP client requests, caching of HTTP
resources, automatic proxy discovery, and obtaining network
traffic and statistical information. |
| 5. |
Classes in Compression Namespace
can be used to write and read data in GZIP compression standards. |
| 6. |
The Ping class allows an application
to determine whether a remote computer is available in the network. |
| 7. |
A new class “Stopwatch”
is added to System.Diagnostics namespace and it allows application
to measure performance timing. |
|
| |
| Security Enhancements |
| |
| 1. |
A New tool “Permission
Calculator” is integrated with VS.Net 2005 to check the
permissions required by an application during compilation. Using
this tool you can find out what permission is required by your
application targeting specific zone like internet zone. This
tool is integrated with ClickOnce(Web style deployment for client
applications), so that before deploying any application you
can verify whether all the permission required by this application
is available. This tool can be accessed from security pane on
project designer. |
| 2. |
The SecurityException
class has been expanded to provide additional data that facilitates
investigation into the cause of security exceptions. New properties
provide information that includes the method in which the exception
occurred, full assembly information, so developers can figure
out what assembly was the cause of the exception, and the security
action on the call stack, such as Deny or PermitOnly,
that caused the exception. PermissionState
were not populated consistently in .NET 1.x, now it has been
corrected. |
| 3. |
An access control list (ACL)
is a security list that tells a computer operating system which
access rights each user has to a particular system object, such
as a file directory or individual file. New classes have been
added to the .NET Framework that allows managed code to create
and modify an ACL. |
| 4. |
The .NET Framework 2.0 now supports
X.509 certificate stores, chains, and extensions. In addition,
you can sign and verify XML using X.509 certificates without
using platform invoke. There is also support for PKCS7 signature
and encryption, and CMS (a superset of the PCKS7 standard available
on Microsoft Windows 2000 and later operating systems). PKCS7
is the underlying format used in Secure/Multipurpose Internet
Mail Extensions (S/MIME) for signing and encrypting data. |
|
| |
| Conclusion |
| |
| This article explored some of the new
features in .NET Framework 2.0. These features will enhance the Rapid
Application Development facilities of the language hence increase
the productivity and also in some cases it will improve the performance
of that application. This article also explained various scenarios
where these new features can be used. |
| |
| |
| |