Is Tuple the same thing as creating an object?

8

I've used Tuple a few times today came the doubt, this:

public class User{
    public String Name {get; set;}
    public String LastName {get; set;}
}

new User(){
    Name = "Leonardo",
    LastName = "Bonetti"
}

Is it the same as this?

new Tuple<String,String >("Leonardo", "Bonetti")

I'm not sure if this is the case.

    
asked by anonymous 13.08.2018 / 17:05

2 answers

10

Strictly speaking, yes, it's the same thing. After all, every tuple is an object, just like all data in C #.

But this Tuple you are using is already considered obsolete. If you want to know more about it: What is Tuple and when to use it .

Today it is much more common to use the language tuple , which even has names for the fields (and < not equal to a type, and other than Tuple that even has names, but generic ( Item1 , Item2 , etc.). This one has much more advantages of performance, semantics, tools and ways to use. This is not to say that the old form has no reason to use, but it is quite difficult to do and if the Tuple class would still be an advantage, there is probably another way to do it.

Of course creating a class (and there we are not talking about object), is not exactly the same thing, but it is similar. The class will allow you to use it as a template to create objects of this type (a class creates a type in your application) and of course, it will have a name. It is much more advantageous to use a class for your example (at least without seeing a larger context), User will be an object that used in every application, you do not have to use an object that we can say is anonymous and without a clear model. It turns into a mess, every moment you can make a different user, and you will not know what that is, because the type of object created with the tuple mechanism is Tuple (in the oldest).

Tuple was created for something quite specific, to carry more than one data, which has some relation at that time, as a single object. It was primarily created to return more than one value by a method that only accepts returning a value. That is, it is to build it by placing objects inside it, and get those objects on the other side and it's over.

But this generates allocation, the language does not know what that is, it's a class like any other, and it's complicated for tools to work with it, it's too generic, it's just a little better than doing% . So for those cases where a tuple is useful, it has been used a List<object> working in the stack , not by pressing the garbage collector , the language understands what it does and can be used in many ways, it is more intuitive, Visual Studio helps you with Intelisense, after all field names are visible, among other advantages.

Even this new form is unsuitable to replace its (objeto1, objeto2) class, alias is even worse because the semantics of this class is by reference and the tuple is by value. And that's good, it shows how very different mechanisms are for completely different purposes. The tuple is a mechanism of your application, the class may even be this, but often, and in the specific example is, a model for a domain object, that is, it has to do with the problem you are solving.

Never use a tuple of any kind to replace a useful type for the application. Tuple is just to join objects that need to be together at that time and not to represent a general object of the application, so at this point not totally different.

Know anonymous method? It's more like that and I honestly think that language should never have created this mechanism, just as the library should not have User , simple tuple is the solution for those cases.

A tuple can not be confused as a substitute for a type.

    
13.08.2018 / 17:28
5

I personally think Tuple should be used when we need to simplify something in the code, a stretch that needs simple structural validation, with Item1 + Item2 where we do not have to know what the meaning is in each of the elements, in addition to being much more restrictive than a Class .

Tuple is much simpler to implement relative to classes, it's more lightweight , but we should use common sense to figure out where we should use one or the other.

A class is something more meaningful and much broader, we should use when it is important to know what the X or Y property represents and under what circumstances it should be used.

In short, everything depends on the use we give the object, I think it will be something to analyze case by case.

There are good answers in this StackOverflow "global" issue, take a look! When to use: Tuple vs Class C # 7.0

    
13.08.2018 / 17:17