VB -> C#: Converter ignores the three-valued logic for nullable types and relational operators.
See original GitHub issueVB.Net input code
Public Sub Main()
Dim x As Integer? = Nothing
If x <> 10 Then
Console.WriteLine("Fail!")
Else
Console.WriteLine("Ok!")
End If
End Sub
This will print OK because: x <> 10
results in Nothing
with Boolean?
type.
Nothing
is False
so code will execute Else
statement.
Erroneous output
public static void Main()
{
int? x = default;
if (x != 10 == true) {
Console.WriteLine("Fail!");
}
else {
Console.WriteLine("Ok!");
}
}
Here the code will incorrectly print Fail because: x != 10
results in true and true
is equal to true
.
Expected output
public static void Main()
{
int? x = default;
if ((x == null ? null : x != 10) == true) {
Console.WriteLine("Fail!");
}
else {
Console.WriteLine("Ok!");
}
}
VB uses three-valued logic for nullable types. Any relational comparison (=, <>, >, >=, <, <=) that involves nullable types results in Boolean?
.
If lhs
or rhs
is null then the entire expression will be null.
The expected output produces ugly code but I’m not sure if it’s possible to maintain the same logic using something simpler as we need to keep bool?
as the result of the comparison.
I will take a look if there is something in Microsoft.VisualBasic.dll that could simplify the code.
Details
- Product in use: VS extension
- Version in use: https://github.com/icsharpcode/CodeConverter/commit/b5e798bb4baf03d84aeb71e55f000545f5d69938
- Did you see it working in a previous version, which?
- Any other relevant information to the issue, or your interest in contributing a fix.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
An Exhausting List of Differences Between VB.NET & C# | ...
Operator =/<> on strings are not the same (or any relational operators for that matter) 64. Nullable value types use three-valued logic ...
Read more >c# - Three valued logic with nullable bool?
A Nullable can be assigned the values true false, or null. The ability to assign null to numeric and Boolean types is especially...
Read more >Bang Bang (!!) to clean up argument null checks. : r/csharp
Anecdotally, I work with TypeScript pretty often, which has a similar system as C#. Its types are compiler tricks, and not enforced at...
Read more >Conversions - C# language specification
This conversion produces a null reference if the target type is a reference type, or the null value (§8.3.12) of the given nullable...
Read more >VB -> C#: wrong conversion of linq expression containing ...
VB.Net input code Private Shared Sub LinqWithNullable() Dim a = New List(Of Integer?) From {1, 2, 3, Nothing} Dim result = From x...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep it definitely needs a bunch more tests than it has! I think in the general case you’d need to expand to
x.HasValue ? x != 10 : null
for the operator (with some consideration for whether x itself is an expression that shouldn’t be reevaluated). Then within the if statement, add the== true
like now. Obviously for a single operator condition you can reduce that down.For the re-evaluatable expression part, there is some code that can help extract temp variables, but you might be better off trying to use some syntax that introduces a variable in scope along the lines of
x is {} xVal ? xVal != 10 : null
So for this case you’d end up with
x is {} xVal && xVal != 10
I think…which still isn’t exactly prettyNote: Because the decompilation doesn’t show a call to the VB library I assumed (there I go again assuming) that there wasn’t one, but there may well be.
Is the problem here only with the
<>
operator, or other things too?Nullables are value types so VB will box them before calling the methods: Sharplab.io