What requirement was the tuple designed to solve?

2020-01-24 02:11发布

I'm looking at the new C# feature of tuples. I'm curious, what problem was the tuple designed to solve?

What have you used tuples for in your apps?

Update

Thanks for the answers thus far, let me see if I have things straight in my mind. A good example of a tuple has been pointed out as coordinates. Does this look right?

var coords = Tuple.Create(geoLat,geoLong);

Then use the tuple like so:

var myLatlng = new google.maps.LatLng("+ coords.Item1 + ", "+ coords.Item2 + ");

Is that correct?

标签: c# tuples
13条回答
乱世女痞
2楼-- · 2020-01-24 02:41

I stumbled upon this performance benchmark between Tuples and Key-Value pairs and probably you will find it interesting. In summary it says that Tuple has advantage because it is a class, therefore it is stored in the heap and not in the stack and when passed around as argument its pointer is the only thing that is going. But KeyValuePair is a structs so it is faster to allocate but it is slower when used.

http://www.dotnetperls.com/tuple-keyvaluepair

查看更多
甜甜的少女心
3楼-- · 2020-01-24 02:44

Old question since 2010, and now in 2017 Dotnet changes and become more smart.

C# 7 introduces language support for tuples, which enables semantic names for the fields of a tuple using new, more efficient tuple types.

In vs 2017 and .Net 4.7 (or installing nuget package System.ValueTuple), you can create/use a tuple in a very efficient and simple way:

     var person = (Id:"123", Name:"john"); //create tuble with two items
     Console.WriteLine($"{person.Id} name:{person.Name}") //access its fields

Returning more than one value from a method:

    public (double sum, double average) ComputeSumAndAverage(List<double> list)
    {
       var sum= list.Sum();
        var average = sum/list.Count;
        return (sum, average);
    }

    How to use:

        var list=new List<double>{1,2,3};
        var result = ComputeSumAndAverage(list);
        Console.WriteLine($"Sum={result.sum} Average={result.average}");    

For more details read: https://docs.microsoft.com/en-us/dotnet/csharp/tuples

查看更多
成全新的幸福
4楼-- · 2020-01-24 02:48

It provides an alternative to ref or out if you have a method that needs to return multiple new objects as part of its response.

It also allows you to use a built-in type as a return type if all you need to do is mash-up two or three existing types, and you don't want to have to add a class/struct just for this combination. (Ever wish a function could return an anonymous type? This is a partial answer to that situation.)

查看更多
The star\"
5楼-- · 2020-01-24 02:53

A common use might be to avoid creating classes/structs that only contains 2 fields, instead you create a Tuple (or a KeyValuePair for now). Usefull as a return value, avoid passing N out params...

查看更多
霸刀☆藐视天下
6楼-- · 2020-01-24 02:55

very useful for returning two values from a function

查看更多
淡お忘
7楼-- · 2020-01-24 02:56

Personally, I find Tuples to be an iterative part of development when you're in an investigative cycle, or just "playing". Because a Tuple is generic, I tend to think of it when working with generic parameters - especially when wanting to develop a generic piece of code, and I'm starting at the code end, instead of asking myself "how would I like this call to look?".

Quite often I realise that the collection that the Tuple forms become part of a list, and staring at List> doesn't really express the intention of the list, or how it works. I often "live" with it, but find myself wanting to manipulate the list, and change a value - at which point, I don't necessarily want to create a new Tuple for that, thus I need to create my own class or struct to hold it, so I can add manipulation code.

Of course, there's always extension methods - but quite often you don't want to extend that extra code to generic implementations.

There have been times I'm wanted to express data as a Tuple, and not had Tuples available. (VS2008) in which case I've just created my own Tuple class - and I don't make it thread safe (immutable).

So I guess I'm of the opinion that Tuples are lazy programming at the expense of losing a type name that describes it's purpose. The other expense is that you have to declare the signature of the Tuple whereever it's used as a parameter. After a number of methods that begin to look bloated, you may feel as I do, that it is worth making a class, as it cleans up the method signatures.

I tend to start by having the class as a public member of the class you're already working in. But the moment it extends beyond simply a collection of values, it get's it's own file, and I move it out of the containing class.

So in retrospect, I believe I use Tuples when I don't want to go off and write a class, and just want to think about what I've writing right now. Which means the signature of the Tuple may change quite a lot in the text half an hour whilst I figure out what data I am going to need for this method, and how it's returning what ever values it will return.

If I get a chance to refactor code, then often I'll question a Tuple's place in it.

查看更多
登录 后发表回答