c sharp

How To Use Pair in C#

Pair is a useful data structure in C# that allows you to store a pair of values, where each value can be of different data types. The Pair structure is part of the System.Collections.Generic namespace in C#. This data structure can be used in various scenarios, such as storing key-value pairs or returning two values from a function. This article explores the usage of pairs in C# along with illustrative examples.

Using Pair in C#

To use Pair in C#, you need to include the System.Collections.Generic namespace in your code. Then, you can create a Pair object with two values using the Pair<T1, T2> class. The first value is of type T1, and the second value is of type T2.

Here is an example of creating a Pair object with two values:

Pair<string, int> myPair = new Pair<string, int>("Sam", 20);

In this example, we created a Pair object with two values, where the first value is a string “Sam” and the second value is an integer 20.

You can access the values of a Pair object using the properties First and Second, here is an illustration:

Console.WriteLine(myPair.First);
Console.WriteLine(myPair.Second);

You can also modify the values of a Pair object using these properties, here is an illustration:

myPair.First = "Mark";
myPair.Second = 30;

Console.WriteLine(myPair.First);
Console.WriteLine(myPair.Second);

Pair can be useful when you need to return two values from a function. Instead of creating a custom class or using out parameters, you can return a Pair object with two values. Here is an example:

using System;
using System.Collections.Generic;

namespace PairExample {
  class Program {
    static void Main(string[] args) {
      // Creating a pair
      Pair < string, int > myPair = new Pair < string, int > ("Sam", 20);
      // Output original pair
      Console.WriteLine("Original pair:");
      Console.WriteLine($"First: {myPair.First}, Second: {myPair.Second}");
      // Modifying a pair
      myPair.First = "Mark";
      myPair.Second = 30;
      // Output modified pair
      Console.WriteLine("\nModified pair:");
      Console.WriteLine($"First: {myPair.First}, Second: {myPair.Second}");
      Console.ReadLine();
    }
  }
  // Pair class
  public class Pair < T1, T2 > {
    public T1 First {
      get;
      set;
    }
    public T2 Second {
      get;
      set;
    }
    public Pair(T1 first, T2 second) {
      First = first;
      Second = second;
    }
  }
}

In this code, we first create a Pair object with a string and an integer value. We then access the values of the pair using the First and Second properties and then, we modify the values of the pair using the same properties.

Note that we define the Pair class separately from the Main method. The Pair class has two generic type parameters, T1 and T2, which represent the types of the first and second values of the pair, respectively. We define two properties, First and Second, to access these values, and we provide a constructor to initialize them.

Conclusion

Pair is a useful data structure in C# that allows you to store a pair of values, where each value can be of different data types. It can be used in various scenarios, such as storing key-value pairs or returning two values from a function. In this article, we have explored the utilization of pairs in C# through various examples. By incorporating pairs into your code, you can enhance its readability, maintainability, and overall simplicity.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.