Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. การได้ประเภทของอินสแตนซ์ปัจจุบันใน C#

    ใน C# ทุกอ็อบเจ็กต์สืบทอดมาจาก object ซึ่งมีเมธอด GetType() คืนค่า Type object ที่อธิบายประเภทของอินสแตนซ์ใน runtime (runtime type) ใช้สำหรับ reflection, debugging, serialization, และ type checking ตัวอย่างโค้ดที่ 1: สตริง using System; public class Demo { public static void Main() {

  2. การได้ประเภทของสมาชิก Tuple ใน C#

    ใน C# คลาส Tuple มีเมธอด GetType() ที่คืนค่า Type object แสดง signature ของ tuple รวมถึง nested tuple ได้ เมธอดนี้มาจาก object.GetType() แสดงประเภทที่แท้จริงของอินสแตนซ์ ตัวอย่างโค้ดที่ 1: Nested Tuple using System; public class Demo { public static void Main(string[] args) { var t

  3. การได้รหัสเฉพาะของ Managed Thread ปัจจุบันใน C#

    ใน C# property ManagedThreadId ของคลาส Thread คืนค่ารหัสตัวเลขเฉพาะ (unique identifier) ของ managed thread ปัจจุบัน ใช้สำหรับการ debug, logging, หรือติดตามเธรดในระบบ multi-threaded ตัวอย่างโค้ดที่ 1: Thread ธรรมดา และ Thread Pool using System; using System.Threading; public class Demo { public

  4. วิธีเพิ่มคีย์/ค่าใน SortedList ใน C#

    SortedList (ใน System.Collections) เป็นคอลเลกชันคีย์-ค่าที่จัดเก็บเรียงลำดับตามคีย์ ใช้เมธอด Add(key, value) เพิ่มข้อมูล คีย์ต้องไม่ซ้ำกัน ตัวอย่างโค้ดที่ 1: เพิ่มข้อมูลและดู Keys/Values using System; using System.Collections; public class Demo { public static void Main(string[] args) {

  5. วิธีตรวจสอบสถานะปัจจุบันของ Thread ใน C#

    ใน C# คลาส Thread (ใน System.Threading) มี property ThreadState ที่คืนค่าสถานะปัจจุบันของเธรดเป็น enum ThreadState ซึ่งเป็น flags (สามารถรวมสถานะได้หลายค่า) สถานะหลักได้แก่ Unstarted, Running, WaitSleepJoin, Suspended, Aborted, Stopped ฯลฯ ตัวอย่างโค้ดที่ 1: เธรดที่ยังไม่เริ่ม using System; using S

  6. วิธีสร้าง SortedSet ใน C#

    SortedSet<T> (ใน System.Collections.Generic) เป็นคอลเลกชันที่เก็บค่าที่ ไม่ซ้ำกัน (unique) และ เรียงลำดับอัตโนมัติ (sorted) ใช้ Red-Black Tree ด้านใน ให้การเพิ่ม/ลบ/ค้นหา ใน O(log n) ตัวอย่างโค้ดที่ 1: สร้างและวนลูปด้วย Enumerator using System; using System.Collections.Generic; public class

  7. เพิ่มอ็อบเจ็กต์ท้าย ArrayList ใน C#

    ArrayList ใช้เมธอด Add(object) เพิ่มอ็อบเจ็กต์ที่ท้ายของลิสต์ เนื่องจากเป็น non-generic จึงรับพารามิเตอร์เป็น object สามารถเพิ่มได้ทุกประเภท (value types จะถูก boxing) ตัวอย่างโค้ดที่ 1: เพิ่มสตริง using System; using System.Collections; public class Demo { public static void Main(string[] arg

  8. การดำเนินการ XOR แบบบิตวایزระหว่าง BitArray ใน C#

    BitArray ใน C# รองรับการดำเนินการบิตวایز (bitwise operations) รวมถึง Xor() (exclusive OR) ซึ่งเปรียบเทียบบิตแต่ละตำแหน่งของสอง BitArray คืนค่า true ก็ต่อเมื่อบิตต่างกัน (true/false หรือ false/true) และ false ก็ต่อเมื่อบิตเหมือนกัน ตัวอย่างโค้ดที่ 1 using System; using System.Collections; public cl

  9. เพิ่มคีย์และค่าใน StringDictionary ใน C#

    StringDictionary เป็นคลาสใน System.Collections.Specialized ที่ออกแบบมาสำหรับเก็บคีย์-ค่าที่ทั้งคู่เป็นสตริง (string key, string value) ใช้เมธอด Add(key, value) ในการเพิ่มข้อมูล คีย์จะถูกแปลงเป็น lowercase อัตโนมัติ ตัวอย่างโค้ด using System; using System.Collections; using System.Collections.Specia

  10. การได้คีย์จาก SortedList ใน C#

    SortedList มี property Keys ที่คืนค่า ICollection ของคีย์ทั้งหมด เรียงลำดับตามคีย์โดยอัตโนมัติ นอกจากนี้ยังมี Values สำหรับค่า และ GetKeyList()/GetValueList() ที่คืน IList ตัวอย่างโค้ด: ดึงคีย์และค่าด้วย IndexOfKey using System; using System.Collections; public class Demo { public static void

  11. ตรวจสอบว่า SortedList เป็น Read-Only หรือไม่ใน C#

    SortedList มี property IsReadOnly ที่บอกว่าอินสแตนซ์นั้นเป็น read-only หรือไม่ โดยค่าเริ่มต้น SortedList ไม่ใช่ read-only (IsReadOnly = false) สามารถเพิ่ม/ลบ/แก้ไขข้อมูลได้เสมอ ตัวอย่างโค้ด using System; using System.Collections; public class Demo { public static void Main(string[] args) {

  12. ลบรายการแรกที่พบใน ArrayList ใน C#

    ใน C# คลาส ArrayList มีเมธอด Remove(object) ที่ลบ รายการแรกที่พบ (first occurrence) ของอ็อบเจ็กต์ที่ระบุ คืนค่า void หากไม่พบรายการนั้นก็ไม่เกิดอะไรขึ้น (ไม่โยน exception) ตัวอย่างโค้ด: ลบจาก ArrayList ที่มีค่าซ้ำ using System; using System.Collections; public class Demo { public static void M

  13. ลบสมาชิกที่ระบุออกจาก List ใน C#

    ใน C# คลาส List<T> มีเมธอด Remove(T item) สำหรับลบ ครั้งแรกที่พบ ของค่าที่ระบุ คืนค่า true หากลบสำเร็จ false หากไม่พบค่านั้น นอกจากนี้ยังมี RemoveAt(index) ลบตามดัชนี และ RemoveAll(Predicate<T>) ลบตามเงื่อนไข ตัวอย่างโค้ด: ลบสตริงออกจาก List using System; using System.Collections.Generi

  14. ตรวจสอบว่า Hashtable ถูกซิงโครไนซ์หรือไม่ใน C#

    Hashtable มี property IsSynchronized ที่บอกว่าอินสแตนซ์นั้นเป็น thread-safe (ซิงโครไนซ์) หรือไม่ โดยค่าเริ่มต้น Hashtable ไม่ได้**ถูกซิงโครไนซ์** (IsSynchronized = false) แต่สามารถสร้าง wrapper ที่ thread-safe ได้ด้วย Hashtable.Synchronized() ตัวอย่างโค้ด: สร้าง Hashtable ธรรมดา และ wrapper ที่ซิงโ

  15. ตรวจสอบว่า SortedDictionary มีคีย์ที่ระบุหรือไม่ใน C#

    SortedDictionary<TKey, TValue> เป็นคอลเลกชันคีย์-ค่าที่จัดเก็บข้อมูลเรียงลำดับตามคีย์ (sorted by key) ใช้เมธอด ContainsKey(key) เพื่อตรวจสอบว่ามีคีย์ที่ระบุอยู่หรือไม่ คืนค่า true หรือ false ตัวอย่างโค้ด: คีย์เป็นตัวเลข using System; using System.Collections; using System.Collections.Generic;

  16. เพิ่มสมาชิกใน List ใน C#

    ใน C# คลาส List<T> (อยู่ใน System.Collections.Generic) เป็นคอลเลกชันแบบเจเนอริกที่นิยมใช้มากที่สุด มีเมธอด Add() สำหรับเพิ่มสมาชิกที่ท้ายของลิสต์ ทำงานในเวลา O(1) เฉลี่ย (amortized constant time) ตัวอย่างโค้ด: เพิ่มสตริง using System; using System.Collections.Generic; public class Demo {

  17. เพิ่มข้อมูลท้าย ArrayList ใน C#

    ใน C# คลาส ArrayList (อยู่ใน System.Collections) มีเมธอด Add() สำหรับเพิ่มรายการเดี่ยวที่ท้าย และ AddRange() สำหรับเพิ่มหลายรายการจากคอลเลกชันอื่นๆ พร้อมกัน ตัวอย่างโค้ด: ใช้ Add() และ AddRange() using System; using System.Collections; public class Demo { public static void Main(string[] args)

  18. เพิ่มโหนดหรือค่าท้าย LinkedList ใน C#

    ใน C# คลาส LinkedList<T> มีเมธอด AddLast() สำหรับเพิ่มโหนดใหม่หรือค่าใหม่ที่ท้ายของลิงก์ลิสต์ นี่คือวิธีมาตรฐานในการสร้างลิงก์ลิสต์โดยเพิ่มข้อมูลทีละรายการ ตัวอย่างโค้ด: เพิ่มสตริง using System; using System.Collections.Generic; public class Demo { public static void Main() {

  19. ตรวจสอบว่า HybridDictionary มีขนาดคงที่หรือไม่ใน C#

    HybridDictionary เป็นคลาสใน namespace System.Collections.Specialized ที่ทำงานเหมือน ListDictionary เมื่อข้อมูลน้อย และเปลี่ยนเป็น Hashtable อัตโนมัติเมื่อข้อมูลเยอะขึ้น (เกินประมาณ 9 รายการ) Property IsFixedSize จะคืนค่า false เสมอ เพราะ HybridDictionary ขยายตัวได้ตลอดเวลา ตัวอย่างโค้ด using System

  20. การได้จำนวนโหนดใน LinkedList ใน C#

    ใน C# คลาส LinkedList<T> มี property Count ที่คืนค่าจำนวนโหนด (nodes) ที่อยู่ในลิสต์ปัจจุบัน นี่เป็นวิธีที่เร็วที่สุดและตรงไปตรงมาที่สุดในการทราบขนาดของลิงก์ลิสต์ ตัวอย่างโค้ด using System; using System.Collections.Generic; public class Demo { public static void Main() { Link

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:108/134  20-คอมพิวเตอร์/Page Goto:1 102 103 104 105 106 107 108 109 110 111 112 113 114