ว่าด้วยเรื่อง IEnumerator, IEnumerable และ ICloneable

23 Aug 2016,
Share: 

บทความนี้ จะมาพูดถึง interface 3 ตัวนี้ IEnumerator, IEnumerable และ ICloneable ซึ่ง .net เตรียมไว้ให้เราไว้ใช้งาน โดยมีรายละเอียดดังนี้

IEnumerator

เป็น interface พื้นฐานเพื่อให้ class ที่อ้างถึง interface นี้สามารถใช้ foreach ได้ ภายในประกอบไปด้วย

  • Method
    • MoveNext ระบุให้เลื่อน element ปัจจุบันไป 1 ช่อง
    • Reset สังให้ current กลับไปจุดเริ่มต้น
  • Properties
    • Current ดึง element ปัจจุบันจาก collection

ตัวอย่าง เราจะสร้าง class Month ที่จะใช้งานกับ foreach ได้  โดยเราต้องสร้าง Class Month ที่ inherited interface IEnumerable แล้ว implement method GetEnumerator() โดยเราจะต้อง return เป็น class ที่ inherited interface IEnumerator ตามตัวอย่าง source code ดังนี้

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IEnumeratorExample {
    class Program {
        static void Main(string[] args) {

            Month objMonth = new Month();

            foreach (string item in objMonth) {
                Console.WriteLine(item);
            }

            Console.ReadKey();

        }
    }

    class Month : IEnumerable {

        public string[] arrMonth = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

        public IEnumerator GetEnumerator() {
            return new AllMonth(this);
        }
    }

    class AllMonth : IEnumerator {

        private Month _month;
        private int index = -1;

        public AllMonth(Month inMonth) {
            _month = inMonth;
        }

        public object Current {
            get {
                return _month.arrMonth[index];
            }
        }

        public bool MoveNext() {
            index++;
            if (index < _month.arrMonth.Length) {
                return true;
            } else {
                return false;
            }
        }

        public void Reset() {
            index = -1;
        }
    }
}

จะได้ผลลัพธ์ดังนี้

Image

download source code

IEnumerable

เป็น interface ที่จะทำการส่งคลาส IEnumerator ไปยังคำสั่ง foreach ภายในมีสมาชิกดังนี้

  • Method
    • GetEnumerator ส่งค่า enumerator ที่จะส่งไปยัง foreach

ตัวอย่าง ตัวอย่างนี้ผมจะสร้าง function ที่ return ค่าเป็นประเภท IEnumerable ในตัวอย่างนี้ผมใช้ yield ซึ่ง yield ใน C# จะถูกใช้ใน method ที่ return type IEnumerable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IEnumerableExample {
    class Program {
        static void Main(string[] args) {

            foreach (int item in getNumber()) {
                Console.WriteLine("Number from getNumber(): " + item.ToString());
            }

            Console.ReadKey();

        }

        private static IEnumerable<int> getNumber() {
            yield return 1;
            yield return 2;
            yield return 3;
            yield return 4;
            yield return 5;
            yield return 6;
            yield return 7;
            yield return 8;
            yield return 9;
            yield return 10;
        }
    }
}

จะได้ผลลัพธ์ดังนี้

Image

download source code

ICloneable

เป็น interface ที่กำหนดให้คลาสที่ประกาศต้องมีความสามารถในการ clone ตัวมันเอง

  • Method
    • Clone ทำการ clone ตัวมันเองแล้วส่งค่านั้นกลับไป

ตัวอย่าง ตัวอย่างเราจะสร้าง class ที่เก็บตัวเลข และสามารถ Clone ตัวเองได้ เขียนด้วยภาษา C# ดังนี้

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ICloneableExample {
    class Program {
        static void Main(string[] args) {
            myNumber obj1, obj2;

            obj1 = new myNumber(1);
            obj2 = (myNumber)obj1.Clone();

            obj1.number = 90;

            Console.WriteLine("obj1 number: " + obj1.number.ToString());
            Console.WriteLine("obj2 number: " + obj2.number.ToString());

            Console.ReadKey();
        }
    }

    class myNumber : ICloneable {

        private int _number = 0;

        public int number { get { return _number; } set { _number = value; } }

        public myNumber(int inNumber) {
            _number = inNumber;
        }

        public object Clone() {
            return new myNumber(_number);
        }
    }
}

จะได้ผลลัพธ์ดังนี้

Image

download source code

Suggestion blogs

Vue.js เริ่มต้น ตอน2 (Component)

ในบทความตอนที่แล้ว "Vue.js เริ่มต้น ตอน1 (สร้าง Project)" ผมได้อธิบายถึงแนวทางการพัฒนาเว็บด้วย Vue.js ในแบบต่างๆ และการสร้าง Project ด้วย Vue-cli ถ้าใครยังไม่ได้อ่านกลับไปอ่านก่อนเนื่องจากเนื้อหาในบทความนี้เป็นเนื้อหาที่ต่อเนื่องจากบทความที่แล้ว

ประเภท SSL ของ Cloudflare

ประเภท SSL ของ CloudflareImageการใช้งาน SSL ใน Cloudflare แบ่งออกเป็น 4 ประเภทOff -> ก็คือการปิดการทำงานของ SSLFlexible SSL จะเป็นการเข้ารหัสจาก User ถึง CloudflareFull SSL จะเป็นการเข้ารหัสทั้งจาก User ถึง Cloudflare จะจาก Cloudflare ถึง Server เรา (ใช้ใบรับรองแบบ Self-signed ได้)Full SSL (strict) จะเป็นการเข้ารหัสทั้งจาก User ถึง Cloudflare จะจาก Cloudflare ถึง Server เรา ซึ่งจะต้องใช้ใบรับรองที่ได้รับการยอมรับจาก Root CAข้อมูลจาก cloudflare

vb.net excel database ด้วย OleDb

ในบทความนี้จะมาพูดถึงเรื่องการทํางานกับ file Excel ด้วย VB.NET โดยจะสามารถเข้าถึงข้อมูล แก้ไขข้อมูล และเพิ่มข้อมูล ด้วยคําสั่ง SQL เหมือนกับเป็น Database ตัวนึง ซึ่งการใช้งานจะต้องใช้ libraries ที่ชื่อว่า OleDb ใน Virtual Studio จะมีให้อยู่แล้ววิธีการใช้งานจะแบ่งเป็นสองแบบดังนี้การใช้งานโดยไม่ต้องการผลลัพธ์ของข้อมูลเช่น insert, update, deleteการใช้งานโดยต้องการผลลัพธ์ของข้อมูลเช่น Query ข้อมูลจาก Excel(คําสั่ง Select)


Copyright © 2019 - 2026 thiti.dev |  v1.61.0 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Nostr   Youtube channel   Telegram   Email contact   วงแหวนเว็บ