简体   繁体   English

C#和VB.Net字符串比较的区别

[英]Difference between C# and VB.Net string comparison

I'm actually trying to answer this question but as that's very involved and unlikely to get a good response quickly, I'm going to try and work out the implementation myself.我实际上是在尝试回答这个问题,但由于这非常复杂,而且不太可能很快得到很好的回应,我将尝试自己制定实施方案。 The fundamental problem seems to be that the C# example I was following doesn't translate directly to VB.基本问题似乎是我所遵循的 C# 示例没有直接转换为 VB。

When examining a String comparison BinaryExpression in a lambda, VB reports the Expression.Method.DeclaringType to be Microsoft.VisualBasic.CompilerServices.Operators with a method name of CompareString .在检查 lambda 中的字符串比较BinaryExpression时,VB 报告Expression.Method.DeclaringTypeMicrosoft.VisualBasic.CompilerServices.Operators ,方法名称为CompareString This is clearly VB-specific.这显然是特定于 VB 的。

The Expression is just comparing x.Content_Type <> "" and calling ToString on it returns {(CompareString(x.Content_Type, "", False) != 0)} - which seems pretty logical (CompareString docs here ).表达式只是比较x.Content_Type <> ""并在其上调用 ToString 返回{(CompareString(x.Content_Type, "", False) != 0)} - 这似乎很合乎逻辑(CompareString docs here )。

Can someone explain to me how (or even better, why) VB and C# handle string comparisons differently.有人可以向我解释一下(甚至更好,为什么)VB 和 C# 以不同的方式处理字符串比较。

I think if I can get an answer to that, I should be able to work out a solution to the other problem.我想如果我能得到答案,我应该能够找到另一个问题的解决方案。

Edit: To clarify, I'm implementing a custom LINQ provider which is examining the following Where call:编辑:为了澄清,我正在实现一个自定义 LINQ 提供程序,它正在检查以下Where调用:

Query.Where(function(x) x.Content_Type <> "")

or the C# equivalent...或 C# 等价物...

query.Where(x=>x.Content_Type!="");

As far as I'm aware, the 2 should be functionally identical据我所知,2 应该在功能上相同

VB.NET inherited the Option Compare statement from previous versions of Visual Basic. VB.NET 从以前版本的 Visual Basic 继承了Option Compare语句。 To make that work, all string comparison expressions in VB.NET are translated to a helper function that can find out what the selected Option Compare value was in the specific source code file in which the statement was written.为了实现这一点,VB.NET 中的所有字符串比较表达式都被转换为一个辅助函数,该函数可以找出在编写该语句的特定源代码文件中所选的 Option Compare 值是什么。

The Operators.CompareString(string, string, bool) method is that helper function. Operators.CompareString(string, string, bool) 方法就是这个辅助函数。 The last argument is named "TextCompare", the VB.NET compiler automatically passes True if Option Compare Text is in effect, False if Option Compare Binary is in effect.最后一个参数被命名为“TextCompare”,如果选项比较文本有效,VB.NET 编译器会自动传递 True,如果选项比较二进制有效,则 False 。

C# doesn't have anything like that. C# 没有这样的东西。

Decompiling CompareString gives反编译CompareString给出

public static int CompareString(string Left, string Right, bool TextCompare)
{
  if (Left == Right)
    return 0;
  if (Left == null)
    return Right.Length == 0 ? 0 : -1;
  else if (Right == null)
  {
    return Left.Length == 0 ? 0 : 1;
  }
  else
  {
    int num = !TextCompare 
       ? string.CompareOrdinal(Left, Right) 
       : Utils.GetCultureInfo().CompareInfo
              .Compare(Left, Right, CompareOptions.IgnoreCase 
                                  | CompareOptions.IgnoreKanaType 
                                  | CompareOptions.IgnoreWidth);
    if (num == 0)
      return 0;
    return num > 0 ? 1 : -1;
  }
}

from which can be seen that there's custom logic around null (" Nothing in Visual Basic", as the refrain goes) handling, and more importantly, a mode-switching parameter TextCompare , which takes its value from the Option Compare setting in effect.从中可以看出,围绕null (“Visual Basic 中的Nothing ”,正如副歌所说)处理有自定义逻辑,更重要的是,模式切换参数TextCompare ,它从有效的Option Compare设置中获取其值。

Perhaps explicitly using a method on string , rather than a comparison operator , would help you out.也许,明确使用方法string ,而不是比较操作,会帮助你。

As to the 'why', well, VB (classic) was always culturally a more "do the sensible thing" language, as opposed to the "do exactly what I tell you, nothing more, nothing less" philosophy of the C++ / Win32 world.至于“为什么”,嗯,VB(经典)在文化上始终是一种更“做明智之事”的语言,而不是“完全按照我告诉你的,仅此而已”的 C++/Win32 哲学世界。 VB.NET and C# are closer, but still differences like this remain. VB.NET 和 C# 更接近,但仍然存在这样的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM