Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

vb.net - VB to C# Functions

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()
question from:https://stackoverflow.com/questions/1722896/vb-to-c-sharp-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.
  • The VB string functions uses a one based index, while the .NET method uses a zero based index. I.e. 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!
  • The Many classic VB String functions, including Len(), UCase(), LCase(), Right(), RTrim(), and Trim(), will treat an argument of Nothing (Null in c#) as being equivalent to a zero-length string. Running string methods on Nothing will, of course, throw an exception.
  • You can also pass Nothing to the classic VB Mid() and Replace() functions. Instead of throwing an exception, these will return Nothing.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...