คลาส Tuple ให้คุณสมบัติ Item1, Item2, Item3 ... สำหรับเข้าถึงองค์ประกอบแต่ละตำแหน่ง โดย Item3 คือองค์ประกอบที่สาม
ตัวอย่างที่ 1: Tuple 7 องค์ประกอบ
using System;
public class Demo
{
public static void Main(string[] args)
{
var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
Console.WriteLine("Tuple1 เท่ากับ Tuple2? = " + tuple1.Equals(tuple2));
Console.WriteLine("HashCode ของ Tuple1 = " + tuple1.GetHashCode());
Console.WriteLine("HashCode ของ Tuple2 = " + tuple2.GetHashCode());
Console.WriteLine("Tuple1 องค์ประกอบที่ 1 = " + tuple1.Item1);
Console.WriteLine("Tuple2 องค์ประกอบที่ 1 = " + tuple2.Item1);
Console.WriteLine("Tuple1 องค์ประกอบที่ 2 = " + tuple1.Item2);
Console.WriteLine("Tuple2 องค์ประกอบที่ 2 = " + tuple2.Item2);
Console.WriteLine("Tuple1 องค์ประกอบที่ 4 = " + tuple1.Item4);
Console.WriteLine("Tuple2 องค์ประกอบที่ 4 = " + tuple2.Item4);
Console.WriteLine("Tuple1 องค์ประกอบที่ 5 = " + tuple1.Item5);
Console.WriteLine("Tuple2 องค์ประกอบที่ 5 = " + tuple2.Item5);
Console.WriteLine("Tuple1 องค์ประกอบที่ 6 = " + tuple1.Item6);
Console.WriteLine("Tuple2 องค์ประกอบที่ 6 = " + tuple2.Item6);
Console.WriteLine("Tuple1 องค์ประกอบที่ 7 = " + tuple1.Item7);
Console.WriteLine("Tuple2 องค์ประกอบที่ 7 = " + tuple2.Item7);
}
}
ผลลัพธ์
Tuple1 เท่ากับ Tuple2? = True HashCode ของ Tuple1 = 3231587 HashCode ของ Tuple2 = 3231587 Tuple1 องค์ประกอบที่ 1 = 75 Tuple2 องค์ประกอบที่ 1 = 75 Tuple1 องค์ประกอบที่ 2 = 200 Tuple2 องค์ประกอบที่ 2 = 200 Tuple1 องค์ประกอบที่ 4 = 700 Tuple2 องค์ประกอบที่ 4 = 700 Tuple1 องค์ประกอบที่ 5 = 100 Tuple2 องค์ประกอบที่ 5 = 100 Tuple1 องค์ประกอบที่ 6 = 1200 Tuple2 องค์ประกอบที่ 6 = 1200 Tuple1 องค์ประกอบที่ 7 = 1500 Tuple2 องค์ประกอบที่ 7 = 1500
ตัวอย่างที่ 2: Tuple 8 องค์ประกอบ (Item3 = 2200)
using System;
public class Demo
{
public static void Main(string[] args)
{
var tuple = Tuple.Create(1200, 1500, 2200, 2700, 3100, 3500, 4500, 5500);
Console.WriteLine("HashCode ของ Tuple = " + tuple.GetHashCode());
Console.WriteLine("องค์ประกอบที่ 1 = " + tuple.Item1);
Console.WriteLine("องค์ประกอบที่ 2 = " + tuple.Item2);
Console.WriteLine("องค์ประกอบที่ 3 = " + tuple.Item3); // <- องค์ประกอบที่สาม
Console.WriteLine("องค์ประกอบที่ 4 = " + tuple.Item4);
Console.WriteLine("องค์ประกอบที่ 5 = " + tuple.Item5);
Console.WriteLine("องค์ประกอบที่ 6 = " + tuple.Item6);
Console.WriteLine("องค์ประกอบที่ 7 = " + tuple.Item7);
// Tuple 8 องค์ประกอบ จะมี Rest property สำหรับองค์ประกอบที่ 8
}
}
ผลลัพธ์
HashCode ของ Tuple = 49989024 องค์ประกอบที่ 1 = 1200 องค์ประกอบที่ 2 = 1500 องค์ประกอบที่ 3 = 2200 องค์ประกอบที่ 4 = 2700 องค์ประกอบที่ 5 = 3100 องค์ประกอบที่ 6 = 3500 องค์ประกอบที่ 7 = 4500
หมายเหตุเกี่ยวกับ Tuple ใน C#
- Tuple คลาสเก่า (System.Tuple) เป็น Reference Type และ Immutable
- รองรับสูงสุด 8 องค์ประกอบ (Tuple<T1...T7, TRest>)
- ตั้งแต่ C# 7.0 แนะนำใช้ ValueTuple (System.ValueTuple) ซึ่งเป็น Struct (Value Type) มีประสิทธิภาพดีกว่า
ตัวอย่าง ValueTuple (สมัยใหม่ แนะนำ)
// Tuple แบบใหม่ - มีชื่อองค์ประกอบได้
var person = (Name: "John", Age: 30, City: "Bangkok");
Console.WriteLine(person.Name); // John
Console.WriteLine(person.Item2); // 30 (ยังเข้าถึงแบบ ItemN ได้)
// Deconstruction
var (name, age, city) = person;
Console.WriteLine(name); // John
// ฟังก์ชันคืนค่าหลายค่า
(string Name, int Age) GetPerson() => ("Alice", 25);
var p = GetPerson();
Console.WriteLine(p.Name); // Alice
ข้อแตกต่างสำคัญ
| คุณสมบัติ | System.Tuple (คลาสเก่า) | System.ValueTuple (Struct ใหม่) |
|---|---|---|
| ประเภท | Class (Reference Type) | Struct (Value Type) |
| Mutability | Immutable | Mutable |
| ชื่อองค์ประกอบ | ไม่รองรับ (Item1, Item2...) | รองรับ (Name, Age...) |
| ประสิทธิภาพ | ช้ากว่า (Heap allocation) | เร็วกว่า (Stack allocation) |
| สูงสุด | 8 องค์ประกอบ | ไม่จำกัด (แต่แนะนำไม่เกิน 7-8) |