Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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

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.

Edit: To clarify, I'm implementing a custom LINQ provider which is examining the following Where call:

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

or the C# equivalent...

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

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

like image 998
Basic Avatar asked Dec 07 '22 12:12

Basic


1 Answers

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.

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# doesn't have anything like that.

like image 102
Hans Passant Avatar answered Dec 21 '22 09:12

Hans Passant