VB 到 C# 函数

扫码查看
本文介绍了VB 到 C# 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下从 VB.Net 到 C# 的运算符有哪些等价的?

Which are the equivalent of the following operators from VB.Net to C#?

  • UBound()
  • LBound()
  • IsNothing()
  • Chr()
  • Len()
  • UCase()
  • LCase()
  • 左()
  • 右()
  • RTrim()
  • LTrim()
  • 修剪()
  • 中()
  • 替换()
  • 拆分()
  • 加入()
  • MsgBox()
  • IIF()

推荐答案

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")

注意事项

  • yourArray.GetUpperBound(0) vs yourArray.Length:如果数组的长度为零,GetUpperBound 将返回 -1,而 Length 将返回 0.UBound() 将为零长度数组返回 -1.
  • VB 字符串函数使用基于 1 的索引,而 .NET 方法使用基于 0 的索引.IE.Mid("asdf",2,2) 对应于"asdf".SubString(1,2).
  • ? 不是 IIf 的完全等价物,因为 IIf 总是计算 both 参数和 ? 只评估它需要的那个.如果评估有副作用,这可能很重要~不寒而栗!
  • 许多经典的VB字符串函数,包括Len()UCase()LCase()Right()RTrim()Trim() 将处理 Nothing (Null在 c# 中)相当于零长度的字符串.在 Nothing 上运行字符串方法当然会抛出异常.
  • 您还可以将 Nothing 传递给经典的 VB Mid()Replace() 函数.这些不会抛出异常,而是返回 Nothing.
  • 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.

这篇关于VB 到 C# 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:48
查看更多