© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
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;
        }
    }
}
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;
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Test createAsync not working as expected
C#CC# / help
2y ago
❔ If statement isn't working as expected?
C#CC# / help
3y ago
UDP Multicast does not work as expected [Answered]
C#CC# / help
4y ago
ASP.NET building my first website, Discord oauth redirect not working as expected
C#CC# / help
16mo ago