VB 到 C# 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1722896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB to C# Functions
提问by Jonathan Escobedo
Which are the equivalent of the following operators from VB.Net to C#?
从 VB.Net 到 C#,以下运算符的等效项是什么?
- UBound()
- LBound()
- IsNothing()
- Chr()
- Len()
- UCase()
- LCase()
- Left()
- Right()
- RTrim()
- LTrim()
- Trim()
- Mid()
- Replace()
- Split()
- Join()
- MsgBox()
- IIF()
- UBound()
- LBound()
- 没什么()
- Chr()
- 连()
- UCase()
- LCase()
- 剩下()
- 对()
- RTrim()
- LTrim()
- 修剪()
- 中()
- 代替()
- 分裂()
- 加入()
- 消息框()
- IIF()
采纳答案by Gavin Miller
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)
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 botharguments, 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
.
yourArray.GetUpperBound(0)
vsyourArray.Length
:如果数组长度为零,GetUpperBound 将返回 -1,而 Length 将返回 0。UBound()
在 VB.NET 中,零长度数组将返回 -1。- VB 字符串函数使用基于 1 的索引,而 .NET 方法使用基于 0 的索引。即
Mid("asdf",2,2)
对应于"asdf".SubString(1,2)
。 ?
不完全等同于IIf
因为IIf
总是评估两个参数,并且?
只评估它需要的一个。如果评价有副作用的话,这也没关系~不寒而栗!- 许多经典的 VB 字符串函数,包括
Len()
、UCase()
、LCase()
、Right()
、RTrim()
和Trim()
,会将Nothing
(Null
在 c# 中)的参数视为等价于零长度字符串。运行字符串方法Nothing
当然会抛出异常。 - 也可以传给
Nothing
经典的VBMid()
和Replace()
函数。这些将返回,而不是抛出异常Nothing
。
回答by Wim Hollebrandse
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)
回答by Tim S. Van Haren
IIf(test, trueval, falseval)
>> (test ? trueval : falseval);
IIf(test, trueval, falseval)
>> (test ? trueval : falseval);
IsNothing(obj)
>> (obj == null);
IsNothing(obj)
>> (obj == null);
UCase(str)
>> str.ToUpper();
UCase(str)
>> str.ToUpper();
LCase(str)
>> str.ToLower();
LCase(str)
>> str.ToLower();
回答by Meta-Knight
You'll find the conversion for many of these functions on this wikipedia page.
您可以在此维基百科页面上找到许多这些函数的转换。
回答by RvdK
If you look on MSDN you see that most of the time there are sample code for both languages.
如果您查看 MSDN,您会发现大部分时间都有两种语言的示例代码。
回答by Adam Neal
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.
我相信其中一些Mid()
在 Microsoft.VisualBasic 命名空间中的 .NET Framework 中仍然可用,您仍然可以从 C# 代码中引用。
回答by PMN
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;
回答by Joel Coehoorn
First of all, most of those are NOT operators. They are functions, and the functions are only included in VB.Net for compatibility reasons. That means you shouldn't use them in VB.net either, and instead use the equivalents provided by the new API.
首先,其中大部分都不是运算符。它们是函数,出于兼容性原因,这些函数仅包含在 VB.Net 中。这意味着您也不应该在 VB.net 中使用它们,而应使用新 API 提供的等效项。
- UBound()—
arrayVar.Length
- LBound()— obsolete, lower bound is always0 in a normal .Net array
- IsNothing()— obsolete. Use
Is Nothing
in VB.Net and== null
in C# - Chr()—
Convert.ToChar()
or(char)someVar
- Len()—
stringVar.Length
use this in VB too - UCase()—
stringVar.ToUpper()
use this in VB too - LCase()—
stringVar.ToLower()
use this in VB too - Left()—
stringVar.Substring(0, n)
use this in VB too - Right()—
stringVar.Substring(stringVar.Length - n)
use this in VB too - RTrim()—
stringVar.TrimEnd()
use this in VB too - LTrim()—
stringVar.TrimStart()
use this in VB too - Trim()—
stringVar.Trim()
use this in VB too - Mid()—
stringVar.Substring(n, m)
use this in VB too - Replace()—
stringVar.Replace()
use this in VB too - Split()—
stringVar.Split()
use this in VB too - Join()—
String.Join()
use this in VB too - MsgBox()—
MessageBox.Show()
- IIF()—
(condition) ? truepart : falsepart
- note that there are some differences, because "?" is an operator and not a function
- UBound()—
arrayVar.Length
- LBound()— 已过时,在普通 .Net 数组中下限始终为0
- IsNothing()— 已过时。使用
Is Nothing
在VB.Net,并== null
在C# - Chr()—
Convert.ToChar()
或(char)someVar
- Len()—
stringVar.Length
在 VB 中也使用它 - UCase()—
stringVar.ToUpper()
在 VB 中也使用它 - LCase()—
stringVar.ToLower()
在 VB 中也使用它 - Left()—
stringVar.Substring(0, n)
在 VB 中也使用它 - Right()—
stringVar.Substring(stringVar.Length - n)
在 VB 中也使用它 - RTrim()—
stringVar.TrimEnd()
在 VB 中也使用它 - LTrim()—
stringVar.TrimStart()
在 VB 中也使用它 - Trim()—
stringVar.Trim()
在 VB 中也使用它 - Mid()—
stringVar.Substring(n, m)
在 VB 中也使用它 - Replace()—
stringVar.Replace()
在 VB 中也使用它 - Split()—
stringVar.Split()
在 VB 中也使用它 - Join()—
String.Join()
在 VB 中也使用它 - MsgBox()—
MessageBox.Show()
- IIF()——
(condition) ? truepart : falsepart
注意有一些区别,因为“?” 是一个运算符而不是一个函数
回答by Scott Anderson
- UBound() -> if x is an array of string[] for example: x.GetUpperBound();
- LBound() -> if x is an array of string[] for example: x.GetLowerBound();
- IsNothing() -> if (x == null)
- Chr() -> char x = (char)65;
- Len() -> x.Length();
- UCase() -> assume x is a string: x.ToUpper();
- LCase() -> assume x is a string: x.ToLower();
- Left() -> assume x is a string: x.Substring(0, 10); // first 10 characters
- Right() -> assume x is a string: x.Substring(x.Length - 10); // last 10 characters
- RTrim() -> x.TrimEnd();
- LTrim() -> x.TrimStart();
- Trim() -> x.Trim();
- Mid() -> assume x is a string: x.Substring()
- Replace() -> assume x is a string: x.Replace();
- Split() -> assume x is a string: x.Split();
- Join() -> String.Join();
- MsgBox() -> MessageBox.Show();
- IIF() -> ternary operator (x == true ? true-value : false-value);
- UBound() -> 如果 x 是一个 string[] 数组,例如:x.GetUpperBound();
- LBound() -> 如果 x 是一个 string[] 数组,例如:x.GetLowerBound();
- IsNothing() -> if (x == null)
- Chr() -> char x = (char)65;
- Len() -> x.Length();
- UCase() -> 假设 x 是一个字符串:x.ToUpper();
- LCase() -> 假设 x 是一个字符串:x.ToLower();
- Left() -> 假设 x 是一个字符串: x.Substring(0, 10); //前10个字符
- Right() -> 假设 x 是一个字符串: x.Substring(x.Length - 10); // 最后10个字符
- RTrim() -> x.TrimEnd();
- LTrim() -> x.TrimStart();
- Trim() -> x.Trim();
- Mid() -> 假设 x 是一个字符串:x.Substring()
- Replace() -> 假设 x 是一个字符串: x.Replace();
- Split() -> 假设 x 是一个字符串: x.Split();
- Join() -> String.Join();
- MsgBox() -> MessageBox.Show();
- IIF() -> 三元运算符 (x == true ? true-value : false-value);
回答by Thomas Levesque
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 :
所有这些函数都是Microsoft.VisualBasic.Information
类的成员方法,在Microsoft.VisualBasic
程序集中,所以你可以直接使用它们。但是,它们中的大多数在核心 .NET 框架类中具有 C# 等效项或非特定于语言的等效项:
- UBound() :
Array.GetUpperBound
- LBound() :
Array.GetLowerBound
- IsNothing() :
== null
- Chr() :
(char)intValue
(cast) - Len() :
String.Length
- UCase() :
String.ToUpper
- LCase() :
String.ToLower
- Left(), Right() and Mid() :
String.Substring
(with different arguments) - RTrim() :
String.TrimEnd
- LTrim() :
String.TrimStart
- Trim() :
String.Trim
- Replace() :
String.Replace
- Split() :
String.Split
- Join() :
String.Join
- MsgBox() :
MessageBox.Show
- IIF() :
condition ? valueIfTrue : valueIfFalse
(conditional operator)
- UBound() :
Array.GetUpperBound
- LBound() :
Array.GetLowerBound
- 没什么() :
== null
- Chr() :
(char)intValue
(演员) - 伦():
String.Length
- UCase() :
String.ToUpper
- LCase() :
String.ToLower
- Left()、Right() 和 Mid() :(
String.Substring
使用不同的参数) - RTrim() :
String.TrimEnd
- LTrim() :
String.TrimStart
- 修剪() :
String.Trim
- 代替() :
String.Replace
- 分裂() :
String.Split
- 加入() :
String.Join
- MsgBox() :
MessageBox.Show
- IIF() :
condition ? valueIfTrue : valueIfFalse
(条件运算符)
Links
链接