Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Cách tạo 1-Tuple (Singleton Tuple) trong C#

Lớp Tuple<T1> biểu diễn 1-tuple, còn gọi là singleton. Tuple là cấu trúc dữ liệu có trình tự các phần tử.

Ví dụ 1

using System;
public class Demo
{
    public static void Main(string[] args)
    {
        Tuple tuple = new Tuple(100);
        Console.WriteLine("Value = " + tuple.Item1);
        if (tuple.Item1 == 150)
        {
            Console.WriteLine(tuple.Item1);
        }
        if (tuple.Item1 == 100)
        {
            Console.WriteLine(tuple.Item1);
        }
    }
}

Đầu ra

Value = 100
100

Ví dụ 2

using System;
public class Demo
{
    public static void Main(string[] args)
    {
        Tuple tuple = new Tuple("amit");
        Console.WriteLine("Value = " + tuple.Item1);
        if (tuple.Item1 == "tom")
        {
            Console.WriteLine(tuple.Item1);
        }
        if (tuple.Item1 == "amit")
        {
            Console.WriteLine("Exists: Tuple Value = " + tuple.Item1);
        }
    }
}

Đầu ra

Value = amit
Exists: Tuple Value = amit