Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real world and efficient example of solving a problem in C# using Tuple class [duplicate]

Tags:

c#

tuples

c#-4.0

We have been discussing Tuple and its various possible uses. In almost all cases it sounds like root of evil, it makes design worse, and generally calls for a specialized class instead of Tuple everywhere it could be used.

It is supposedly close to DB but its hard to find a use for Tuple in db persistant logic code too.

Has anyone any real world good sample of Tuple use , preferably closer to some real world domain model problem or any use actualy.

like image 361
Valentin Kuzub Avatar asked Sep 11 '11 08:09

Valentin Kuzub


People also ask

What are some real world problems that can be solved using C?

Some real word problem which can be easily solved by using c language like traffic controller, vehicle parking. How can you use your programming skills to solve real world problems? We actually learn programming to solve real world problems, it’s not just for schools and academies.

What are some examples of solved programs in C?

Here is the List of C solved programs/examples with solutions (category wise) and detailed explanation. C Solved Programs by categories……. C program to find the sum of Series: 1/1! + 2/2! + 3/3! + 4/4! +….+ N/N! C Program to implement Matrix Multiplication using Recursion C Program to Display all Nodes in Linked List using Recursion

What are some real world problems that can be solved by programming?

Many real world problem can be solved by programming like c,c++, Java, .net. Some real word problem which can be easily solved by using c language like traffic controller, vehicle parking. How can you use your programming skills to solve real world problems?

What are the real world examples of C programming?

What are the real world examples in c? There are thousands of real time softwares written in C programming . C programming is sufficient language to build embedded system. 1. Operating Systems:


2 Answers

Tuple is said to have been introduced to .NET 4 because some programming languages (let's say, for example, IronPython or F#) support tuples as a core feature of the language, and the .NET BCL team wanted to provide a uniform tuple type for such languages (for language interoperability reasons, I guess):

The BCL team decided to work with the F# team to standardize on one tuple type for the framework so that every language could benefit from them. (in Eric Lippert's answer to the SO question, What problem was the tuple designed to solve? )

Tuple does not make much sense in the C# language IMHO, because C# doesn't support tuples and some associated language constructs very well; e.g. tuple "explosion": spreading a tuple over a function's parameters, or taking a function's return values (in the form of a Tuple) and spreading it over several local variables:

Tuple<int,int>  GetFoo() { … }
//     v   v
int   (a,  b) = GetFoo();  // C# doesn't support this tuple-related syntax.

That being said, if you're writing only C# code, you can usually find "better", more structured solutions than using Tuple. For example, using tuples to return more than one value from a method (such as in GetFoo above) is quick and convenient, but the return value doesn't have any inherent structure — you might do better with a struct or class type, if you're willing to take some additional time to define it.

like image 125
stakx - no longer contributing Avatar answered Oct 02 '22 02:10

stakx - no longer contributing


There are many languages where it's legal to return multiple values (something like return (a, b) instead of using ref or out (these are considered another root of all evils) .) Tuple solves this problem.

Let's look at int Math.DivRem(int, int, out int). It could be refactored in Tuple<int, int> Math.DivRem(int, int). If you think that in this case instead of Tuple you should use special classes, well, for delegates the direction is the opposite: after creating hundred (tens in reality) of specialized delegates MS has created generic Action and Func and has begun using them.

(We will ignore that Tuple is a family of classes so using them is "slow". Premature optimization and all that beautiful things :-) )

I'll add that Tuple solves the problem of writing short code blocks here on SO :-)

like image 25
xanatos Avatar answered Oct 02 '22 00:10

xanatos