CLI/C++ named ValueTuple like in C# (.Net Framework 4.7)

403 views Asked by At

Is there any way to achieve the same in CLI/C++ as the following:

namespace Test
{
static class TestClass
{
  static (int a, int b) test = (1, 0);
  static void v()
  {
    var a = test.a;
    var b = test.b;
    _ = (a, b);
  }
}

} So is there any way to create an ValueTuple with other names than Item1 and Item2 in CLI/C++ so it could be used inside C#. I am using .Net Framework 4.7.

1

There are 1 answers

0
Paulo Morgado On BEST ANSWER

The names of the values are not part of ValueTuple<...>.

The names are maintained by the compiler and attributes are added when they need to be communicated to outside code.

Check it out on sharplab.io.

This:

namespace Test
{
    static class TestClass
    {
        static (int a, int b) test = (1, 0);
        static void v()
        {
            var a = test.a;
            var b = test.b;
            _ = (a, b);
        }
    }
}

will be translated into this:

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;

namespace Test
{
    internal static class TestClass
    {
        [TupleElementNames(new string[] {
            "a",
            "b"
        })]
        private static ValueTuple<int, int> test = new ValueTuple<int, int>(1, 0);

        private static void v()
        {
            int item2 = test.Item1;
            int item = test.Item2;
        }
    }
}