Implement Many-To-Many Relationship in C# Class

In previous articles, we saw how to implement various relationships among C# classes. You can read them here:
  1. One one relationship in C# class
  2. Implement one too many relationships in C# class.
  3. Many to One Relationship in C# Class
In this article, we will see how to implement a Many-To-Many relationship in a C# class. Let's explain a few scenarios where a Many-To-Many relationship is relevant.
 
The relationship between a Cricket match and Cricketer is one example of a Many-To-Many relationship. One Cricket match is played by many Cricketers and one Cricketer takes part in many Cricket matches.
 
Let's implement this example using a C# class.
 
Relationship between Cricketer and matches
 
As we have explained many Cricketers play in a single Cricket match and one Cricketer takes part in multiple matches. For the sake of simplicity, we have used only a Cricketer class. We can use a Matches class in the same fashion. Have a look at the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace Client {  
  7.     class Cricketer {  
  8.         List < Matches > MLst = new List < Matches > ();  
  9.         List < Cricketer > CList = new List < Cricketer > ();  
  10.   
  11.         public string Name { get;  
  12.             set; }  
  13.   
  14.         public List < Matches > getSetMatch {  
  15.             get { return MLst; }  
  16.             set { MLst = value; }  
  17.         }  
  18.   
  19.         public List < Cricketer > getsetCricketer {  
  20.             get { return CList; }  
  21.             set { CList = value; }  
  22.         }  
  23.   
  24.         public void Print() {  
  25.             Console.WriteLine("-----Cricketer Name---- ");  
  26.             foreach(Cricketer c in CList) {  
  27.                 Console.WriteLine(c.Name);  
  28.             }  
  29.   
  30.             Console.WriteLine("-----Matches Played----");  
  31.             foreach(Matches m in MLst) {  
  32.                 Console.WriteLine(m.MatchVs);  
  33.             }  
  34.         }  
  35.     }  
  36.   
  37.     class Matches {  
  38.         List < Cricketer > Lst = new List < Cricketer > ();  
  39.         public string MatchVs { get;  
  40.             set; }  
  41.   
  42.         public void Print() {  
  43.             Console.WriteLine("Match Verses");  
  44.             foreach(Cricketer c in Lst) {  
  45.                 Console.WriteLine("Cricketer Name:- " + c.Name);  
  46.             }  
  47.         }  
  48.     }  
  49.   
  50.     class Program {  
  51.         static void Main(string[] args) {  
  52.             List < Cricketer > CList = new List < Cricketer > ();  
  53.             List < Matches > MList = new List < Matches > ();  
  54.   
  55.             Cricketer c = new Cricketer();  
  56.             c.Name = "Sourav Ganguly";  
  57.             CList.Add(c);  
  58.             c = new Cricketer();  
  59.             c.Name = "Sachin Tendulkar";  
  60.             CList.Add(c);  
  61.   
  62.             //Add matches  
  63.             Matches m = new Matches();  
  64.             m.MatchVs = "India-Sri Lanka";  
  65.             MList.Add(m);  
  66.   
  67.             //Add another match  
  68.             m = new Matches();  
  69.             m.MatchVs = "India - Bangladesh";  
  70.             MList.Add(m);  
  71.   
  72.             c.getsetCricketer = CList;  
  73.             c.getSetMatch = MList;  
  74.             c.Print();  
  75.   
  76.             Console.ReadLine();  
  77.         }  
  78.     }  

Here is a sample output:
 
Many to Many Relationship
 

Conclusion

 
In this article, we have learned how to implement a Many-To-Many relationship in a C# Class. Hope you have understood the concept. Though it is a very rare scenario where a Many-To-Many relationship is relevant. Happy learning.