1

I created the following program

public class Program
{
    static void Main()
    {
        System.Console.WriteLine("Hello, World!");
    }
}

When compiling the code in the command prompt (c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc hello.cs) which returns

Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

The program still compiles so I thought no big deal. Based on other searches I've done I it seemed as if this message is only informational.

However, for the program

public class Program
{
    static void Main()
    {
        System.Console.WriteLine(DateTime.Now.DayOfWeek);
    }
}

the return is

Microsoft (R) Visual C# Compiler version 4.7.3056.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

hello.cs(5,28): error CS0103: The name 'DateTime' does not exist in the current context

which should work fine according to the pluralsight videos I'm following along with. After some searches many of the answers refer to changing the version of C# being used in the project but can't seem to translate that to working in the command prompt. What needs to change?

3
  • I think it is just that the using System; is missing. You can compile it successfully with a fully name like System.DateTime.Now.DayOfWeek. Commented Jun 19, 2018 at 3:14
  • I added using System; and it cleared the error, but I still receive the message about language version support. Still works, but curious as to why I still receive the message. Commented Jun 20, 2018 at 1:13
  • Please check my answer. Commented Jun 20, 2018 at 2:13

3 Answers 3

3

There are several versions of C# compiler (csc.exe) available on the system. So you have

  1. Microsoft Compilers as Part of The Framework, like c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
  2. Microsoft Compilers (Roslyn) as Part of Visual Studio, like C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe

When you compiles with

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc

You are using the C# compiler that shipped with .NET Framework, and this compiler only supports language features up to C# 5.0.

For example, it cannot compile the following program which uses the new C# 6.0 language feature Await in catch/finally blocks.

class Program
{
    static void Main(string[] args)
    {
    }

    async Task<int> GetVAsync()
    {
        try
        {
        }
        catch
        {
            //gives error CS1985: Cannot await in the body of a catch clause
            await Task.Delay(1000); 
        }            
        return 3;
    }
}

So the warning message means,

the compiler you choose to use (the one shipped with .NET Framework) is not capable of compiling new features introduced after C# 5.0. The compilation may succeed if don't use any new feature, just like your example. But you have better use the newer compilers - Roslyn that shipped with Visual Studio - to take advantage of the full power of the compiler.

The solution

Use this in your command line (the path may differs based on your VS edition)

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe hello.cs

Reference

Stackoverflow user Lex Li gave an excellent explanation on the versioning history of the C# compilers in this post.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your answer and your time
1

Check the revised code/comments:

using System; //Add this to your code, it should work well

public class Program
{
    static void Main()
    {
        System.Console.WriteLine("Hello, World!");
        System.Console.WriteLine(DateTime.Now.DayOfWeek);
    }
}

3 Comments

This compiles fine. Thank you for your answer. I still receive the message regarding language version support. Any idea how to address that?
Hey! your post says "Developer Command Prompt in VS2017". But, why are you going specifically to [(c:\Windows\Microsoft.NET\Framework\v4.0.30319] folder to run CSC command? In Windows 10, launch "Developer Command Prompt for VS2017", and run the CSC command and provide the path of your .CS file. The Dev Command Prompt will set necessary environment variable for you.
I'm following the instructions in the pluralsight course I'm using. Thats how he did it.
0

Step 1 - CLR code: The first thing we need to do is to write the CLR code. This could be written in either C#.NET . In this example we are using C#.

[Microsoft.SqlServer.Server.SqlProcedure()]
public static void spSendMail(string recipients, string subject, string fromto, string body)
{

    MailMessage message = new MailMessage(fromto, recipients);
    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
    string msg = string.Empty;
    try
    {
        message.From = new MailAddress(fromto);
        message.To.Add(recipients);
        message.Subject = subject;
        message.IsBodyHtml = true;
        message.Body = body;
        smtpClient.Port = 587;
        smtpClient.Credentials = new System.Net.NetworkCredential(fromto, "779957082");
        smtpClient.EnableSsl = true;
        smtpClient.Send(message);
        msg = "Message ENvoyé !";
        Console.WriteLine(msg);
        //Console.ReadKey();
    }
    catch (Exception ex)
    {
        msg = ex.Message;
        Console.WriteLine(msg);
        //Console.ReadKey();
    }

}

Step 2 - Compile CLR Code In order to use this code, the code has to be compiled.

The following command is run from a command line to compile the CLR code using the csc.exe application So from a command line run the command as follows: cd C:\yourfolder C:\Windows\Microsoft.NET\Framework64\v4.0.30319\vbc /target:library SendEmail.cs After this you have this : The code should now be compiled in a file called: C:\yourfolder\SendEmail.dll

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.