2

I'm currently working on a .NET core 2.1 project using C# 7+. I want to use a Named Tuple as the result of my method. Somehow I cannot manage to correctly create the method return parameters.

So far I have:

public static Tuple<List<Dictionary<string, object>>, List<KeyValuePair<int, Dictionary<string, float>>>> CreateFruits() 
{
   // logic to create apples and oranges

   return new Tuple<List<Dictionary<string, object>>, List<KeyValuePair<int, Dictionary<string, float>>>>(apples, oranges);
}

When I use my method Create Fruits I currently can use Apples and Oranges like that:

var result = MyHelper.CreateFruits();
var apples = result.Item1;
var oranges = result.Item2;

Instead of Item1 and Item2 I would like to use something like Apple and Orange.

var result = MyHelper.CreateFruits();
var apples = result.Apple;
var oranges = result.Orange;

Is it actually possible, I've read something about Named Tuples in C# 7, but I'm not so sure about how to use them correctly, especially with complex datatypes.

Do you know how to solve this issue?

Thanks a lot!

timhorton42
  • 241
  • 5
  • 15
  • 1
    Your code is *not* using the new tuples. It should be `(List> apple, List> orange)`. This won't make this code easier to read and use though - why return suchs lists and dictionaries instead of strongly-typed objects? – Panagiotis Kanavos Dec 17 '18 at 13:35
  • 1
    From Rehman `Please check link. https://learn.microsoft.com/en-us/dotnet/csharp/tuples I think you are missing refrence : (Apples: apple, Orange: orange)` – Panagiotis Kanavos Dec 17 '18 at 13:35
  • 1
    Possible duplicate of https://stackoverflow.com/questions/7745938/better-naming-in-tuple-classes-than-item1-item2 – Kevin Kouketsu Dec 17 '18 at 13:36
  • thanks! The datatypes are used to parse a not strongly typed message. I cannot change unfortunately. – timhorton42 Dec 17 '18 at 13:49
  • 1
    Named tuples won't help. You should refactor return value type. I already feel the pain of developer, who will support this code. I doubt that even **you** will remember, what means `int` in the second `KVP` list after a couple of weeks. – Dennis Dec 17 '18 at 13:53
  • @timhorton42 you *can* change this and return a `dynamic` type. You can use ExpandoObject or inherit from `DynamicObject`. Tuples are meant as a convenience feature for simple data objects and this is definitely not simple – Panagiotis Kanavos Dec 17 '18 at 13:58
  • @timhorton42 dynamic objects are essentially dictionaries with "magic" compiler support. You could think of a `dynamic` instance as a `Dictionary` which would allow `apples` to become `dynamic[] apples` or `List apples`. – Panagiotis Kanavos Dec 17 '18 at 14:00
  • @Dennis, thanks btw. it's me supporting this 10 year old code, I try to make it better... of course it's kind of hard to refactor a class with more than 1000 lines and almost no methods. – timhorton42 Dec 17 '18 at 14:20

1 Answers1

3

C# also supports the following, I think it suits what you are trying to do better

   class TuppleExample
{
    public (Apple, Orange) GetAppleAndOrage()
    {
        var apple = new Apple();
        var orange = new Orange();
        return (apple, orange);
    }

    public void EatAppleAndOrange()
    {
        var (apple, orange) = this.GetAppleAndOrage();

        apple.Bite();
        orange.PeelThenBite();
    }
}
class Apple { }
class Orange { }

The advantage being you skip the .Item1, .Item2 syntax and access your tuple variables directly, just in the order they are declared.

Gerald Chifanzwa
  • 1,277
  • 7
  • 14
  • 1
    That is a ValueTuple, otherwise known as a "named tuple", and it's only available in C#7+ / VS2017+. This is extremely important as not everyone can use this tooling – Camilo Terevinto Dec 17 '18 at 13:49