C#C
C#3y ago
devhl

✅ NotNullWhen not working as expected

This must just be a Roslyn bug...right?
using System.Diagnostics.CodeAnalysis;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string userInput = "foo";

            bool isFormatted = TryFormat(userInput, out string? formattedUserInput);

            if (isFormatted)
                Console.WriteLine(formattedUserInput.ToString()); // 'formattedUserInput' may be null here
        }

        public static bool TryFormat(string userInput, [NotNullWhen(true)] out string? output)
        {
            output = userInput;

            return true;
        }
    }
}
Was this page helpful?