0

Code below is compiled using C# 9 compiler in Visual Studio 2019.

According to Why does VS warn me that typeof(T) is never the provided type in a generic method where the type parameter is restricted to implement T? second answer typeof(TPocoTest) is C# Type type which is never derived from PocoBaseTest type.

C# compiler should thow warning but actually it compiles without warning.

How to force Visual Studio to throw error or warning on this ?

    class PocoBaseTest { }
    class PalkTest : PocoBaseTest { }

    class EntityBaseTest<TPocoTest> where TPocoTest : PocoBaseTest
    {
        void Test()
        {
            // Warning
            // CS0184  The given expression is never of the provided('PocoBaseTest') type
            // does not occur.
            if (typeof(TPocoTest) is PocoBaseTest)
                return;
        }
    }

typeof(TPocoTest) is string throws warning as epxected.

Andrus
  • 26,339
  • 60
  • 204
  • 378
  • [sharplab](https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLTIPaIMYVQcwgDsIFYMFkAfAAQCYBGAWACh6ACAUSJgEsYAngCEoyCABUUMADziAChmwZJyGAD52AdwAWpCO3mLlU9iHYKlIsSpit299gG87D1zQAsBqQAoAlC9d7ZxZA0PYAenD2AHUoBCJeInwAsPtI9gBhAGUABgYADk8DXXZ8XgA3YnYIAA8AByRkZF4MInZeZHYSSoR2DDB2GBKGjHLeABMIce8AcgsMKwkpGd9BgTqIFNT08YwULowYPuxsRAA6LbDeAe9BDf7vQyUbVY7zI0WXy9T7GgB2ADc33YAF8UmCWKwOPNPiZHKCUtCoAAbADWNlM70soiWqicCJYQA=) shows warning here. Check your IDE settings – Pavel Anikhouski Jan 23 '21 at 18:23
  • `typeof(TPocoTest) is string` and other places in code throw this warning. So this warning is enabled in IDE. – Andrus Jan 23 '21 at 18:25
  • Try cleaning bin/obj folders and/or restarting VS. Also what version of framework are you using? If it is .NET Core/.NET 5 try calling `dotnet build`. – Guru Stron Jan 23 '21 at 18:27
  • After cleaning and restarting VS problem persists. I'm using .NET5 and latest VS 2019. `dotnet build` also does not show this warning. – Andrus Jan 23 '21 at 18:41
  • Test it in latest VS2019, .NET5, and everything works fine. Did you filter the warning in `Error list window` and choose `Build+IntelliSense`? – 大陸北方網友 Jan 25 '21 at 08:09

0 Answers0