Search code examples
c#visual-studionpgsqlroslyn-code-analysiscsharpcodeprovider

LanguageVersion does not contain a definition for C# 9 after clearing NuGet caches


Creating ASP.NET Core 6 MVC application with EF Core and Npgsql in Visual Studio 2022.

After clicking "Clear All NuGet Cache(s)" button in

Visual Studio > Tools Options > NuGet Package Manager > General

enter image description here

property Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9 in source code

CSharpParseOptions.Default.WithLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9);

throws compile error

Error CS0117 'LanguageVersion' does not contain a definition for 'CSharp9'

All NuGet packages are up to date. How to fix this ? Assembly information does not contain CSharp9 member :

#region Assembly Microsoft.CodeAnalysis.CSharp, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// location unknown
// Decompiled with ICSharpCode.Decompiler 6.1.0.5902
#endregion

namespace Microsoft.CodeAnalysis.CSharp
{
    //
    // Summary:
    //     Specifies the language version.
    public enum LanguageVersion
    {
        //
        // Summary:
        //     C# language version 1
        CSharp1 = 1,
        //
        // Summary:
        //     C# language version 2
        CSharp2 = 2,
        //
        // Summary:
        //     C# language version 3
        //
        // Remarks:
        //     Features: LINQ.
        CSharp3 = 3,
        //
        // Summary:
        //     C# language version 4
        //
        // Remarks:
        //     Features: dynamic.
        CSharp4 = 4,
        //
        // Summary:
        //     C# language version 5
        //
        // Remarks:
        //     Features: async, caller info attributes.
        CSharp5 = 5,
        //
        // Summary:
        //     C# language version 6
        //
        // Remarks:
        //     Features:
        //     • Using of a static class
        //     • Exception filters
        //     • Await in catch/finally blocks
        //     • Auto-property initializers
        //     • Expression-bodied methods and properties
        //     • Null-propagating operator ?.
        //     • String interpolation
        //     • nameof operator
        //     • Dictionary initializer
        CSharp6 = 6,
        //
        // Summary:
        //     C# language version 7.0
        //
        // Remarks:
        //     Features:
        //     • Out variables
        //     • Pattern-matching
        //     • Tuples
        //     • Deconstruction
        //     • Discards
        //     • Local functions
        //     • Digit separators
        //     • Ref returns and locals
        //     • Generalized async return types
        //     • More expression-bodied members
        //     • Throw expressions
        CSharp7 = 7,
        //
        // Summary:
        //     C# language version 7.1
        //
        // Remarks:
        //     Features:
        //     • Async Main
        //     • Default literal
        //     • Inferred tuple element names
        //     • Pattern-matching with generics
        CSharp7_1 = 701,
        //
        // Summary:
        //     C# language version 7.2
        //
        // Remarks:
        //     Features:
        //     • Ref readonly
        //     • Ref and readonly structs
        //     • Ref extensions
        //     • Conditional ref operator
        //     • Private protected
        //     • Digit separators after base specifier
        //     • Non-trailing named arguments
        CSharp7_2 = 702,
        //
        // Summary:
        //     C# language version 7.3
        CSharp7_3 = 703,
        //
        // Summary:
        //     C# language version 8.0
        CSharp8 = 800,
        //
        // Summary:
        //     The latest major supported version.
        LatestMajor = 2147483645,
        //
        // Summary:
        //     Preview of the next language version.
        Preview = 2147483646,
        //
        // Summary:
        //     The latest supported version of the language.
        Latest = int.MaxValue,
        //
        // Summary:
        //     The default language version, which is the latest supported version.
        Default = 0
    }
}

Microsoft.CodeAnalysis.CSharp.dll file is not referenced directly from any anyprojet.

NuGet window does not show any package containing roslyn in name.


Solution

  • The error clearly indicates you are using an old version of the DLL that contains the enum, from before C#9 was made available.

    Just upgrade the package in your projects to use the latest one and it should work fine. Here is the latest version of that package:

    If upgrading manually is not possible, check for indirect dependencies on your project dependency tree, and upgrade whatever package is at the top that is referencing the old package. You can do that by navigating through the dependencies in the project's dependencies node in Solution Explorer.

    Another way to find which project exactly is referencing the "problematic" (in this case, old) dll is by doing a windows search by the DLL name in the solution folder, and checking for the difference in file sizes: usually, the different versions will have different sizes, which then allows you to compare them across your projects and with the packages from NuGet.