Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
Why Does .NET 4.6 Specific Code Compile When Targeting Older Versions of the Framework?

I have a project that targets older versions of the .NET framework (.NET 4.5.2). I installed Visual Studio 2015 (and therefore .NET 4.6 on my machine). I noticed that if I use C# language features released in .NET 4.6/C# 6, it still compiles. If my project's target framework is < .NET 4.6, shouldn't this not compile:

    public string MyExpressionBodyProperty => "1";

    //auto properties are new in C# 6
    public string MyAutoProperty { get; } = "1";

    private static void MethodThatUsesNameOf(string filename)
    {
        if (filename == null) 
        {
            //nameof was released in C# 6
            throw new ArgumentException("The file does not exist.", nameof(filename));
        }
    }

How can I ensure I'm only using .NET language features that work with the framework version I'm targeting?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The .NET Framework version and the C# language version are different things. The C# 6 language features

public string MyAutoProperty { get; } = "1";
nameof(filename)

can be compiled by VS 2015 into code that targets earlier frameworks.

How can I ensure I'm only using .NET language features that work with the framework version I'm targeting?

If you try to use a .NET 4.6 Framework feature, you will get an appropriate compiler error if you are targeting an earlier framework.

Would I be able to deploy this code to machine which did not have .NET 4.6?

Yes, you would. As long as the deployment machine has a framework compatible with the one you target in VS2015.

over 4 years ago · Santiago Trujillo Relatório

0

There are C# 6 features and .NET 4.6 features.

nameof is a C# 6 feature so only needs to be running on a newer compiler but can still use the older .NET framework.

An example of a feature that requires .NET 4.6 is the following:

public void Foo(IReadOnlyCollection<string> input)
{
}

public void Main(string[] args)
{
    Foo(new Stack<string>());
}

In .NET 4.6 Stack implements IReadOnlyCollection but in previous versions it does not. The above code would fail to compile if you selected .NET 4.5.2 as your target platform.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda