Search Eridotnet

 

Tuesday, January 15, 2008

Quick thoughts on .NET 1.x, 2.0, 3.0 and 3.5 (part four)

Hi there! I'm back.

I can just declare variable without typing?


On my previous post on LINQ, I discussed quick overview of LINQ. If you read the code, there are many uses of "untyped" variable, I just used var in C# or Dim in VB.NET. This is also one of new features of VS 2008. This feature is known as "type inferencing".

Type inferencing sample in C#:

        static void Main(string[] args)
{
var i = 10;
var anytext = "anytext";
var anyarray = new int[]{ 1, 2, 3 };
Console.WriteLine("i= {0}", i);
Console.WriteLine("anytext= {0}", anytext);
foreach (var item in anyarray)
{
Console.WriteLine("array ={0}", item);
}
Console.ReadLine();
}

Type inferencing sample in VB.NET:

    Sub Main()
Dim i = 1
Dim anytext = "anytext"
Dim anyarray = New Integer() {1, 2, 3}
Console.WriteLine("i= {0}", i)
Console.WriteLine("anytext= {0}", anytext)
For Each item In anyarray
Console.WriteLine("array ={0}", item)
Next
Console.ReadLine()

End Sub

 

As MSDN of C# says:

"When used with local variables, the var keyword instructs the compiler to infer the type of the variable or the array elements from the expression on the right side of the initialization statement."

Also MSDN of VB.NET says:

"By using local type inference (also referred to as implicit typing), the compiler determines the data types of local variables based on the values that are used to initialize them."

Yes, they're both the same context and concept. The point is, the type inferencing is not .NET 3.5 new features. It's simply a language feature. But I decide to discuss it here since it's often in describing LINQ samples used throughout MSDN for VS 2008. Type inferencing can be used for primitive types such as integer, boolean, string. Complex types such as arrays and collections or just classes of IEnumerable are welcome.

So, don't worry. Your beloved .NET is still strongly typed. No messing around with real untyped variables, or your code won't compile.

OK, I have LINQ, type inferencing, what are other new features?


 

Many! On .NET 3.5 side, you have:


  • LINQ
  • Addins
  • Peer to peer networking
  • Integration of WCF and WF
  • 2D on 3D in WPF

On VS 2008 side:


  • Split view editor for ASP.NET designer
  • Fully released and supported WPF Designer (formerly known as "Cider")

On both Visual C# and VB.NET:



  • Type inferencing
  • Lambda expressions
  • Extension methods
  • LINQ as a language service/feature

On VB 2008 (a.k.a. VB.NET 9.0) side only:


  • XML Axis Properties
  • XML Literals
  • Anonymous type (almost similar to C# object initializers, but only restricted to anonymous type, not collections)
  • Relaxed delegates

On Visual C# 2008 (a.k.a. C#3.0) side only:


  • Partial method (only valid for methods that return void)
  • Auto-implemented properties with backing field automatically generated
  • Object initializers, enables object initialization without explicit calls to a constructor.
  • yield keyword
  • Anonymous methods

Next post? I'll try to discuss Lambda expressions on C# and VB.NET.

1 comments:

eriawan said...

Yes, I can finally comment using my OpenID!

Addition info to this my blog post:
.NET 3.5 runtime can be downloaded in a large single file. The SDK will be available soon, at the time Windows Server 2008 release.