none
List of ValueTuple reverts to unnamed Tuple RRS feed

  • Question

  • So I have a list of value tuples,

    List<(object dataArray, MyClass metadata)>

    I've tried different methods to create the value tuple to see if that made a difference, but no matter, when I add it to the list and reference it there it has become a regular unnamed tuple with the default Item1, Item2 names.

    Which is odd, because on the same object intellisense shows the original names that I assigned, although they are listed as null.  The value of each is still accessible, but I have to jump through a hoop like so,

    listitem.ToTuple<object, MyClass>().Item2.propertyname
    Somewhat defeating the purpose of having a value tuple in the first place.  So I'm just wondering if there is some initialzation I'm missing for the list?

    Thursday, April 11, 2019 8:14 PM

All replies

  • Have you tried the ValueTuple

    https://docs.microsoft.com/en-us/dotnet/csharp/tuples


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 11, 2019 9:41 PM
    Moderator
  • Why yes, I have.

    "So I have a list of value tuples,"

    Thursday, April 11, 2019 9:51 PM
  • Have you tried a new project in Visual Studio 2019? IntelliSence, expressions like ‘var listitem = mylist[0]; var d = listitem.dataArray’, Debugger tooltips seem to work.

    Friday, April 12, 2019 5:53 AM
  • A simple example that demonstrates the behavior.  Examine newTuple, then examine it inside the list after it's added.  You'll see that referencing it inside the list it now shows the default names.

    class Program
        {
            static void Main(string[] args)
            {
    
                List<(object dataArray, MyClass metadata)> test = new List<(object dataArray, MyClass metadata)>();
                (object dataArray, MyClass metadata) newTuple = (new string[] { "Hi there", "How are you"}, new MyClass());
    
                test.Add(newTuple);
            }
    
    
        }
    
    
        public class MyClass
        {
            public string MyValue { get; set; }
        }
    }


    • Edited by Marbry Friday, April 12, 2019 1:31 PM
    Friday, April 12, 2019 1:26 PM
  • Have you tried a new project in Visual Studio 2019? IntelliSence, expressions like ‘var listitem = mylist[0]; var d = listitem.dataArray’, Debugger tooltips seem to work.

    I don't have VS 2019 installed, this need to work in VS 2017 as that is what we're using.
    Friday, April 12, 2019 1:53 PM
  • And perhaps even more strangely, taking "test" and doing

    var arrayResult = test.ToArray();

    And the proper names are magically re-assigned.

    Friday, April 12, 2019 2:06 PM
  • Hi

    >>And perhaps even more strangely, taking "test" and doing

    >>And the proper names are magically re-assigned.

    I could not understand the two sentences, could you explain it more clearly?

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 17, 2019 3:17 AM
    Moderator
  • Whereas test as list of value tuples does not reflect the names assigned to the original value tuple on each item, just casting test to array will without any other changes.

    So, value tuple with specific names created

    Value tuple goes into List<>, no original value tuple names, just default names

    Convert same List<> to array, now the original names are available again

    Thursday, April 25, 2019 6:55 PM