How to enable c# 8?

I just updated to 2020.2.0a12 via Hub, patch notes say compiling scripts with c# 8 is now available but my project is still on c# 7.3

1 Like

Same. I was waiting for this feature, but the default projects get set to C# 7.3 still :frowning:

In the Visual Studio Editor package, ProjectGeneration.cs file you find the following:

            var targetLanguageVersion = "latest"; // danger: latest is not the same absolute value depending on the VS version.

            if (m_CurrentInstallation != null && m_CurrentInstallation.SupportsCSharp8)
            {
                // Current installation is compatible with C# 8.
                // But Unity has no support for C# 8 constructs so far, so tell the compiler to accept only C# 7.3 or lower.
                targetLanguageVersion = "7.3";
            }

My guess is they need to update this package and remove that if statement. I tried a few C# 8 language features and they seem to work. You can manually edit the autogenerated .csproj and change the LangVersion value to ‘latest’ to not get errors in Visual Studio, but it will be overwritten at some point.

EDIT:
A solution for now is to install the previous version 2.0.0 of the Visual Studio Editor package.
That if statement was added only on the latest 2.0.1 version.
https://docs.unity3d.com/Packages/com.unity.ide.visualstudio@2.0/changelog/CHANGELOG.html

3 Likes

Another option if you want to stick with the 2.01 version is to cut/paste the package from /Library/PackageCache/ to /Packages/ and comment out those lines manually.

And if you’re brave, create a csc.rsp text file in the root of your project and add the line -nullable:enable

It looks like <Nullable>enable</Nullable> is ignored by Visual Studio on old style .csproj files. The only way to get the warnings is to add #nullable enable to each .cs file.
Would be nice if the VS editor package updated to the new csproj style.

Ah, of course it appears that they didn’t do basic testing.

It would also be nice if the generated solution was placed somewhere in the Library folder so it doesn’t clutter the project root. Also, NuGet support in the Package Manager, but that’s another story.

1 Like

In alpha releases I guess we’re the basic testing.

4 Likes

Not library, but definitely not root. I use docfx to generate my documentation which needs to know where the csprojs are generated, so as long as they’re in one consistent self contained location I’m happy. Definitely don’t like how cluttered root gets once you start using asmdefs and external plugins a lot

1 Like

This!

Made by a Unity employee @xoofx

https://github.com/xoofx/UnityNuGet

Hold up. The .NET Core prototype, now this? Both were made by him, and neither got picked up officially.

EDIT: Oh, spoke too soon. It’s a limited selection, not a proper integration.

Why not Library? Definitely not the root of Library either — something like Library\GeneratedSolution.

The reason is because not all packages target .NET Standard 2.0 or work across all platforms.

But changing that json you could easily support any package supporting .NET Standard 2.0 really easy.

Unfortunately, it’s like this


In fact, it’s even worse, Unity reinventing the wheel and making a mess of it:

https://docs.unity3d.com/Packages/com.unity.nuget.newtonsoft-json@2.0/manual/index.html

Can’t you just check the dependency list? They have .NETStandard,Version=v2.0 there. And it should be the user’s responsibility to check whether a package supports all platforms that they’re targeting.
NuGet mirror packages
 yeah. Goddammit.

EDIT: Uh, yeah, I see I didn’t quite read the FAQ.

That could work, disregard my straight “no” for library, it just needs to be self contained and reasonable to find. Since package cache gets its own top level folder it feels like a top level solution folder would be fine

1 Like

I tried editing the visual studio package to change the location of the solution and project files. It seems to require simple changes to only 3 methods. I only tested it briefly but seems to be working.

        public string ProjectFile(Assembly assembly)
        {
            return Path.Combine(ProjectDirectory, "Library", "VSSolution", $"{m_AssemblyNameProvider.GetAssemblyName(assembly.outputPath, assembly.name)}.csproj");
         // Change Library and VSSolution to any other path.
        }

        public string SolutionFile()
        {
            return Path.Combine(FileUtility.Normalize(ProjectDirectory), "Library", "VSSolution", $"{InvalidCharactersRegexPattern.Replace(m_ProjectName, "_")}.sln");
        // Same as above.
        }

        string EscapedRelativePathFor(string file)
        {
            var projectDir = FileUtility.Normalize(ProjectDirectory);
            file = FileUtility.Normalize(file);
            var path = SkipPathPrefix(file, projectDir);

            if (!Path.IsPathRooted(path)) // you need to add this
                path = Path.Combine("..\\..\\", path); // you need to add this
...

There’s also a WriteAllText method where you could make it check/add the new folder in case it doesn’t exist.

1 Like

I filed a bug for this in the Tools for Unity and we’ll investigate the fix. For future, let us know via the Help > Report a Problem menu in Visual Studio too.

1 Like

How do I get C#8 happening with Rider?

1 Like

Just seems to work for me.

Without needing to do anything?
Tried inserting this in my code but it won’t let me


foreach(var item in 1..5)
            {
                Debug.Log(item);
            }

Doesn’t recognise 1
5 as a range.