本文介绍了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)
vsyourArray.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
传递给经典的 VBMid()
和Replace()
函数.这些不会抛出异常,而是返回Nothing
.
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.- 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 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!- The Many classic VB String functions, including
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. - You can also pass
Nothing
to the classic VBMid()
andReplace()
functions. Instead of throwing an exception, these will returnNothing
.
这篇关于VB 到 C# 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!