Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision?

前端 未结 13 1894
我在风中等你
我在风中等你 2020-12-02 04:55

With the addition of the Tuple class in .net 4, I have been trying to decide if using them in my design is a bad choice or not. The way I see it, a Tuple can be a shortcut

13条回答
  •  忘掉有多难
    2020-12-02 05:36

    I've used tuples, both the Tuple and the new ValueTuple, in a number of different scenarios and arrived at the following conclusion: do not use.

    Every time, I encountered the following issues:

    • code became unreadable due to lack of strong naming;
    • unable to use class hierarchy features, such as base class DTO and child class DTOs, etc.;
    • if they are used in more than one place, you end up copying and pasting these ugly definitions, instead of a clean class name.

    My opinion is tuples are a detriment, not a feature, of C#.

    I have somewhat similar, but a lot less harsh, criticism of Func<> and Action<>. Those are useful in many situations, especially the simple Action and Func variants, but anything beyond that, I've found that creating a delegate type is more elegant, readable, maintainable, and gives you more features, like ref/out parameters.

提交回复
热议问题