简体   繁体   English

没有Visual Studio的C#6.0

[英]C# 6.0 without Visual Studio

How to use C# 6.0 features without Visual Studio? 如何在没有Visual Studio的情况下使用C#6.0功能?

The following C# 6.0 Code does not work: 以下C#6.0代码不起作用:

using static System.Console;

class Program 
{ 
    static void Main() 
    { 
        WriteLine("Hello world!"); 
    } 
}

Here's the Error: 这是错误:

>csc CS6.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

CS6.cs(1,7): error CS1041: Identifier expected; 'static' is a keyword
CS6.cs(1,14): error CS1518: Expected class, delegate, enum, interface, or struct

Here's the C# Code to determine which .NET Framework versions are installed : 这是用于确定安装了哪些.NET Framework版本的C#代码:

using System;
using Microsoft.Win32;

class RegistryVersion
{   
    private static void GetVersionFromRegistry()
    {
         // Opens the registry key for the .NET Framework entry.
            using (RegistryKey ndpKey = 
                RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                // As an alternative, if you know the computers you will query are running .NET Framework 4.5 
                // or later, you can use:
                // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
                // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            foreach (string versionKeyName in ndpKey.GetSubKeyNames())
            {
                if (versionKeyName.StartsWith("v"))
                {

                    RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                    string name = (string)versionKey.GetValue("Version", "");
                    string sp = versionKey.GetValue("SP", "").ToString();
                    string install = versionKey.GetValue("Install", "").ToString();
                    if (install == "") //no install info, must be later.
                        Console.WriteLine(versionKeyName + "  " + name);
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                            Console.WriteLine(versionKeyName + "  " + name + "  SP" + sp);
                        }

                    }
                    if (name != "")
                    {
                        continue;
                    }
                    foreach (string subKeyName in versionKey.GetSubKeyNames())
                    {
                        RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                        name = (string)subKey.GetValue("Version", "");
                        if (name != "")
                            sp = subKey.GetValue("SP", "").ToString();
                        install = subKey.GetValue("Install", "").ToString();
                        if (install == "") //no install info, must be later.
                            Console.WriteLine(versionKeyName + "  " + name);
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                Console.WriteLine("  " + subKeyName + "  " + name + "  SP" + sp);
                            }
                            else if (install == "1")
                            {
                                Console.WriteLine("  " + subKeyName + "  " + name);
                            }
                        }
                    }
                }
            }
        }
    } 

    static void Main()
    {
        GetVersionFromRegistry();
    }
}

Here's the output for the C# Code to determine which .NET Framework versions are installed: 以下是C#代码的输出,用于确定安装了哪些.NET Framework版本:

>csc FrameworkVersion.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.

>FrameworkVersion.exe
v2.0.50727  2.0.50727.4927  SP2
v3.0  3.0.30729.4926  SP2
v3.5  3.5.30729.4926  SP1
v4
  Client  4.6.01055
  Full  4.6.01055
v4.0
  Client  4.0.0.0

Please note that this question is about C# 6.0 without any Visual Studio/IDE . 请注意,这个问题是关于没有任何Visual Studio / IDE的 C#6.0。

C# 6 is not build towards a specific framework version, it all depends on the compiler. C#6不是针对特定的框架版本构建的,它完全取决于编译器。 If you use the C# 6 compiler, you can compile that snippet. 如果使用C#6编译器,则可以编译该代码段。

Make sure you have installed the latest MSBuild version on your build machine (they moved the compiler from the framework folder). 确保在构建计算机上安装了最新的MSBuild版本(他们从框架文件夹中移动了编译器)。 That installation contains the latest compiler. 该安装包含最新的编译器。 (Clients don't need to run the latest framework version if your project is specified to build for a lower framework version) (如果指定项目为较低的框架版本构建,则客户端不需要运行最新的框架版本)

You can also install the compiler NuGet package using the NuGet command line. 您还可以使用NuGet命令行安装编译器NuGet包。

Useful resources: 有用的资源:

Your problem is with the compiler 你的问题在于编译器

>csc CS6.cs
Microsoft (R) Visual C# Compiler version -->4.6.1055.0<--
for Microsoft (R) .NET Framework 4.5

You are using the 4.6 compiler when you need the 6 compiler. 当您需要6编译器时,您正在使用4.6编译器。

C# 6 compiler is installed with Visual Studio, however looks like someone managed to install it regardless of IDE C#6编译器随Visual Studio一起安装,但看起来有人设法安装它而不管IDE

https://social.msdn.microsoft.com/Forums/vstudio/en-US/9959d4c1-16fe-45bc-9535-7b0775d26e9a/please-confirm-c-60-compiler-is-not-installed-with-net-framework-46-if-so-location?forum=csharpgeneral https://social.msdn.microsoft.com/Forums/vstudio/en-US/9959d4c1-16fe-45bc-9535-7b0775d26e9a/please-confirm-c-60-compiler-is-not-installed-with-net-框架-46-IF-这样定位?论坛= csharpgeneral

http://www.microsoft.com/en-us/download/details.aspx?id=48159 Microsoft Build Tools 2015 It says it only depends on .NET Framework 4.5... I guess the Roslyn-based C# 6.0 compiler can target .NET Framework 4.5? http://www.microsoft.com/en-us/download/details.aspx?id=48159 Microsoft Build Tools 2015它说它只依赖于.NET Framework 4.5 ...我想基于Roslyn的C#6.0编译器可以目标.NET Framework 4.5? What is this thing's relationship to the .NET Framework 4.6 Targeting pack? 这个东西与.NET Framework 4.6 Targeting包的关系是什么? Will that install this? 会安装吗? Or are both needed? 或者都需要? And like I said, can I compile using C# 6 language features in VS2013? 就像我说的,我可以使用VS2013中的C#6语言功能进行编译吗?

Try to use compilers from these locations: 尝试使用以下位置的编译器:

%ProgramFiles(x86)%\MSBuild\12.0\Bin\
%ProgramFiles(x86)%\MSBuild\12.0\Bin\amd64\
%ProgramFiles(x86)%\MSBuild\14.0\Bin\
%ProgramFiles(x86)%\MSBuild\14.0\Bin\amd64\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM