In C#, a singleton tuple is a tuple that contains only one element and singleton tuple is also known as 1-tuple. You can create a 1-tuple using two different ways:
Using Tuple<T1>(T1) Constructor
You can create a singleton tuple using Tuple <T1>(T1) constructor. It initializes a new instance of the Tuple<T1> class. But when you create a tuple using this constructor then you have to specify the type of the element stored in the tuple.
Syntax:
public Tuple (T1 item1);
Here, item1 is the value of the tuple’s only component.
Example:
using System;
public class GFG {
static public void Main()
{
Tuple< string > My_Tuple = new Tuple< string >( "GeeksforGeeks" );
Console.WriteLine( "Element: " + My_Tuple.Item1);
}
}
|
Output:
Element: GeeksforGeeks
Using Create Method
You can also create a singleton tuple with the help of Create method. When you use this method then there is no need to specify the type of the elements stored in the tuple.
Syntax:
public static Tuple<T1> Create<T1> (T1 item1);
Here, item1 is the value of the tuple element and T1 is the type of the element stored in the tuple.
Return Type: This method returns the components of the singleton tuple whose value is item1.
Example:
using System;
public class GFG {
static public void Main()
{
var My_Tuple = Tuple.Create( "Geeks" );
Console.WriteLine( "Element: " + My_Tuple.Item1);
}
}
|
References:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!