This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What problem was the tuple designed to solve?
Could someone please show/describe an appropriate use of the System.Tuple class?
I have looked on MSDN and the description there doesn't show any practical uses.
From the MSDN on the 2-tuple:
A tuple is a data structure that has a specific number and sequence of values. The Tuple class represents a 2-tuple, or pair, which is a tuple that has two components. A 2-tuple is similar to a KeyValuePair structure.
In the Tuple<T1, T2> case, It is a generalization of the KeyValuePair<,> data structure.
Tuples are basically generic data structures. You can use them to avoid defining your own structure. An example would be for returning multiple values from a function.
It is a very useful tool in function-oriented/functional programming.
Related
This question already has answers here:
Return multiple values to a method caller
(28 answers)
Is there a way I can return more than one integer from a method? [duplicate]
(5 answers)
Closed 2 years ago.
When my method needs to return more than one value, I can use out parameters or tuples.
Is this a personal preference or do we have a recommended practice documentation how to choose between the two options?
Prefer not to use a complex class type for every o such scenario, because my solution will be bloated with so many classes.
I understand that TryParse is an example exists in .net framework, however it was introduced before tuples.
I think that if you have many results that you want to return, ValueTuple ( for .NET Framesowrk 4.7 and above ) may be better for you because is a struct and immutable, especially if you have many results to return, or you use some kind of API like LINQ and you dont want to create objects of all the result.
You can read also :
Returning two values, Tuple vs 'out' vs 'struct'
What's the difference between System.ValueTuple and System.Tuple?
This question already has answers here:
When should I use a struct rather than a class in C#?
(31 answers)
Closed 4 years ago.
Why POCO objects are created using classes instead of structs in C#, while POCO is intended to carry only public attributes and not any behaviors?
Do we really need to create a class with get and set accessors? I think structs would be much cleaner and simple to carry object's state.
In C#, struct type is a value, so looks that it is not a good choice.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the purpose of the Tuple(T1)/Singleton in .net?
Trying to mimic a Tuple as implemented in .Net 4 (For .Net 3) I just realized there is a Tuple(Of T)? This was quite a surprize!
Why would anyone do this
Tuple<string> result = new Tuple<string>("Data");
Instead of this
return "Data";
Isn't the whole point of a tuple that its a container for "loosely related data that isnt cohesive enough to make another class"? Am I missing something?
There are a finite number of tuple-arities in the library, so to define an 8-tuple, you use the kind with 7-elements whose 'rest' argument is a one-tuple. See
http://msdn.microsoft.com/en-us/library/dd383325.aspx
This is a carry over from set theory that might not have much use for a software developer.
Tuples are simply ordered lists of elements. An N-tuple has n elements, and n can be one, which is called a singleton. You probably won't have much use for a 1-tuple in code, but I'm guessing the C# team put it in there for completeness.
http://en.wikipedia.org/wiki/Tuple#Etymology
This question already has answers here:
C# adding implict conversions to existing types
(2 answers)
Closed 9 years ago.
I have two custom classes for which I want to implement casts between each other. I only have the DLLs of the two projects and not the code. Can I use extension methods to implement the cast or would I need to do something else?
I'd suggest that you implement your own mappers between the 2 classes or use mapping tools such as AutoMapper or ValueInjecter
You will have to use either extension methods or some other mapping. You could also use http://automapper.codeplex.com/
I don't think there is a way to do it. Anyway, do you really need the code to look like cast? Sometimes when you implement operators or casts for custom types the code may become harder to understand. I would suggest to create separate utility to convert types which would be more obvious for someone who sees the code for the first time.
This question already has answers here:
SortedList<>, SortedDictionary<> and Dictionary<>
(6 answers)
Closed 9 years ago.
I have a large collection of small objects, each has a unique string ident. I need to decide which class to use.
MSDN says about the first two
The two classes have similar object
models, and both have O(log n)
retrieval. Where the two classes
differ is in memory use and speed of
insertion and removal
Since I rarely insert, mostly just retrieve it seems both are good for me. What about the plain old Dictionary?
Plain-old dictionary is the best option if you're not interested in sorting (since it's O(1) retrieval). If you're not going to modify the list much you should use SortedList since it uses less memory.