Is Tuple the same as creating an object?

Asked

Viewed 228 times

9

I’ve used the Tuple a few times and today came the doubt, that:

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

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

It’s the same thing as that?

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

I have this doubt because it seems that yes, but in a less organized way because there is no name for the attributes.

2 answers

11


Strictly speaking, yes, it is the same thing. After all every tuple is a object, as well as all data in C#.

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

Today it is much more common to use the language tuple, that even have names for the fields (and nonattributes) equal to a type, and different from Tuple that even have names, but generic (Item1, Item2, etc.). This has many 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 very difficult to happen, and if the class Tuple would still be an advantage probably has another way of doing.

Of course creating a class (and then we’re not talking about an object), it’s not exactly the same thing, but it’s similar. The class will allow you to use it as a template to create objects of this type (a class creates a type in its 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 more context), User will be an object that used in every application, has no reason to use an object that we can say is anonymous and without a clear model. It turns messy, every moment you can make a different user, and you won’t even know what that is, because the type of object created with the tuple mechanism is a Tuple (in the oldest).

Tuple was created for something quite specific, to carry more than one data, which has some relation at that moment, as a single object. It was created primarily to return more than one value by a method that only accepts to return one value. I mean, it’s to build it by putting objects inside it, and taking those objects from the other side and it’s over.

Only this generates allocation, the language doesn’t know what this is, it’s a class like any other, and it gets complicated for tools to work with it, it’s too generic, it’s just a little bit better than doing a List<object>. So for these cases where a tuple (vessel) is useful has passed if using a (objeto1, objeto2) who works at stack, not pressing the Garbage Collector, the language understands what it does and can be used in various ways, it is more intuitive, Visual Studio helps you with Intelisense, after all the names of the fields are visible, among other advantages.

Even this new form is unsuitable to replace your class User, 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 that, 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 to replace a useful type for the application. Tupla is just to join objects that need to be together at that moment and not to represent a general object of the application, so at that point they are totally different.

Do you know any anonymous methods? It’s more like this and I honestly think that language should never have created this mechanism (anonymous method), just as the library should not have Tuple, the simple tuple is the solution for these cases.

A tuple cannot be mistaken as a substitute for a specific type, even if it is a general type.

  • Maniero, I’ll give you an example just to see if I got the concept and its recommendation right, analyzing the table of this image: https://i.stack.Imgur.com/rSuts.png are 2 ID’s of 2 different users(ie object User), i need to return ONLY these two related ID’s, I can both create an object RelacaoUser with both attributes or I can use the Tuple to connect the two objects in one. According to your answer the best would be to use the Tuple<User,User> loading the ID inside each object User correct?

  • On the other question about the subject I only saw after I think I answered that, didn’t I? I wasn’t clear not to use Tuple?

  • Yes, after commenting you answered there. Thank you !

5

Personally I think that the Tuple should be used when we need to simplify something in the code, an excerpt that needs a simple structural validation, with Item1 + Item2 where we do not necessarily need to know the meaning of what is in each of the elements, and it is much more restrictive than a Class.

Tuple is much simpler to implement in relation to classes, it is more lightweight, but we must use common sense to understand where we should use one or the other.

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

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

There are good answers to this question of Stackoverflow "global", take a look at! When to use: Tuple vs Class c# 7.0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.