โครงสร้างเป็นชนิดข้อมูลประเภทค่า ช่วยให้คุณสร้างตัวแปรเดียวเก็บข้อมูลที่เกี่ยวข้องกับประเภทข้อมูลต่างๆ คีย์เวิร์ด struct ใช้สำหรับสร้างโครงสร้าง
ในการกำหนดโครงสร้าง คุณต้องใช้คำสั่ง struct คำสั่ง struct กำหนดประเภทข้อมูลใหม่ โดยมีสมาชิกมากกว่าหนึ่งรายสำหรับโปรแกรมของคุณ
ตัวอย่างเช่น นี่คือวิธีที่คุณสามารถประกาศโครงสร้างหนังสือ สมาชิกดังต่อไปนี้ −
struct Books {
public string title;
public string author;
public string subject;
public int id;
}; ในการเข้าถึงและแสดงสมาชิกเหล่านี้ในโครงสร้าง -
ตัวอย่าง
using System;
struct Books {
public string title;
public string author;
public int id;
};
public class testStructure {
public static void Main(string[] args) {
Books Book1;
Book1.title = "PHP IN 7 Days";
Book1.author = "Jacob Dawson";
Book1.id = 34;
Console.WriteLine( "Book 1 title : {0}", Book1.title);
Console.WriteLine("Book 1 author : {0}", Book1.author);
Console.WriteLine("Book 1 book_id :{0}", Book1.id);
Console.ReadKey();
}
}