Practical example where Tuple can be used in .Net 4.0?

后端 未结 19 653
后悔当初
后悔当初 2020-12-04 07:44

I have seen the Tuple introduced in .Net 4 but I am not able to imagine where it can be used. We can always make a Custom class or Struct.

相关标签:
19条回答
  • 2020-12-04 07:54

    Tuples are great for doing multiple async IO operations at a time and returning all the values together. Here is the examples of doing it with and without Tuple. Tuples can actually make your code clearer!

    Without (nasty nesting!):

    Task.Factory.StartNew(() => data.RetrieveServerNames())
        .ContinueWith(antecedent1 =>
            {
                if (!antecedent1.IsFaulted)
                {
                    ServerNames = KeepExistingFilter(ServerNames, antecedent1.Result);
                    Task.Factory.StartNew(() => data.RetrieveLogNames())
                        .ContinueWith(antecedent2 =>
                            {
                                if (antecedent2.IsFaulted)
                                {
                                    LogNames = KeepExistingFilter(LogNames, antecedent2.Result);
                                    Task.Factory.StartNew(() => data.RetrieveEntryTypes())
                                        .ContinueWith(antecedent3 =>
                                            {
                                                if (!antecedent3.IsFaulted)
                                                {
                                                    EntryTypes = KeepExistingFilter(EntryTypes, antecedent3.Result);
                                                }
                                            });
                                }
                            });
                }
            });
    

    With Tuple

    Task.Factory.StartNew(() =>
        {
            List<string> serverNames = data.RetrieveServerNames();
            List<string> logNames = data.RetrieveLogNames();
            List<string> entryTypes = data.RetrieveEntryTypes();
            return Tuple.Create(serverNames, logNames, entryTypes);
        }).ContinueWith(antecedent =>
            {
                if (!antecedent.IsFaulted)
                {
                    ServerNames = KeepExistingFilter(ServerNames, antecedent.Result.Item1);
                    LogNames = KeepExistingFilter(LogNames, antecedent.Result.Item2);
                    EntryTypes = KeepExistingFilter(EntryTypes, antecedent.Result.Item3);
                }
            });
    

    If you were using an anonymous function with an implied type anyway then you aren't making the code less clear by using the Tuple. Retuning a Tuple from a method? Use sparingly when code clarity is key, in my humble opinion. I know functional programming in C# is hard to resist, but we have to consider all of those old clunky "object oriented" C# programmers.

    0 讨论(0)
  • 2020-12-04 07:57

    There's an excellent article in MSDN magazine that talks about the belly-aching and design considerations that went into adding Tuple to the BCL. Choosing between a value type and a reference type is particularly interesting.

    As the article makes clear, the driving force behind Tuple was so many groups inside of Microsoft having a use for it, the F# team up front. Although not mentioned, I reckon that the new "dynamic" keyword in C# (and VB.NET) had something to do with it as well, tuples are very common in dynamic languages.

    It is otherwise not particularly superior to creating your own poco, at least you can give the members a better name.


    UPDATE: due for a big revision in C# version 7, now getting a lot more syntax love. Preliminary announcement in this blog post.

    0 讨论(0)
  • 2020-12-04 07:58

    That's the point - it is more convenient not to make a custom class or struct all the time. It is an improvement like Action or Func... you can make this types yourself, but it's convenient that they exist in the framework.

    0 讨论(0)
  • 2020-12-04 08:00

    I don't like the abuse of them, since they produce code that doesn't explain itself, but they're awesome to implement on-the-fly compound keys, since they implement IStructuralEquatable and IStructuralComparable (to use both for lookup and ordering purposes).

    And they combine all of their items' hashcodes, internally; for example, here is Tuple's GetHashCode (taken from ILSpy):

        int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
        {
            return Tuple.CombineHashCodes(comparer.GetHashCode(this.m_Item1), comparer.GetHashCode(this.m_Item2), comparer.GetHashCode(this.m_Item3));
        }
    
    0 讨论(0)
  • 2020-12-04 08:01

    Tuples are heavily used in functional languages which can do more things with them, now F# is a 'official' .net language you may want to interoperate with it from C# and pass them between code written in two languages.

    0 讨论(0)
  • 2020-12-04 08:01

    A few examples off the top of my head:

    • An X and Y location (and Z if you like)
    • a Width and Height
    • Anything measured over time

    For example you wouldn't want to include System.Drawing in a web application just to use Point/PointF and Size/SizeF.

    0 讨论(0)
提交回复
热议问题