Convert VB.NET To C# Or Vice Versa
There are tools to do this, but it is still useful to remember these tips.
1.
|
|
Case sensitivity |
|
VB.NET |
No |
|
C# |
Yes |
2.
|
|
Comment |
|
VB.NET |
‘ |
|
C# |
//, /* */ |
3.
|
|
Reference |
|
VB.NET |
Imports System
Imports System.Web.Security |
|
C# |
using System;
using System.Web.Security; |
4.
|
|
Inherit |
|
VB.NET |
Public Class MyClass
Inherits System.Web.UI.Page |
|
C# |
public class MyClass : System.Web.UI.Page |
5.
|
|
Region |
|
VB.NET |
#Region " Web Form Designer Generated Code " |
|
C# |
#region Web Form Designer generated code |
6.
|
|
Sub / void |
|
VB.NET |
Private Sub Page_Load() … End Sub |
|
C# |
private void Page_Load(){…} |
7.
|
|
If Clause |
|
VB.NET |
If … Then … Else … End If |
|
C# |
If(…) {…} else {…} |
8.
|
|
Declare |
|
VB.NET |
Dim strMyConn As String = "abc" |
|
C# |
string strMyConn = "abc"; |
9.
|
|
Event Handler |
|
VB.NET |
Private Sub txtEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
…
End Sub
|
|
C# |
private void InitializeComponent()
{
this.txtEmail.TextChanged += new System.EventHandler(this.txtEmail_TextChanged);
…
}
private void txtEmail_TextChanged(object sender, System.EventArgs e)
{
…
} |
10.
|
|
AndAlso / && |
|
VB.NET |
AndAlso |
|
C# |
&& |
11.
|
|
Array |
|
VB.NET |
arrWeekDay(1) = "Monday" |
|
C# |
arrWeekDay[1] = " Monday "; |
12.
|
|
IsNothing / null |
|
VB.NET |
If IsNothing(MyType) |
|
C# |
if(MyType == null) |
13.
|
|
Multi Line String Combination |
|
VB.NET |
&(space)_ |
|
C# |
+ |
14.
|
|
Try Catch |
|
VB.NET |
Try
…
Catch ex As Exception
…
End Try |
|
C# |
try
{
}
catch
{
} |
15.
|
|
Exit Loop |
|
VB.NET |
Exit Do/While/For |
|
C# |
break; |
If the conversion does need to be done manually, what I feel kind of boring include:
1. Array
For example, ConfigurationSettings.AppSettings, Request.Cookies, Request.QueryString, SqlParameter etc
Because this can never be fully done by find/replace.
2. property and method
For example, SelectedValue is a property of a dropdown list. In VB.NET, both ddlCountry.SelectedValue and ddlCountry.SelectedValue() are fine; in C#, only ddlCountry.SelectedValue is correct. Same to the Text property to a textbox.
Another example, both ToString and ToString() are good in VB.NET, however, only ToString() passes in C#.