Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีสร้าง Tuple ที่มีสมาชิก string และ int ใน C#

ขั้นแรก กำหนดสมาชิก 2 ตัวใน tuple:

Tuple<int, string> tuple = new Tuple<int, string>(20, "Tom");

ตรวจสอบสมาชิกตัวแรก (integer):

if (tuple.Item1 == 20) { Console.WriteLine(tuple.Item1); }

ตรวจสอบสมาชิกตัวที่สอง (string):

if (tuple.Item2 == "Tom") { Console.WriteLine(tuple.Item2); }

ตัวอย่าง

using System; 
using System.Threading; 
namespace Demo { 
    class Program { 
        static void Main(string[] args) { 
            Tuple<int, string> tuple = new Tuple<int, string>(20, "Tom"); 
            if (tuple.Item1 == 20) Console.WriteLine(tuple.Item1); 
            if (tuple.Item2 == "Tom") Console.WriteLine(tuple.Item2); 
        } 
    } 
}

ผลลัพธ์

20 Tom