public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Archive for the ‘C#’ Category

Difference between C# and VB.NET

Posted by Virendra Dugar on September 18, 2009

This question is almost asked in every interview, if you have worked with both the language. There are syntax difference, keyword difference in both the language. But the interviewer is not looking for such answers. One need to explain the functional difference between both the languages.

Here are some key difference in C# and VB.NET other than syntax differences.

  1. In C#, using keyword is used to release unmanaged resources. (Not available in VB.NET)
  2. Optional parameter is supported in VB.NET. (Not available in C# 3.0 or below version).
  3. Structure and unstructured error handling (On Error GoTo) is supported in VB.NET. (Unstructured error handling is not supported in C#).
  4. Event gets bind automatically in VB.Net.
  5. VB.NET is not case sensitive where C# is.
  6. Shadowing: – This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding the member. You can shadow a base class member in the derived class by using the keyword “Shadows”. The method signature, access level and return type of the shadowed member can be completely different than the base class member.

    Hiding: – This is a C# Concept by which you can provide a new implementation for the base class member without overriding the member. You can hide a base class member in the derived class by using the keyword “new”. The method signature, access level and return type of the hidden member has to be same as the base class member.

    Comparing the two:-
    1) The access level, signature and the return type can only be changed when you are shadowing with VB.NET. Hiding and overriding demands these parameters as same.
    2) The difference lies when you call the derived class object with a base class variable. In class of overriding although you assign a derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function will be called.

  7. Visual Basic .NET can also force parameters to be passed by value, regardless of how they are declared, by enclosing the parameters in extra parentheses. There is no way to achieve this thing in C#.
    For Example:

    Dim y As Integer = 5
    Dim z As Integer
    z = Add(y) //This will set both Y and Z to 6.
    z = Add((y)) //This will set Z to 6 but Value of Y will not be change, as we have included extra parenthese while calling.

    The Add function:

    Public Function Add(ByRef x As Integer) As Integer
    x = x + 1
    Return x
    End Function

I know this is not a complete list, so if you know any other difference kindly post your comments.



Posted in C#, VB.NET | Tagged: , | 2 Comments »

Using C# and VB.NET classes together in the App_Code folder

Posted by Virendra Dugar on June 15, 2009

Download Source Code

Learn new ASP.NET Core 1.0 Framework

In ASP.NET 2.0 one can mix web forms coded in C# and VB.NET together in a single web site. This works great for web forms. However if you want to code classes from App_Code folder in different languages? Such mixing of classes coded in different languages is not allowed with default settings. You can, however,configure your web site to get this done. This article is going to explain you that how one can do that.

Creating sub folders and classes

First of all, Create a new webSite in VS.NET 2005. Add App_Code folder to it. You can do so easily by right clicking on the web site in the Solution Explorer and choosing Add ASP.NET Folder” option (see below).

Different Language

Once you add the App_Code folder add two class files – Class1.cs and Class2.vb. Note that one class file must be in C# where as the other must be in VB.NET. Add the following code in Class1.cs.
C# Code

Similarly, add the following code in Class2.vb.


Both of these classes contain a method called HelloWorld() that simply return a string to the caller.

Now try compiling the web site. What happens? You will get an error as shown below:

Error 1 The files ‘/VBandCSharptogether/App_Code/Class2.vb’  and ‘/VBandCSharptogether/App_Code/Class1.cs’ use a
different language, which is not allowed since they need to be compiled together.

The error message bluntly tells us that you can not use different coding languages for the classes in App_Code folder. Fortunately, there is a way to get out of this trap. Firstly you need to put C# and VB.NET classes in separate sub-folders under App_Code. Secondly you need to add some markup in the web.config file to tell ASP.NET compiler about your intention.

<codeSubDirectories> Section

Create two folders under App_Code filder named CSCode and VBCode. Move Class1.cs inside CSCode folder and Class2.vb inside VBCode folder.

Add a web.config file to your web site and add the following markup to it:

Web Config Code

Here, we added <compilation> section. The <codeSubDirectories> section defines a set of sub-directories relative to App_Code folder that are compiled at run time. The directoryName attribute points to the sub-folder of App_code. Each sub folder is compiled separately and hence each can have classes coded in different languages. We added our CSCode and VBCode folder in this section. This way the compiler will compile classes from CSCode and VBCode folders separately.

After configuring the web site try to compile it. This time it compiles successfully.


Quick summary of what’s changed in ASP.NET Core 1.0

Posted in ASP.NET, C#, VB.NET | Tagged: , , | 18 Comments »

C# 4.0 Features

Posted by Virendra Dugar on May 7, 2009

As we all know, .NET 4.0 is around the corner so as C# 4.0. As we know, each version of C# was introduced with some major features. For example, C# 1.0 major feature was managed code. In C# 2.0, Generics were introduced. In C# 3.0, Automatic properties, object initializer, collection initializer and LINQ were launched. C# 4.0 comes with these major feartures:

  1. Dynamic LookUp
  2. Named and optional argument (Much needed)
  3. Improved COM interoperability
  4. Co and Contra Variance

You can download a document related to new features of C# 4.0 from MSDN website. This document explains all the above features.

You can also check this article on Code project.


Posted in ASP.NET 4.0, C# | Tagged: , | Leave a Comment »

Visual studio keyboard shortcuts

Posted by Virendra Dugar on March 10, 2009

Download the cool PDF versions of all the keyboard shortcut available in Visual Studio 2005 and 2008.

For C#

Make habit of using shortcut keys and save your time. 🙂


Posted in ASP.NET, C#, VB.NET | Tagged: , , | Leave a Comment »

Good Programming Practices

Posted by Virendra Dugar on February 5, 2009

I found a very cool document reagrding good programming practices.. Go and check it out guys..

If you are not able to view then click here.



Posted in ASP.NET, C# | Tagged: , | Leave a Comment »

ref and out in C#

Posted by Virendra Dugar on February 3, 2009

When we pass a parameter as ref to a method, the method refers to the same variable and changes made will affect the actual variable. Even the variable passed as out parameter is similar to ref, but there are few implementation differences when you use it in C# .

Argument passed as ref must be initialized before it is passed to the method, where as in case of out its is not necessary,but after a call to the method as an out parameter the variable must be initialized.

When to use ref and out parameter

Out parameter can be used when we want to return more than one value from a method.


Posted in C# | Tagged: | Leave a Comment »

Parse and TryParse

Posted by Virendra Dugar on January 30, 2009

Parse method is used to parse any value to specified data type.

For example
string test = “42”;
int32 Result;
Result = Int32.Parse(test);

This will work fine but what if when you are not sure about the value of string variable test. As for example if test=”abc” and if you try to use above method, .NET will throw an exception as you are trying to convert string data to integer.

TryParse is a good method if the string you are converting to an interger is not always numeric.

if(!Int32.TryParse(test,out iResult))
//do something

TryParse method returns a boolean to denote whether the conversion has been successfull or not, and returns the converted value through an out parameter.

**declare variable iResult of Int32.


Posted in C# | Tagged: | 1 Comment »

%d bloggers like this: