簡體   English   中英

沒有Visual Studio的C#6.0

[英]C# 6.0 without Visual Studio

如何在沒有Visual Studio的情況下使用C#6.0功能?

以下C#6.0代碼不起作用:

using static System.Console;

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

這是錯誤:

>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

這是用於確定安裝了哪些.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();
    }
}

以下是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

請注意,這個問題是關於沒有任何Visual Studio / IDE的 C#6.0。

C#6不是針對特定的框架版本構建的,它完全取決於編譯器。 如果使用C#6編譯器,則可以編譯該代碼段。

確保在構建計算機上安裝了最新的MSBuild版本(他們從框架文件夾中移動了編譯器)。 該安裝包含最新的編譯器。 (如果指定項目為較低的框架版本構建,則客戶端不需要運行最新的框架版本)

您還可以使用NuGet命令行安裝編譯器NuGet包。

有用的資源:

你的問題在於編譯器

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

當您需要6編譯器時,您正在使用4.6編譯器。

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-框架-46-IF-這樣定位?論壇= csharpgeneral

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? 這個東西與.NET Framework 4.6 Targeting包的關系是什么? 會安裝嗎? 或者都需要? 就像我說的,我可以使用VS2013中的C#6語言功能進行編譯嗎?

嘗試使用以下位置的編譯器:

%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