Which are the equivalent of the following operators from VB.Net to C#?
- UBound()
- LBound()
- IsNothing()
- Chr()
- Len()
- UCase()
- LCase()
- Left()
- Right()
- RTrim()
- LTrim()
- Trim()
- Mid()
- Replace()
- Split()
- Join()
- MsgBox()
- IIF()
Which are the equivalent of the following operators from VB.Net to C#?
Most of these would be instance methods on the string object that return the modified string.
IIf(test, trueval, falseval)
>>(test ? trueval : falseval);
IsNothing(obj)
>>(obj == null);
UCase(str)
>>str.ToUpper();
LCase(str)
>>str.ToLower();
Notes
yourArray.GetUpperBound(0)
vsyourArray.Length
: if the array is zero-length, GetUpperBound will return -1, while Length will return 0.UBound()
in VB.NET will return -1 for zero-length arrays.Mid("asdf",2,2)
corresponds to"asdf".SubString(1,2)
.?
is not the exact equivalent ofIIf
becauseIIf
always evaluates both arguments, and?
only evaluates the one it needs. This could matter if there are side effects of the evaluation ~ shudder!Len()
,UCase()
,LCase()
,Right()
,RTrim()
, andTrim()
, will treat an argument ofNothing
(Null
in c#) as being equivalent to a zero-length string. Running string methods onNothing
will, of course, throw an exception.Nothing
to the classic VBMid()
andReplace()
functions. Instead of throwing an exception, these will returnNothing
.In addition to the answers above. Be carefull with replacing Len() -> x.Length. VB Len() allows you to pass null, but in c# you will get an exception. Sometimes it would be better to use String.IsNullrEmpty() (If the situation allows)
All these functions are member methods of the
Microsoft.VisualBasic.Information
class, in theMicrosoft.VisualBasic
assembly, so you can use them directly. However, most of them have C# equivalents, or non language specific equivalents in core .NET framework classes :Array.GetUpperBound
Array.GetLowerBound
== null
(char)intValue
(cast)String.Length
String.ToUpper
String.ToLower
String.Substring
(with different arguments)String.TrimEnd
String.TrimStart
String.Trim
String.Replace
String.Split
String.Join
MessageBox.Show
condition ? valueIfTrue : valueIfFalse
(conditional operator)Links
I believe some of these like
Mid()
are still available in the .NET Framework in the Microsoft.VisualBasic namespace which you can still reference from C# code.