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#?
VB C#
UBound() = yourArray.GetUpperBound(0) or yourArray.Length for one-dimesional arrays
LBound() = yourArray.GetLowerBound(0)
IsNothing() = Object.ReferenceEquals(obj,null)
Chr() = Convert.ToChar()
Len() = "string".Length
UCase() = "string".ToUpper()
LCase() = "string".ToLower()
Left() = "string".Substring(0, length)
Right() = "string".Substring("string".Length - desiredLength)
RTrim() = "string".TrimEnd()
LTrim() = "string".TrimStart()
Trim() = "string".Trim()
Mid() = "string".Substring(start, length)
Replace() = "string".Replace()
Split() = "string".Split()
Join() = String.Join()
MsgBox() = MessageBox.Show()
IIF() = (boolean_condition ? "true" : "false")
Notes
yourArray.GetUpperBound(0) vs yourArray.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 of IIf because IIf always evaluates both arguments, and ? only evaluates the one it needs. This could matter if there are side effects of the evaluation ~ shudder!Most of these would be instance methods on the string object that return the modified string.
MsgBox vs. MessageBox.Show(..)
IIF vs. (expression?returnValueIfTrue:returnValueElse)
IIf(test, trueval, falseval) >> (test ? trueval : falseval);
IsNothing(obj) >> (obj == null);
UCase(str) >> str.ToUpper();
LCase(str) >> str.ToLower();
You'll find the conversion for many of these functions on this wikipedia page.
If you look on MSDN you see that most of the time there are sample code for both languages.
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.
UBound() "array".Length
LBound()
IsNothing(): "object" == null
Chr() (char)"N"
Len() "string".Length
UCase() "string".ToUpper()
LCase() "string".ToLower()
Left() "string".Substring(from, to)
Right() "string".Substring(from, to)
RTrim() "string".TrimEnd()
LTrim() "string".TrimStart()
Trim() "string".Trim()
Mid() "string".Substring(from, to)
Replace() "string".Replace()
Split() "string".Split()
Join() String.Join()
MsgBox() MessageBox.Show()
IIF() validate ? iftrue : iffalse;
All these functions are member methods of the Microsoft.VisualBasic.Information class, in the Microsoft.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.GetUpperBoundArray.GetLowerBound== null(char)intValue (cast)String.LengthString.ToUpperString.ToLowerString.Substring (with different arguments)String.TrimEndString.TrimStartString.TrimString.ReplaceString.SplitString.JoinMessageBox.Showcondition ? valueIfTrue : valueIfFalse (conditional operator)Links
Another one...
VB - IsDBNull(value)
C# - yourdatarow.IsNull("columnName")