下面哪些運算符等同於VB.Net到C#?VB到C#的功能
- UBound函數()
- LBOUND()
- IsNothing()
- CHR()
- LEN()
- 用Ucase()
- LCASE()
- 左()
- 右()
- RTRIM()
- LTRIM()
- TRIM()
- MID()
- 更換()
- 斯普利特()
- 加入()
- MsgBox()
- IIF()
下面哪些運算符等同於VB.Net到C#?VB到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")
注
yourArray.GetUpperBound(0)
VS yourArray.Length
:如果數組是零長度的,GetUpperBound將返回-1,而長度將在VB.NET返回0 UBound()
將返回-1爲零長度數組。Mid("asdf",2,2)
對應於"asdf".SubString(1,2)
。?
不是IIf
完全等效,因爲IIf
總是計算都參數,?
只評估它需要的一個。如果有評估〜不寒而慄的副作用,這可能很重要!Len()
,UCase()
,LCase()
,Right()
,RTrim()
,和Trim()
,將治療的Nothing
(Null
在c#)的自變量爲等同於一個零長度的字符串。當然,在Nothing
上運行字符串方法會引發異常。Nothing
傳遞給經典的VB Mid()
和Replace()
函數。這些將不會拋出異常,而會返回Nothing
。它是CW所以隨時糾正/修改/添加 – 2009-11-12 15:16:38
保重,雖然,並仔細檢查你沒有踩別人的編輯 – ChrisF 2009-11-12 15:21:55
我懷疑你扭轉UBound和LBound,但我不知道VB。 – 2009-11-12 15:23:15
其中大部分將是返回修改字符串的字符串對象上的實例方法。
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();
你會發現this wikipedia page許多這些功能的轉換。
如果你看看你的時間也有兩種語言的示例代碼的MSDN最見。
相信喜歡Mid()
其中的一些仍然在.NET Framework提供的Microsoft.VisualBasic命名空間,你仍然可以從C#代碼的參考。
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;
「array」.Length實際上是UBound + 1 ... – 2009-11-12 15:32:12
首先,那些最沒有運營商。他們是功能,和功能只包括在VB.Net出於兼容性原因。這意味着你不應該在VB.net或者使用它們,等同而是使用所提供的新的API。
arrayVar.Length
Is Nothing
。在C#Convert.ToChar()
或(char)someVar
stringVar.Length
使用這種在VB太stringVar.ToUpper()
使用本太== null
LCase() —在VB中也使用這個stringVar.Substring(0, n)
在VB在VB中使用這種在VB太stringVar.Substring(stringVar.Length - n)
使用本太stringVar.TrimEnd()
使用本太stringVar.TrimStart()
在VB中也使用這個stringVar.Trim()
使用這種在VB太stringVar.Substring(n, m)
使用這種在VB太stringVar.Replace()
使用這種在VB太stringVar.Split()
在VB中也使用它String.Join()
在VB中使用過MessageBox.Show()
(condition) ? truepart : falsepart
- 「?」 注意,有一些差異,因爲是一個操作符,而不是一個功能arrayVar.Length實際上是UBound + 1 ... – 2009-11-12 15:31:24
@Thomas:它取決於如何在舊的vb中設置Option Base。 – 2009-11-12 15:37:52
COM調用返回的數組下限可能不是0。 – Govert 2015-03-23 22:58:32
所有這些功能都是Microsoft.VisualBasic.Information
類的成員方法,在Microsoft.VisualBasic
程序集中,因此您可以直接使用它們。然而,大多有C#當量,或非語言特定當量在覈心.NET框架類:
Array.GetUpperBound
Array.GetLowerBound
== null
(char)intValue
(澆鑄)String.Length
String.ToUpper
String.ToLower
String.Substring
(具有不同參數)String.TrimEnd
String.TrimStart
String.Trim
String.Replace
String.Split
String.Join
MessageBox.Show
condition ? valueIfTrue : valueIfFalse
(條件運算符)鏈接
另外一...
VB - IsDBNull以便(值)
C# - yourdatarow。ISNULL( 「列名」)
還有一個除了這可能是的IndexOf()函數的字符串中查找字符串
下面的例子...
string MainString = "String Manipulation";
string SearchString = "pul";
int FirstChr = MainString.IndexOf(SearchString);
//SHOWS START POSITION OF STRING
MessageBox.Show("Found at : " + FirstChr);
除了答案以上。 請注意替換Len() - > x.Length。 VB Len()允許你傳遞null,但在C#中你會得到一個異常。 有時它會更好地使用String.IsNullrEmpty()(如果情況允許的話)
空間功能是從其他人的名單丟失:
Space(16) -> new String(" ", 16)
這些都是真正從VB6遺留下來的呢 - 在VB.NET你應該使用String或Array對象的方法,例如String.TrimLeft,String.ToUpper和Array.GetUpperBound。當然MessageBox.Show – MartW 2009-11-12 15:20:29
僅供參考,它們不是真正的「VB.NET」運算符,VB.NET中包含兼容性庫(如果需要,可以選擇在C#中使用,只需添加引用和使用)從VB.OLD進行這些功能 - 所以如果一個人想要真正迂腐... – Murph 2009-11-12 15:22:28
只是一個小挑剔:這些都是功能。 **沒有**他們是運營商。 – 2009-11-12 15:25:00