These expressions are not fool proof but they certainly should aid in the conversion of VB code to C# code.
Caveats:
For shorter pieces of code you would be better to just manually convert the code.
The regular expression engine used in the Visual Studio find and replace box and the regex engine used in the dotnet framework are infuriatingly different :(. Visual Studio should be forced to eat its own cooking.
The expressions often rely on the case sensitivity as VB tends to use ProperCase while C# language elements use lowercase
Description | Match Expression | Replace Expression |
Variable declarations | Dim {:a*} As New {[:a.]*} | \2 \1 |
<{:a*} As {[:a.]*} | \2 \1 | |
ByVal {:a*} As {[:a.]*} | \2 \1 | |
ByRef {:a*} As {[:a.]*} | ref \2 \1 | |
If statements | ^{[ \t]*}If {[:a :Pu\<\>\= ]*} Then$ | \1if (\2) \n\1{ |
End If statments | ^{[ \t]*}End If | \1} |
Else statements | ^{[ \t]*}Else | \1}\n\1else\n\1{\n |
Function declarations | ^{[ \t]*}Function {[:a:Pu ]*} As {[:a:Pu ]*}$ | \1 function \3 \2 |
True$ | true | |
False$ | false | |
^{[ \t]*}Return | \1return | |
Another useful way to convert languages is to use Reflector however often the compiled code will have been optimized so not appear as you had imagined it. An example of this is the VB Select Case statement being turned into a series of if statements rather than a tidy switch statement.