-1

Writing the following code in visual studio 2022

public class MyClass { }

public bool TrySomething (out MyClass myClass)
{
    myClass = null;

    return false;
}

Makes it complain about "possible non-nullable" enter image description here

But it's a class parameter, how could this be a non-nullable? what am I missing?

NOTE: rider doesn't complain about with, now I want to know if the rider is correct or if I had suppressed some warning kind and don't remember

enter image description here


EDIT: added the project file

    <Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWindowsForms>true</UseWindowsForms>
        <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>

      <ItemGroup>
        <PackageReference Include="OpenTK" Version="4.7.4" />
        <!--<PackageReference Include="OpenTK.WinForms" Version="4.0.0-pre.6" />-->
      </ItemGroup>

      <ItemGroup>
        <Reference Include="OpenTK.WinForms">
          <HintPath>dlls\OpenTK.WinForms.dll</HintPath>
        </Reference>
      </ItemGroup>

      <ItemGroup>
        <Compile Update="Properties\Resources.Designer.cs">
          <DesignTime>True</DesignTime>
          <AutoGen>True</AutoGen>
          <DependentUpon>Resources.resx</DependentUpon>
        </Compile>
      </ItemGroup>

      <ItemGroup>
        <EmbeddedResource Update="Properties\Resources.resx">
          <Generator>ResXFileCodeGenerator</Generator>
          <LastGenOutput>Resources.Designer.cs</LastGenOutput>
        </EmbeddedResource>
      </ItemGroup>

    </Project>
Nefisto
  • 517
  • 3
  • 9
  • 3
    *"how could this be a non-nullable? what am I missing?"* - See: [Nullable reference types](https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references) – madreflection Feb 28 '23 at 17:53
  • 1
    Please show your project file - various things in that will affect this. – Jon Skeet Feb 28 '23 at 17:54
  • "But it's a class parameter" no, it's **the class itself**. it is also an `out` parameter of a function, which means the `null` value is being returned to the function caller, but the function has no way of knowing if the caller can handle null references, and the caller should have no reason to expect it. – Claies Feb 28 '23 at 17:55
  • 1
    (I'd expect a recent version of Rider to have full support for nullable reference types as well, by the way - i.e. if you're in a context where NRTs are enabled, I'd expect Rider to show a warning too.) – Jon Skeet Feb 28 '23 at 17:57

1 Answers1

1

Your code will compile and run without errors. But I suggest you learn to work with Nullable reference types.

The most common C# error is the null-reference exception. Null reference types help you catch these. They do not modify the resulting program. They serve no purpose except to help you recognize potential bugs.

In your case, the caller is expecting the method to return an instance of MyClass. If you returned null, the caller might fail to detect that and you'd have an error.

If you want null to be a valid option, then declare the parameter as MyClass?. Now, your method will compile without warnings and the caller will get a different warning if they attempt to use the returned value without first checking for null.

If you decide you don't want the advantages of nullable reference types, you can disable it. Or you can add the following at the top of your source file:

#nullable disable
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • As I using it in a try method, I would just reference this class when the method gimmes permission, but I got ur point about null exception, I'll read more about it, but have removed it from project settings, at least for now, thanks – Nefisto Feb 28 '23 at 18:05
  • @candied_orange: Huh? First off, I didn't attach words to anything. But whatever you call it, it helps bring potential errors to your attention. And not helps quietly ignore problematic nulls as you suggested. – Jonathan Wood Feb 28 '23 at 20:58
  • 1
    @candied_orange: No, that's not how it works. If you declare it as nullable then you'll get a warning any time you try to use it without first checking for null. Again, the entire purpose is to raise to your attention situations that could lead to a null-reference exception. – Jonathan Wood Feb 28 '23 at 21:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252209/discussion-between-jonathan-wood-and-candied-orange). – Jonathan Wood Feb 28 '23 at 21:13