Computer >> Máy Tính >  >> Lập trình >> C#

Làm thế nào để tạo 6-Tuple trong C #?

Lớp Tuple đại diện cho 6 tuple. Tuple là một cấu trúc dữ liệu có một chuỗi các phần tử.

Nó có sáu thuộc tính -

  • Item1 - Nhận giá trị của thành phần đầu tiên của đối tượng Tuple .

  • Lặp lại 2 - Nhận giá trị của thành phần thứ hai của đối tượng Tuple .

  • Item3 - Nhận giá trị của thành phần thứ ba của đối tượng Tuple .

  • Mục 4 - Nhận giá trị của thành phần thứ tư của đối tượng Tuple .

  • Item5 - Nhận giá trị của thành phần thứ năm của đối tượng Tuple .

  • Mục 6 - Nhận giá trị của thành phần thứ sáu của đối tượng Tuple hiện tại.

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ để triển khai bộ 6-tuple trong C # -

 using System; public class Demo {public static void Main (string [] args) {Tuple  tuple =new Tuple  ("jack", 150, "pete", 300, 600, "allan"); Console.WriteLine ("Giá trị (Item1) =" + tuple.Item1); Console.WriteLine ("Giá trị (Item2) =" + tuple.Item2); Console.WriteLine ("Giá trị (Item3) =" + tuple.Item3); Console.WriteLine ("Giá trị (Item4) =" + tuple.Item4); Console.WriteLine ("Giá trị (Item5) =" + tuple.Item5); Console.WriteLine ("Giá trị (Item6) =" + tuple.Item6); if (tuple.Item1 =="kevin") {Console.WriteLine ("Tồn tại:Tuple Item 1 =" + tuple.Item1); } if (tuple.Item2 ==250) {Console.WriteLine ("Tồn tại:Tuple Item 2 =" + tuple.Item2); } if (tuple.Item3 =="pete") {Console.WriteLine ("Tồn tại:Tuple Item 3 =" + tuple.Item3); } if (tuple.Item4 ==300) {Console.WriteLine ("Tồn tại:Tuple Item 4 =" + tuple.Item4); } if (tuple.Item5 ==400) {Console.WriteLine ("Tồn tại:Tuple Item 5 =" + tuple.Item5); } if (tuple.Item6 =="allan") {Console.WriteLine ("Tồn tại:Tuple Item 6 =" + tuple.Item6); }}} 

Đầu ra

Điều này sẽ tạo ra kết quả sau -

 Value (Item1) =jackValue (Item2) =150Value (Item3) =peteValue (Item4) =300Value (Item5) =600Value (Item6) =allanExists:Tuple Item 3 =peteExists:Tuple Item 4 =300Exists:Tuple Item 6 =allan 

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ khác để triển khai bộ 6 trong C # -

 using System; public class Demo {public static void Main (string [] args) {Tuple  tuple =new Tuple  (100, 150, 200, 300, 600, 1000); Console.WriteLine ("Giá trị (Item1) =" + tuple.Item1); Console.WriteLine ("Giá trị (Item2) =" + tuple.Item2); Console.WriteLine ("Giá trị (Item3) =" + tuple.Item3); Console.WriteLine ("Giá trị (Item4) =" + tuple.Item4); Console.WriteLine ("Giá trị (Item5) =" + tuple.Item5); Console.WriteLine ("Giá trị (Item6) =" + tuple.Item6); if (tuple.Item1 ==100) {Console.WriteLine ("Tồn tại:Tuple Item 1 =" + tuple.Item1); } if (tuple.Item2 ==250) {Console.WriteLine ("Tồn tại:Tuple Item 2 =" + tuple.Item2); } if (tuple.Item3 ==300) {Console.WriteLine ("Tồn tại:Tuple Item 3 =" + tuple.Item3); } if (tuple.Item4 ==300) {Console.WriteLine ("Tồn tại:Tuple Item 4 =" + tuple.Item4); } if (tuple.Item5 ==400) {Console.WriteLine ("Tồn tại:Tuple Item 5 =" + tuple.Item5); } if (tuple.Item6 ==500) {Console.WriteLine ("Tồn tại:Tuple Item 6 =" + tuple.Item6); }}} 

Đầu ra

Điều này sẽ tạo ra kết quả sau -

 Giá trị (Item1) =100Value (Item2) =150Value (Item3) =200Value (Item4) =300Value (Item5) =600Value (Item6) =1000