VB到C#函数

VB到C#函数,c#,vb.net,operators,vb.net-to-c#,C#,Vb.net,Operators,Vb.net To C#,从VB.Net到C#的下列运算符的等价项是什么 UBound() LBound() 没什么 Chr() Len() UCase() LCase() 左() 右() RTrim() LTrim() Trim() 中() 替换() 拆分() 加入 MsgBox() IIF() 注释 yourArray.GetUpperBound(0)vsyourArray.Length:如果数组长度为零,GetUpperBound将返回-1,而Length将返回0UBound()对于零长度数组将返回-1 VB

从VB.Net到C#的下列运算符的等价项是什么

  • UBound()
  • LBound()
  • 没什么
  • Chr()
  • Len()
  • UCase()
  • LCase()
  • 左()
  • 右()
  • RTrim()
  • LTrim()
  • Trim()
  • 中()
  • 替换()
  • 拆分()
  • 加入
  • MsgBox()
  • IIF()
注释

  • yourArray.GetUpperBound(0)
    vs
    yourArray.Length
    :如果数组长度为零,GetUpperBound将返回-1,而Length将返回0UBound()对于零长度数组将返回-1
  • VB字符串函数使用基于一的索引,而.NET方法使用基于零的索引。即,
    Mid(“asdf”,2,2)
    对应于
    “asdf”。子字符串(1,2)

  • 不是
    IIf
    的完全等价物,因为
    IIf
    总是计算两个参数,而
    只计算它需要的参数。如果评估有副作用,这可能很重要~颤抖
  • 许多经典的VB字符串函数,包括
    Len()
    UCase()
    LCase()
    Right()
    RTrim()
    ,和
    Trim()
    ,将
    Nothing
    Null
    在c#)中的参数视为等同于零长度字符串。当然,在
    Nothing
    上运行字符串方法会引发异常
  • 您还可以将
    Nothing
    传递给经典的VB
    Mid()
    Replace()
    函数。它们不会抛出异常,而是返回
    Nothing

其中大多数是字符串对象上返回修改字符串的实例方法

MsgBox vs. MessageBox.Show(..)
IIf(test,trueval,falseval)
>
(test?trueval:falseval)

IsNothing(obj)
>
(obj==null)

UCase(str)
>
str.ToUpper()


LCase(str)
>
str.ToLower()

您将在上找到这些函数中的许多函数的转换。

如果您查看MSDN,您会发现大多数时候都有两种语言的示例代码

我相信其中一些,如
Mid()
在Microsoft.VisualBasic命名空间中的.NET Framework中仍然可用,您仍然可以从C#代码中引用这些命名空间。

首先,大多数不是运算符。它们是函数,出于兼容性原因,这些函数仅包含在VB.Net中。这意味着您也不应该在VB.net中使用它们,而应该使用新API提供的等价物

  • UBound()-
    arrayVar.Length
  • LBound()-已过时,在正常的.Net数组中下限始终为0
  • IsNothing()-过时。在VB.Net中Use
    为Nothing
    ,在C中Use
    ==null
    #
  • Chr()-
    Convert.ToChar()
    (char)someVar
  • Len()
  • 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()
  • Split()
  • Join()-
    String.Join()
    在VB中也使用它
  • MsgBox()-
    MessageBox.Show()
  • IIF()-
    (条件)?truepart:falsepart
    -请注意有一些区别,因为“?”是运算符而不是函数
      • UBound()->如果x是字符串[]的数组,例如:x.GetUpperBound()
      • LBound()->如果x是字符串[]的数组,例如:x.getLowerBond()
      • IsNothing()->如果(x==null)
      • Chr()->char x=(char)65
      • Len()->x.长度()
      • 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()
      • 修剪()->x.修剪()
      • Mid()->假设x是一个字符串:x.Substring()
      • Replace()->假设x是一个字符串:x.Replace()
      • Split()->假设x是一个字符串:x.Split()
      • Join()->String.Join()
      • MsgBox()->MessageBox.Show()
      • IIF()->三值运算符(x==真?真值:假值)
      所有这些函数都是
      Microsoft.VisualBasic
      程序集中的
      Microsoft.VisualBasic.Information
      类的成员方法,因此您可以直接使用它们。但是,它们中的大多数在核心.NET framework类中具有C#等价物或非语言特定等价物:

      • UBound():
        Array.GetUpperBound
      • LBound():
        Array.GetLowerBound
      • IsNothing():
        ==null
      • Chr():
        (char)intValue
        (cast)
      • Len():
        Strin
        
        MsgBox vs. MessageBox.Show(..)
        IIF vs. (expression?returnValueIfTrue:returnValueElse)
        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;
        
        string MainString = "String Manipulation"; 
        string SearchString = "pul"; 
        int FirstChr = MainString.IndexOf(SearchString); 
        //SHOWS START POSITION OF STRING 
        MessageBox.Show("Found at : " + FirstChr );