Home > Net >  C# Visual Studio 2019 and 2022 code analyse issues
C# Visual Studio 2019 and 2022 code analyse issues

Time:06-16

I have simple Console app with target framework .NETCore 3.1 opened in Visual Studio 2019 and 2022 on macOS (in Windows same problem).

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var user = new User
            {
                Surname = "Doe",
                Name = "John"
            };

            var s = user?.TryGetFullName(out string userFullName) == true ? userFullName : null;

            Console.WriteLine(s);
        }

        public class User
        {
            public string Surname { get; set; }
            public string Name { get; set; }

            public string GetFullName()
            {
                return string.Join(" ", Surname.Trim(), Name.Trim());
            }

            public bool TryGetFullName(out string userFullName)
            {
                userFullName = null;

                try
                {
                    userFullName = GetFullName();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}

Code building on 2019 and 2022 is succesful, but in 2019 a have error on compile: Use of unassigned local variable.

Screenshots:

How can I enable same error in 2022? Why it's not showing?

CodePudding user response:

In visual studio 2022 the default language version is C#10. As stated here in the documentation in prior versions there were false warnings for certain scenarios.

The code building is successful in both case because these are false positive warnings.

With C# 10 the definite assignements have been improved.

  • Related