Necromancer
Necromancer

Reputation: 430

Can ValueTuples be used with .net 4.5.2?

I have an MVC 5 project that targets .net framework 4.52 and I installed the System.ValueTuple through Nuget. I'm using Visual Studio 2017 on Windows 7. During development, Visual Studio doesnt complain about the Tuple return type but when I build I get an array of errors:

error CS1519: Invalid token '(' in class, struct, or interface member declaration
error CS1519: Invalid token ',' in class, struct, or interface member declaration
error CS1519: Invalid token ')' in class, struct, or interface member declaration
error CS1520: Method must have a return type
error CS1026: ) expected
and more..

This is the method that is breaking:

public (bool, string) GetSettings()
{
    if (settingsFile.IsNullOrEmpty())
        throw new Exception("Settings file is null or empty!");
    try
    {
        var definition = new { active = false, excludeList = "" };
        var obj = JsonConvert.DeserializeAnonymousType(settingsFile, definition);
        return (obj.active, obj.excludeList);
    }
    catch
    {
        //TODO: log exception.
        return (false, string.Empty);
    }
}

I don't know how to fix this except to think that it is just not compatible with my framework version. Several extensive online searches have revealed nothing on this.

Upvotes: 3

Views: 3884

Answers (2)

Tech develop
Tech develop

Reputation: 55

I just used Tuple for a .NET452 project. You need to change the method to:

public Tuple<bool, string> GetSettings()

and the return statement to:

return new Tuple<bool, string>(false, string.Empty);

To get the values use:

var resultPair = GetSettings();
bool a = resultPair.Item1;
string b = resultPair.Item2;

Upvotes: 1

Eriawan Kusumawardhono
Eriawan Kusumawardhono

Reputation: 4896

The MSDN blog link mentioned on William Xitaras' comment's on the question already has the quick answer.

Read this paragraph on that blog:

Note that if you don’t see the ValueTuple available in your project, you have to download the System.ValueTuple 4.3.0 NuGet package to your project. You don’t need to do anything if you are using .NET Framework 4.7 or higher, or .NET Standard Library 2.0 or higher.

Therefore You cannot use that System.ValueTuple nuget directly for .NET Framework before .NET Framework v4.7, because you have to meet some requirements first. To ensure that it can work, you have to:

  1. Ensure that your C# project properties has set to use correct version of C# compiler, at least C# 7.0. See screenshot below.
  2. Use System.ValueTuple at least version 4.4.0, because before 4.4.0 is not supported anymore, therefore it's recommended to use at least v4.4.0.

To set the C# compiler version to be used, go to the project properties, select "Build" tab, click Advanced button. A list of C# compiler available will be shown like this screenshot on my VS 2017:

C# project properties

If you change the target to .NET Framework 4.7 or later, then you should not use System.ValueTuple nuget because it is available in the runtime of .NET Framework 4.7.

Upvotes: 2

Related Questions