VB -> C#: CType(Nothing, <Nullable>?) returns default instead of null
See original GitHub issueVB.Net input code
Imports System
Public Class VisualBasicClass
Dim SomeDate As String = "2022-01-01"
Dim SomeDateDateParsed As Date? = If(String.IsNullOrEmpty(SomeDate), CType(Nothing, Date?), DateTime.Parse(SomeDate))
Dim SomeDateDateNothing As Date? = If(String.IsNullOrEmpty(SomeDate), Nothing, DateTime.Parse(SomeDate))
End Class
Erroneous output
using System;
public partial class VisualBasicClass
{
private string SomeDate = "2022-01-01";
private DateTime? SomeDateDateParsed;
private DateTime? SomeDateDateNothing;
public VisualBasicClass()
{
SomeDateDateParsed = string.IsNullOrEmpty(SomeDate) ? default : DateTime.Parse(SomeDate);
SomeDateDateNothing = string.IsNullOrEmpty(SomeDate) ? default : DateTime.Parse(SomeDate);
}
}
Expected output
using System;
public partial class VisualBasicClass
{
private string SomeDate = "2022-01-01";
private DateTime? SomeDateDateParsed;
private DateTime? SomeDateDateNothing;
public VisualBasicClass()
{
SomeDateDateParsed = string.IsNullOrEmpty(SomeDate) ? null: DateTime.Parse(SomeDate);
SomeDateDateNothing = string.IsNullOrEmpty(SomeDate) ? default : DateTime.Parse(SomeDate);
}
}
Details
- Product in use: both
- Version in use: e.g. 9.1.3.577
- Did you see it working in a previous version, which? No
- Any other relevant information to the issue, or your interest in contributing a fix. I know it’s tricky in VB with Nothing, but I think in this scenario it should be null, not default. Please check the fiddle: https://dotnetfiddle.net/PwEL1D
Issue Analytics
- State:
- Created 6 months ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Why is my Nullable(Of Int32) = 0 after I set it to Nothing?
In VB.NET Nothing does not only mean null but also default . So you are assigning the default value of Int32 to the...
Read more >Nothing keyword - Visual Basic
Represents the default value of any data type. For reference types, the default value is the null reference. For value types, the default...
Read more >Nullable Value Types - Visual Basic
When you declare a variable with a nullable value type, its HasValue property has a default value of False . This means that...
Read more >possible VB.NET compiler bug using Null-conditional ...
The reason is because the null-conditional operator isn't returning False , it's returning Nothing . VB.NET does not treat Nothing as False ...
Read more >VB.NET Nothing, IsNothing (Null)
First example. This program assigns a String variable to Nothing. This is not an empty string. It is a String object that does...
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
9.2.1 released
Cool, any chance to update the command line tool? We have it in the pipeline.