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 objectRelacaoUser
with both attributes or I can use theTuple
to connect the two objects in one. According to your answer the best would be to use theTuple<User,User>
loading the ID inside each objectUser
correct?– Leonardo Bonetti
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
?– Maniero
Yes, after commenting you answered there. Thank you !
– Leonardo Bonetti