Nullable warning isn't emitted when passing default to a struct parameter
See original GitHub issueRepro:
using System;
#nullable enable
public class Person
{
public string Name { get; }
public Person(string name) =>
Name = name ?? throw new ArgumentNullException(nameof(name));
}
public struct MyStruct
{
public Person person;
}
public static class Program
{
public static void Foo(MyStruct s)
{
Console.WriteLine(s.person.Name);
}
public static void Main() => Foo(default); // Expected warning here. Because MyStruct contains a non-nullable reference type.
}
The previous example throws NullReferenceException.
I think any struct containing a non-nullable reference type should have a warning when it takes the value “default”.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Wrong compiler warning when comparing struct to null
I discovered this error independently while implementing lifted operator behaviour in Roslyn, and I fixed it in Roslyn before I left.
Read more >Nullable reference types
A reference isn't supposed to be null. The default state of a nonnullable reference variable is not-null. The compiler enforces rules that ...
Read more >C# Nullable - Usage with Generics #3060
So in a project or code block that enables the C# nullable feature, ... warning: passing a nullable value to a non-nullable parameter....
Read more >Digging Into Nullable Reference Types in C#
By enabling Nullable Reference Types, all reference types (by default) do not support Null unless you use the define them with the ...
Read more >New features - Manual
Attempting to use a void function's return value simply evaluates to null , with no warnings emitted. The reason for this is because...
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 Free
Top 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
https://github.com/dotnet/csharplang/issues/99#issuecomment-601792573
That’s clear enough. Let me close.