Keywords
C# | VB.NET |
this | Me |
base | MyBase |
abstract | MustOverride/MustInherit |
virtual | Overridable |
sealed | NotInheritable |
class Class : Interface | Implements (statement) |
internal | Friend |
static | Shared |
typeof() | GetType() |
If you want a static class in VB.NET, you'll need to use the Module keyword.
(One thing to note here is how much more intelligible some of the VB.NET keywords are.)
Logic
C# | VB.NET |
&& | AndAlso |
|| | OrElse |
There is no equivalent for And and Or in C#.
Numeric type suffixes
C# | VB.NET |
12.34M (for Money) | 12.34D (for Decimal) |
12.34D (for Double) | 12.34R (for Real) |
Lambdas
C# | apples.Single(x => x.Colour = "red") |
VB.NET | apples.Single(Function(x) x.Colour = "red") |
Initialising lists and objects
C# | var apple = new Fruit { Colour = "green" }; |
VB.NET | Dim apple = New Fruit With {.Colour = "green"} |
C# | var apples = new List |
VB.NET | Dim apples = New List(Of Fruit) From {New Fruit With {.Colour = "red"}, New Fruit With {.Colour = "green"}} |
Anonymous types
C# | apples.Select(x => new { Colour = x.Colour }); |
VB.NET | apples.Select(Function(x) New With {.Colour = x.Colour}) |
Nulls and Nothing
If you're coming from C#, the Nothing keyword does not do what you'd expect. What would you expect the following code to do?
Dim value = "" If value = Nothing OrElse value Is Nothing Then Throw New Exception() End If
If you said "not throw an exception" you'd be wrong. Weirdly, the first condition is true but the second condition is false, so it throws. Compare with similar C# code:
var value = ""; if (value == null) throw new Exception();
In this case, the exception doesn't get thrown.
No comments:
Post a Comment