A Couple VB.NET Language Tips for C# Developers
Originally, I started out as a Visual Basic developer, and have since moved mostly to C#. However, when doing consulting work, I do need to cross back and forth quite often. Here are a couple VB.NET tips that you probably aren’t aware of if you’re mostly a C# developer. Some VB.NET developers may not even know about then either.
Null Coalesce
Null Coalescing is really simple in C#:
// If "someValue" is Null then set "i" to 0 (zero)
// otherwise set it to the value of "someValue"
int? i = someValue ?? 0;
But what about VB.NET?
Ternary Conditional Operator
This is the ability have a complete If..Else..Then statement on a single line and have it return a value. This is really simple in C#: ```csharp // This performs the same logical operation as the Null Coalesce example above int? i = (someValue == null ? 0 : someValue); ``` How about in VB.NET?Lock Statement
You may be familiar with the *lock *statement in C#, especially if you're used to worrying about concurrency. ```csharp lock (expression) { ...Some Code... } ``` At first it appears to not exist in VB.NET, but they just named it SyncLock instead:Related Posts
-
C#: Console App that Accepts Command-Line Arguments
23 Apr 2024 -
C#: How to iterate over a dictionary?
20 Apr 2024 -
C#: What is the difference between 'string' and System.String?
16 Apr 2024