Don't refactor to nullable "type?"

Sorry for the confusing title. Let me show some code instead, if I have this code
var a = new Timer(null);
and uses Refactor this on var, I get this
Timer? a = new Timer(null);

Is it possible to make Resharper turn var into Timer without the nullable reference ?

0
8 comments

The funny thing is that after converting var to Timer?, Resharper immediately suggests that I might want to remove the ? as it's not needed. 

 

0

var for reference types is always assumed to be nullable, since it could be assigned a null value later in the code:
LDM-2019-12-18

The R# behaviour is correct as per the C# specification.

0

Perhaps you can make it a bit smarter, when you know the nullable ? isn't needed

0

Another observation, when I use the "Introduce field", the field type is non-nullable, shouldn't this then be nullable to be in agreement with converting from var behaviour?

0

Hello Karsten Kousgaard

Thank you for contacting us! We were unable to reproduce the initial request. Does it still behave this way using ReSharper version 2021.3.4 or 2022.1 EAP?

0

Hi. Just updated, it's still the same behavior. I'm targeting .Net 4.8 with these settings in the csproj (new sdk-format)

    <LangVersion>latest</LangVersion>    
    <Nullable>enable</Nullable>

Perhaps that's an unsupported setup, as I understand it's not official supported by Microsoft.

0

Sure, it looks like .NET Framework doesn't support C# 8.0 https://devblogs.microsoft.com/dotnet/building-c-8-0/

0

True, it compiles and works fine though

0

Please sign in to leave a comment.