2

The differences between Lists and Dictionaries are already shown here.

However, I was trying to use Tuple in an application earlier. I want to differentiate between Tuples, Dictionaries, and Lists.

 Dictionary<int, string> dic = new Dictionary<int, string>();
 Tuple<int, string> tuple = new Tuple<int, string>();
 List<int> list = new List<int>();

What is the difference between these three?

jordanz
  • 367
  • 4
  • 12
Iswar
  • 2,211
  • 11
  • 40
  • 65

4 Answers4

25

First, AFAIK, there isn't any list that takes two type parameters - so your List could either be a List<int> or List<string> (or a SortedList<int,string>, but that's a different class).

A Tuple<T1, T2> holds two values - one int and one string in this case. You can use a Tuple to bind together items (that might be of different types). This may be useful in many situations, like, for instance, you want to return more than one value from a method.

I personally hate the Item1, Item2 property names, so I would probably use a wrapper class around Tuples in my code.

A Dictionary<TKey, TValue> is a collection of KeyValuePair<TKey, TValue>. A Dictionary maps keys to values, so that you can have, for instance, a dictionary of people and for each person have their SSN as a key.

A List<T> is a collection of T. You can get individual items by using the index, the Find method, or simply LINQ (since it implements IEnumerable<T>).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • 1
    I think in C# 8 tuples got much better in the sense that you don't need to see Item1 and Item2 anymore. You just give a name. And instead of having to create Tuple, you can just define your type as (T1 Name1, T2 Name2). Instead of Tuple tuple = new Tuple(); You do: (int id, string value) tuple = (myId, myValue); – digaomatias Feb 03 '22 at 06:35
  • @digaomatias There are also other differences between [`System.Tuple`](https://learn.microsoft.com/en-us/dotnet/api/system.tuple?view=net-6.0) and [`Syste.ValueTuple`](https://learn.microsoft.com/en-us/dotnet/api/system.valuetuple?view=net-6.0). – Zohar Peled Feb 03 '22 at 07:05
2

A list can store a sequence of objects in a certain order such that you can index into the list, or iterate over the list. List is a mutable type meaning that lists can be modified after they have been created.

A tuple is similar to a list except it is immutable. There is also a semantic difference between a list and a tuple. To quote Nikow's answer:

Tuples have structure, lists have order.

A dictionary is a key-value store. It is not ordered and it requires that the keys are hashable. It is fast for lookups by key.

2

While the answer you have linked discusses the differentiation between List<T> and Dictionary<int, T>, Tuple<int, string> is not addressed.

From MSDN

Represents a 2-tuple, or pair.

A tuple is a pair of values, opposed to the other types, which represent sort of collections. Think of a method, which returns an error code and string for the last error (I would not write the code this way, but to get the idea)

Tuple<int, string> GetLastError()
{
    ...
}

Now you can use it like

var lastError = GetLastError();
Console.WriteLine($"Errorcode: {lastError.Item1}, Message: {lastError.Item2}");

This way you do not have to create a class, if you want to return a compound value.

Please Note: As of C# 7 there is a newer, more concise syntax for returning tuples.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
1

So for the difference between a Dictionary and a List you've seen the question you linked to. So what is a Tuple? By MSDN:

A tuple is a data structure that has a specific number and sequence of values. The Tuple<T1, T2> class represents a 2-tuple, or pair, which is a tuple that has two components. A 2-tuple is similar to a KeyValuePair<TKey, TValue> structure.

While the two other data structures represent different collections of items (generic to be whatever type you declare) a Tuple<T1,....> is a single item containing a predefined amount of properties - similar to defining a class with few properties.

As the two first ones are collections of items and a tuple is a single item they play very different roles in terms of what to do with them.

Read more on

In addition you can have a look at What requirement was the tuple designed to solve?

Graham
  • 7,431
  • 18
  • 59
  • 84
Gilad Green
  • 36,708
  • 7
  • 61
  • 95