Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C# analog of C++ std::pair?

I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based.

Thank you!

like image 408
Alexander Prokofyev Avatar asked Oct 03 '08 09:10

Alexander Prokofyev


People also ask

What type of language is C?

C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system.

What is meant by C?

noun plural c's, C's or Cs. the third letter and second consonant of the modern English alphabet. a speech sound represented by this letter, in English usually either a voiceless alveolar fricative, as in cigar, or a voiceless velar stop, as in case.

What is C for computer?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

What is C and its uses?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

Tuples are available since .NET4.0 and support generics:

Tuple<string, int> t = new Tuple<string, int>("Hello", 4); 

In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following:

public class Pair<T, U> {     public Pair() {     }      public Pair(T first, U second) {         this.First = first;         this.Second = second;     }      public T First { get; set; }     public U Second { get; set; } }; 

And use it like this:

Pair<String, int> pair = new Pair<String, int>("test", 2); Console.WriteLine(pair.First); Console.WriteLine(pair.Second); 

This outputs:

test 2 

Or even this chained pairs:

Pair<Pair<String, int>, bool> pair = new Pair<Pair<String, int>, bool>(); pair.First = new Pair<String, int>(); pair.First.First = "test"; pair.First.Second = 12; pair.Second = true;  Console.WriteLine(pair.First.First); Console.WriteLine(pair.First.Second); Console.WriteLine(pair.Second); 

That outputs:

test 12 true 
like image 89
Jorge Ferreira Avatar answered Sep 22 '22 08:09

Jorge Ferreira