C#和VB.Net字符串比较之间的区别 [英] Difference between C# and VB.Net string comparison

查看:134
本文介绍了C#和VB.Net字符串比较之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在回答这个问题,但由于涉及的程度很大,而且不太可能很快获得良好的响应,因此我将尝试自己制定实施方案。根本的问题似乎是我遵循的C#示例没有直接转换为VB。

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.

在检查字符串比较 BinaryExpression 在Lambda中,VB报告 Expression.Method.DeclaringType Microsoft.VisualBasic.CompilerServices.Operators ,方法名称为 CompareString

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. This is clearly VB-specific.

表达式只是比较 x.Content_Type<> 并在其上调用ToString返回 {(CompareString(x.Content_Type,,False)!= 0)} -看起来很漂亮逻辑(CompareString 此处的文档

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).

有人可以向我解释VB和C#如何不同地处理字符串比较。

Can someone explain to me how (or even better, why) VB and C# handle string comparisons differently.

我认为,如果我能找到答案,我应该能够解决其他问题。

I think if I can get an answer to that, I should be able to work out a solution to the other problem.

编辑:
为了澄清起见,我正在实现一个自定义LINQ提供程序,它正在检查以下 Where 调用:

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

或相当于C#...

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

据我所知,这2个功能应该相同

As far as I'm aware, the 2 should be functionally identical

推荐答案

VB.NET继承了Visual Basic早期版本中的 Option Compare 语句。为了使该功能生效,VB.NET中的所有字符串比较表达式都转换为一个辅助函数,该函数可以找出在编写该语句的特定源代码文件中所选的选项比较值是什么。

VB.NET inherited the Option Compare statement from previous versions of Visual Basic. 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.

Operators.CompareString(string,string,bool)方法是该辅助函数。最后一个参数名为 TextCompare,如果Option Compare Text有效,则VB.NET编译器自动传递True,如果Option Compare Binary有效,则False。

The Operators.CompareString(string, string, bool) method is that helper function. 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.

C#没有这样的东西。

C# doesn't have anything like that.

这篇关于C#和VB.Net字符串比较之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆