0

Is there a way to tell the compiler that a following check will ensure that Item is not null and it is safe in the rest of the code and the CS8601 warning is not needed?

The warning, which technically true, does not consider that it's handled in the subsequent line and doesn't need to generate a warning.

Console.WriteLine("Hello, World!");

var test = new Test();
test.Init();

public class Test
{
    public record Item(string Name, int Total);

    public Item MyItem { get; set; } = null!;
    public int MyItemTotal { get; set; }

    public void Init()
    {
        MyItem = GetItems().FirstOrDefault();
        if (MyItem == null)
        {
            return;
        }
        else
        {
            MyItemTotal = MyItem.Total;
        }
    }

    private List<Item> GetItems()
    {
        return new List<Item>();
    }
}

In the above code MyItem = GetItems().FirstOrDefault(); shows as a warning, which is technically correct. However, the following line does a check and exits. I am looking for a way to tell the compiler "I know and I'm handling it, no need to warn."

The main reason is that I don't want to ignore warnings and I don't want pragmas all over the code.

Is there a clean way to do it?

EDIT Here's an example on a Razor webpage where if MyItem is null we would redirect away from the page. It is safe to assume in this code that MyItem will never be null. But the warning will always be displayed because Item is not nullable.

public Item MyItem { get; set; } = null!;

public IActionresult OnGet()
{
    MyItem = GetItems().FirstOrDefault();
    if (MyItem == null)
    {
        return Redirect("/");
    }
        
    return Page();
}

13
  • 4
    It would help if you could provide a minimal reproducible example instead of pseudocode - you appear to use Item as both the name of a type and the name of a variable. (I'd also strongly encourage you to follow normal. NET naming conventions, e.g. starting local variables with lower case letters, even for sample code. Anything that distracts the reader harms the post.) Commented Jun 13, 2024 at 17:21
  • Here's a working console app that I hope shows the issue and what I am trying to do. Commented Jun 13, 2024 at 21:00
  • Does this answer your question? What does null! statement mean? Commented Jun 13, 2024 at 21:37
  • 1
    The value of MyItem can be changed by a different thread between assigning the value and checking the value. If want to signal the compiler the guarantee that it won't be null, don't allow anybody to change it behind your back – use a local variable, those can't be changed Commented Jun 17, 2024 at 17:10
  • 1
    Since the Razor page has the knowledge of when the property "should" be non-null, the Razor page should override the nullability with ! as needed. Commented Jun 26, 2024 at 16:39

1 Answer 1

0

I'm answering myself since no one posted an answer to the question.

Before doing the below, read through the comments. There's some good info in there around the concept of nullability, testing for it, using it, etc.

In this case, there's one entry point to a Razor page and it checks for nulls and then redirects away if the property is null. The IDE does not consider this, and thus will forever warn you about a potential null error. 100s of these in a project, IMHO, reduce the efficacy of the warnings as you'll just ignore them all. To remove the warning, you just bang it at the end, which removes the warning.

MyItem = GetItems().FirstOrDefault()!;
if (MyItem == null)
{
   return Redirect("/");
}

What's good about this instead of in the property, for example, is that if someone else were to add a different entry point, they will get the warning and would have to determine the appropriate action.

I'm familiar with banging things (🤔) to address null warnings and nullability. I think I did it initially but got an error and assumed it wouldn't work...not realizing that since it was an async method I needed to wrap it in parenthesis to correctly do what I wanted...duh.

MyItem = (await GetItemAsync())!;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.