Whats the point of Tuple(Of T) [duplicate] - c#

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

Related

For methods that need to return multiple values, is it better practice to use tuples, out parameters? [duplicate]

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?

What is the use of an empty statement [duplicate]

This question already has answers here:
C# Empty Statement
(13 answers)
Closed 5 years ago.
I've come across this example of an empty statement in a C# textbook.
Code:
public void empty()
{
;
}
Some quick googling found that it's a redundant feature and I can't see the use of this as it seems pointless?
I was curious to know when this would've been useful and if it's still used to date even though it's obsolete?
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.

How do I parse a string and get formula? in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Evaluate C# string with math operators
So lets say I have a basic string with the value "1*2+4"
How would I go about parsing the information and then doing the calculation?
I am self teaching c# but my assumption is this is a common issue in programming but without using a library how would one do this?
My results so far have got me splitting the string up and putting them into an array of chars, but this is where I've stopped since I am trying to figure out how to compare chars to operators and chars to ints.
I am not sure if I am going the right way about this but would be great if someone could point me in the right direction.
Thank you for your help in advanced.
What you're looking for is the Shunting-yard algorithm.
You'll need at least two stacks; one for storing operators and one for operands. After you fill the stacks you can make a RPN and calculate the answer.
Well c# (or any other language) might provide you with various tools to help you, but the overall approach to the problem will always remain the same whatever the programming language be.
So yes, you do split up into operators & integers. You do recognize the characters one by one, but try to do it in the most efficient way of the language. Fosco's anser points to the right link. Use Ncalc Library than doing manual labor.
However, to complete what you started :
int.Parse(str)
int.TryParse(str, out num)
...are the functions you may consider to convert character strings into integers (which you got, by using split() function?) in C#. You can read about them here...(Parse, TryParse)
If you want to learn how the various existing libraries do it, you should learn about parsing, lexical and syntactic analysis, expression trees, compiler theory, etc. Also, go through the source-code of any of the multiple open-source libraries that do it.

SortedList<K,V> vs SortedDictionary<K,V> vs Dictionary<K,V> [duplicate]

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.

How do you do maths (or math) with numbers bigger than MaxValue in C#? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Big integers in C#
I want to be able to process arbitrarily large numbers in C#.
I can live with just integers.
Are there established algorithms for this kind of thing?
Or is there a good 3rd-party library?
I might consider changing language if there really is no good way to do it in C#.
Thanks for any help / suggestions.
You might want to check the Big Num kind of libraries. In C# the most popular ones are: IntX and W3b.Sine, plus, they are both Open Source.
Someone has done a BigInteger class in C# at codeproject
C# BigInteger Class.
Worth checking out.
And a sample of the general algorithm can be found at : Re: Hi-prec math for visual studio c#? (It's the post at the bottom of the page with the code snippet)
As people have mentioned, there are various 3rd-party implementations of a BigInteger class. Also, C# 4.0 will be getting a native BigInteger class in the CLR.
If you just need very large numbers, you could use long (64-bit), or even decimal (128-bit floating point).
If you need values larger than 9223372036854775807 (long) or 79228162514264337593543950335 (decimal), then you need to ignore this answer.

Categories