Sunday, January 27, 2008

.NET 3.5 : VB and C#

[The code in this post appears weird for some reasons, I used LiveWriter and few good but buggy plugins to attach code]

I recently made a list of ASP.NET Videos that are out there online. Excluding the MSDN Nuggets and Microsoft Webcast events, the listings spanned over 4 pages!!! I am really concerned and am cursing myself for not having watched any of those for years. I have actually been a close follower of videocasts/screencasts right from those days when there weren't that many online for free. Now Microsoft, I should say is the largest provider of screencasts, of course only about MS technologies and yet there are tons of their videos that I should be watching. (Of course, lets not forget about Channel 9 where there are quiet a lot more screencasts about everything from MS)

Anyway there is also this which is run by .NET Rocks guys and provides .NET TV Sessions. The most recent two shows are really something that everyone who is learning .NET should watch. It is by Kathleen Dollard and gives a first hand explanation about .NET 3.5 and shows VB 9.0 and C# 3.0 side by side which is really something! The actual show can be watched here.

I am writing down some important points for my own use and others might find it useful. I hope I am not violating any rights(hopefully).The disclaimer is that the images shown below is taken from the show using Snipping Tool which is shipped along with Windows Vista. This is not my work and I hope not to make any violations. In case of any, please let me know so that I can remove this post online.
Anyway my notes follows ....

.NET 3.5 Assemblies:

.NET 3.5 The important aspect is that .NET 3.5 still has the same Core - .NET 2.0. Simply put .NET 3.5 is nothing but .NET 3.0 plus additional features and we know that .NET 3.0 is nothing but .NET 2.0 Core plus few amazing libraries like the WPF, WCF, WF, CardSpace. It is also mentioned by Kathleen that Speech library is shipped in the .NET 3.0 and it is not that well known.

.NET Versions

.NET Versions There have been five versions of .NET till date. While VB 7 and C# was for .NET 1.0 and 1.1; VB 8 and C# 2.0 was for .NET 2.0 and .NET 3.0.

Now the brand new C# 3.0 and VB 9.0 adds a lot of new features makes programming in these languages very exciting.

Anyway the slides look amazing, you should watch the show without fail if you are interested!

Nullable in VB vs. C#

Declaring Nullable in VB

   1: Dim x as Nullable(Of Boolean) = False
   2: Dim y as Boolean? = False   

C# Nullables and bit more code to help you understand what Nullables are!

   1: bool? x = null;
   2: if(x.HasValue)
   3:  Console.WriteLine("x does have a value!");
   4: else
   5:  Console.WriteLine("x do not have a value and is null!");
   6: bool? y = x.GetValueOrDefault(); //y is initialized to false!
   7: Console.WriteLine("y is "+y);


Suppose x and y are nullable booleans and x is set to null/nothing while y is set to false. Look at these:
x == y => False in C# and Nothing is VB (since one of them is nothing!). So can you guess the output of the following VB code snippet!

   1: Dim x As Nullable(Of Boolean) = Nothing    
   2: Dim y As Boolean = Nothing    
   3: Console.WriteLine("x = " + ((x = y) Is Nothing).ToString)

Another note for beginners! Consider the C# code:

   1: Employee emp = null;    
   2: if(emp==null)    
   3:  Console.WriteLine("Emp object is set to null!");

The equivalent VB code is:

   1: Dim emp as Employee = Nothing    
   2: If(emp = Nothing) 'This does not return true, = Nothing is not equivalent to == null    
   3:  Console.WriteLine("You would not see this line in the output!")    
   4: If emp Is Nothing ' This is how you check for null!    
   5:  Console.WriteLine("emp Object is set to null!")

If you read the above code snippet, I have conveyed the message in the comment on Line 2.

Frankly there are lot more differences with respect to how operators work differently in C# than in VB. Simply put the key point to remember is that

In Visual Basic, comparison between anything that is null(Nothing) to anything else returns Null/Nothing!!

In C#, you cannot use any operator but == on boolean

Note: By the way a great tool for .NET Developers in Snippet Compiler!

Operators in VB vs C#

Ternary Operator in C#:

   1: bool buddi = false;    
   2: string nameOfBuddi = buddi?"Bhargav":"Someone";    
   3: Console.WriteLine("Buddi is "+nameOfBuddi); //Displays "Someone"    
   4: buddi = true;    
   5: Console.WriteLine("Buddi is "+buddi?"Bhargav":"Someone"); //Displays "Bhargav"

Ternary Operator in VB:

IIf was used as ternary operator in VB but it is actually a function! But in VB 9.0, IIF is an operator! The actual difference would be understood when you look at this snippet:
   1: Shared Sub Main()    
   2:   Console.WriteLine(IIf(true,Method1(),Method2()))    
   3: End Sub    
   5: Function Method1 as Integer    
   6:     Console.WriteLine("Method 1")    
   7:     Method1 = 1    
   8: End Function    
  10: Function Method2 as Integer    
  11:     Console.WriteLine("Method 2")    
  12:     Return 5    
  13: End Function

The output of the above snippet is

Method 1
Method 2

As you can see, Method1() and Method2() are first evaluated and then the appropriate return value is returned. Here since its true, return value of Method1() is returned on the screen.

Another note for beginners, in Visual Basic, you can return a value in two ways. The first way is shown in the above code snippet in Line 7 where you assign FunctionName = returnValue and the second way is to use return keyword, like in other languages. You can use which ever makes more sense to you, well based on your choice.

The next few features were actually present in earlier versions of VB and unavailable in C# ( the above talked features were in C# only until VB 9.0)


No comments: