อินเทอร์เฟซกำหนดคุณสมบัติ เมธอด และเหตุการณ์ ซึ่งเป็นสมาชิกของอินเทอร์เฟซ อินเทอร์เฟซประกอบด้วยการประกาศของสมาชิกเท่านั้น
อินเทอร์เฟซบางประเภทใน C # รวมอยู่ด้วย
-
จำนวนนับได้ - อินเทอร์เฟซพื้นฐานสำหรับคอลเล็กชันทั่วไปทั้งหมด
-
IList − อินเทอร์เฟซทั่วไปที่ใช้โดยอาร์เรย์และประเภทรายการ
-
ไอติม − ชุดพจนานุกรม
IEnumerable เป็นอินเทอร์เฟซที่กำหนดวิธีการเดียว GetEnumerator ที่ส่งคืนอินเทอร์เฟซ IEnumerator
ใช้งานได้สำหรับการเข้าถึงแบบอ่านอย่างเดียวในคอลเล็กชันที่นำ IEnumerable ไปใช้กับคำสั่ง foreach
ต่อไปนี้แสดงการใช้งานอินเทอร์เฟซ IEnumerable
ตัวอย่าง
class Demo : IEnumerable, IEnumerator {
// IEnumerable method GetEnumerator()
IEnumerator IEnumerable.GetEnumerator() {
throw new NotImplementedException();
}
public object Current {
get { throw new NotImplementedException(); }
}
// IEnumertor method
public bool MoveNext() {
throw new NotImplementedException();
}
// IEnumertor method
public void Reset() {
throw new NotImplementedException();
}
} ด้านบน คุณจะเห็น IEnumerator สองวิธี
// IEnumerator method
public bool MoveNext() {
throw new NotImplementedException();
}
// IEnumertor method
public void Reset() {
throw new NotImplementedException();
}