The Growth of .Net Framework

March 20, 2008

Microsoft .Net has grown with each an every release in terms of number of Assemblies, Namespaces, Types and Members. Here is overview of each and every version of .Net framework.

.Net Framework 3.5

Numbers

Assemblies

98

Namespaces

309

Types

11417

Members

109657

 

.Net Framework 3.0

Numbers

Assemblies

80

Namespaces

276

Types

10639

Members

102613

 

.Net Framework 2.0

Numbers

Assemblies

51

Namespaces

190

Types

7419

Members

74607

 

.Net Framework 1.1

Numbers

Assemblies

43

Namespaces

130

Types

3818

Members

37556

 

.Net Framework 1.0

Numbers

Assemblies

41

Namespaces

124

Types

3581

Members

35470

Any guesses on how many types will be in .NET Framework next version??

 


Lambda Expressions

December 26, 2007

Traditional Methods

Delegates are defined as “a reference type that can be used to encapsulate a method with a specific signature” The use of delegates is involved in event handlers, callbacks, asynchronous calls and multi threading, among other uses.

Before C# 2.0, the only way to use delegates was to use named methods. In some cases, this results in forced creation of classes only for using with delegates. In some cases, these classes and methods are never even invoked directly. For example

Program

using System;
using System.Collections.Generic;
using System.Text;
namespace SimpleMethod
{
class Methods
{
static void Main(string[] args)
{
List<string> states = new List<string>();
states.Add(“TN”);
states.Add(“MP”);
states.Add(“UP”);
states.Add(“WB”);
states.Add(“JK”);
string state = states.Find(TN);
Console.WriteLine(state);
}
Public static bool TN(string name)
{
return name.Equals(“TN”);
}
}
}

Delegates

C# 2.0 offers an elegant solution for these methods described above. Anonymous methods allow declaration of inline methods without having to define a named method. This is very useful, especially in cases where the delegated function requires short and simple code. Anonymous methods can be used anywhere where a delegate type is expected. Anonymous Method declarations consist of the keyword delegate, an optional parameter list and a statement list enclosed in parenthesis.

Program

using System;
using System.Collections.Generic;
using System.Text;
namespace AnonymousMethods
{
class Delegates
{
static void Main(string[] args)
{
List<string> states = new List<string>();
states.Add(“TN”);
states.Add(“MP”);
states.Add(“UP”);
states.Add(“WB”);
states.Add(“JK”);
string state = states.Find(delegate(string name)
{
return name.Equals(“TN”);
});
Console.WriteLine(state);
}
}
}

For more on Delegates click here.

Lambda Expressions

C# 3.0 and the .NET 3.0 Runtime introduce a more powerful construct that builds on the anonymous method concept. It allows you to pass an inline expression as a delegate, with minimal syntax.

delegate(string name)
{
return name.Equals(“TN”);
});

…we can do this:

name => name.Equals(“TN”)

The complete program is:

Program

using System;
using System.Collections.Generic;
namespace Lambda
{
class Program
{
static void Main(string[] args)
{
List<string> states = new List<string>();
states.Add(“TN”);
states.Add(“MP”);
states.Add(“UP”);
states.Add(“WB”);
states.Add(“JK”);
string state = states.Find(name => name.Equals(“TN”));
Console.WriteLine(state);
}
}
}

It looks shorter and more concise, doesn’t it? But how does it work? The basic form for a lambda expression is:

argument-list => expression

In the example above, we have an argument named name, implicitly typed as string, then the lambda operator (=>), then an expression which checks to see whether name is equal to “TN”.

To understand better how the lambda expression syntax differs from the anonymous method syntax, let’s turn our example anonymous method:

delegate(string name)
{
return name.Equals(“TN”);
})

We don’t need the delegate keyword, so take it out.

(string name) {
return name.Equals(“TN”);
}

Let’s replace the braces with a => lambda operator to make it an inline lambda expression.

(string name) => return name.Equals(“TN”);

The return keyword isn’t needed because an expression is always a single line of code that returns a value. Also, remove the semicolon, because name.Equals(“TN”) is now an expression, not a full statement.

(string name) => name.Equals(“TN”)

The type of the argument can be inferred as well by the compiler, so we can remove the type declaration for the argument.

name =>name.Equals(“TN”)

And there’s our final lambda expressions.

Lambda Expressions are smart enough to infer variable types. The benefit you get with a lambada expressions you don’t get from delegates is that the compiler performs automatic type interface on the lambada arguments , so I don’t even have to mention that name above is string here.

The big advantage of lambada expression in normal coding is that syntax is more readable and less verbose. This becomes quickly more important the more complex code becomes.

For more on Lambda expression click here.


Get a Unique ID that suits your personality.

December 17, 2007

You must try out the new the new COOLHOTMAIL ID..There are pretty interesting options…Visit the page and see the options Microsoft provides with your account name..


Amazing Tricky Java Script

December 12, 2007

Try out this amazing java script on your browser address bar:

javascript:function reverse() { var inp = ” mih morf gnihtemoS nraeL !! !…ylerecnis gnikrow si tnameH woh eeS !!… !!!krow ruoy oD .!!!!siht rof gniyap ton si YNAPMOC ruoY”; var outp =”"; for (i= 0; i <= inp.length;i++) { outp = inp.charAt (i) + outp ; }alert(outp);}; reverse();javascript:function reverse() { var inp = ” mih morf gnihtemoS nraeL !! !…ylerecnis gnikrow si tnameH woh eeS !!… !!!krow ruoy oD .!!!!siht rof gniyap ton si YNAPMOC ruoY”; var outp =”"; for (i= 0; i <= inp.length;i++) { outp = inp.charAt (i) + outp ; }alert(outp);}; reverse();

After trying, replace my name with your name.


VS 2008-Extension Methods

December 5, 2007

Extension Methods allows developers to add new methods for an existing CLR type. In simple terms it allows developers to add their own methods without having it to added in class and then calling it using the class object. There are many possible scenarios where such methods are useful.

For console applications, the class System.console takes input type as String only. But a user may not write a value and press enter. Since even space/null is taken as string value, program needs to check for valid string. Console application do not have method called IsNullOrEmpty() for Strings. So one possible solution is we can use simple extension methods.

Simple Extension Method Example:

namespace TestExtensionMethods

{

class Test

{
static void Main(string[] args)
{
Console.WriteLine(“Enter a String”);
String s=Console.ReadLine() ;
if (s.IsNullOrEmpty())
{
Console.WriteLine(“Enter a String to Continue”);
Console.ReadLine();
}

}
}

public static class Extensions
{
public static bool IsNullOrEmpty(this string s)
{

return (s == null || s.Trim().Length == 0);
}
}

}

The “this” keyword in the argument of our extension method tells the compiler to add this particular extension method to object of type String. So when i hint “.” keyword on my string variable, my extension variable will show up in the intellisense drop down list .

ramtest.jpg

The extension methods need to be static methods in static class within a namespace that is in the scope as shown above in code.

When to use Them
We must only convert the most reusable code into extension method, so that it provides greater flexibility in handling code and improved readability. Extension methods can be used for any data type where reusablity of methods exists.


Follow

Get every new post delivered to your Inbox.