Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. Index-based I/O ArrayList Collection ใน C#

    ArrayList Class ArrayList เป็น Non-Generic Collection ที่เก็บ Object ใดๆ ได้ ขนาด Dynamic (เพิ่ม/ลบได้) เข้าถึงแบบ Index-Based เหมือน Array - อยู่ใน System.Collections ⚠️ หมายเหตุ: ใน C# สมัยใหม่ ควรใช้ List<T> แทน ArrayList เพื่อ Type Safety และ Performance (No Boxing/Unboxing) Properties

  2. Index-based I/O BitArray Collection ใน C#

    BitArray Class BitArray จัดการ Compact Array ของค่า Bit (Boolean) - true = 1 (On), false = 0 (Off) ใช้ namespace System.Collections คุณสมบัติ (Properties) Property คำอธิบาย Count จำนวน Bit ทั้งหมด IsReadOnly เป็น Read-Only หรือไม่ Item[int index] Get/Set ค่า Bit ที่ Index (Indexer) Len

  3. Final Local Variables ใน C# (readonly)

    คำตอบ: ใช้ readonly Keyword C# ไม่มีคีย์เวิร์ด final เหมือน Java แทนนั้นใช้ readonly สำหรับ Field/Variable ที่ต้องการให้กำหนดค่าได้ครั้งเดียว (ระหว่าง Declaration หรือใน Constructor) readonly vs const คุณสมบัติ const readonly กำหนดค่าเมื่อไหร่ Compile Time (ตอนประกาศ) Runtime (ใน Constructor

  4. Default Access Modifier สำหรับ Class Member ใน C#

    คำตอบ: Private Default Access Modifier สำหรับ Class Member ใน C# คือ private หมายความว่า ถ้าไม่ระบุ Access Modifier สมาชิก (Fields, Methods, Properties, Events, Nested Types) จะเป็น Private อัตโนมัติ - เข้าถึงได้เฉพาะภายในคลาสเดียวกันเท่านั้น ตัวอย่าง เป็น private อัตโนมัติ double length;

  5. วิธีคัดลอก List Collection ไปยัง Array ใน C#

    วิธีหลัก: ใช้ CopyTo() Method คัดลอกข้อมูลจาก List ไปยัง Array ที่สร้างไว้ล่วงหน้า ขั้นตอน สร้าง List และเพิ่มข้อมูล สร้าง Array ขนาด ≥ List.Count เรียก list.CopyTo(array) โค้ดตัวอย่างสมบูรณ์ using System; using System.Collections.Generic; using System.Linq; public class Demo { public stati

  6. ความแตกต่างระหว่าง List Collection และ Array ใน C#

    Array (อาเรย์) เก็บข้อมูลชนิดเดียวกัน ขนาดคงที่ (Fixed Size) ต้องระบุขนาดตอนสร้าง การประกาศ Array // ประกาศพร้อมขนาด int[] arr = new int[5]; // ประกาศพร้อมค่าเริ่มต้น int[] arr2 = new int[5] { 4, 8, 5, 2, 9 }; int[] arr3 = { 4, 8, 5, 2, 9 }; // Shorthand // หลายมิติ int[,] matrix = new int[3, 3];

  7. ความแตกต่างระหว่าง Multi-dimensional Array และ Jagged Array ใน C#

    Multi-dimensional Array (Rectangular Array) อาเรย์หลายมิติที่มีขนาดคงที่ในทุกมิติ (Rectangular) - ทุกแถวมีจำนวนคอลัมน์เท่ากัน Syntax และการประกาศ // 2 มิติ (2D) - 3 แถว 4 คอลัมน์ int[,] matrix = new int[3, 4]; // 3 มิติ (3D) - 2x3x4 int[,,] cube = new int[2, 3, 4]; // Initialization int[,] matrix

  8. Numeric Promotion ใน C# (Arithmetic Operators)

    Numeric Promotion ใน Arithmetic Operators: Operand เล็กถูก Promote ให้เป็น Type ใหญ่ก่อนทำ Operation ตัวอย่าง: short * ushort → int using System; class Program { static void Main() { short val1 = 99; // 16-bit Signed ushort val2 = 11; // 16-bit Unsigned

  9. Numeric Promotion ใน Conditional Expression (C#)

    Numeric Promotion = การแปลง Type เล็ก → Type ใหญ่ อัตโนมัติ ใน Expression เพื่อป้องกัน Overflow และความสูญเสีย Precision Conditional Expression (Ternary Operator) condition ? trueExpression : falseExpression Type ของ Expression ทั้งสองข้างต้อง **Compatible** — Compiler จะหา **Best Common Type** และ

  10. Generics vs Non-Generics Collections ใน C#

    C# มี Collections 2 แบบ: Non-Generic (Legacy) และ Generic (Modern) Non-Generic Collections (System.Collections) เก็บ object → Boxing/Unboxing, No Type Safety ClassDescription ArrayListDynamic Array of Objects HashtableKey-Value (Object-Object) QueueFIFO Queue of Objects StackLIFO Stack of Objec

  11. คีย์เวิร์ด internal ใน C#

    internal = Access Modifier ที่จำกัด Visibility ภายใน **Assembly เดียวกัน** (Project/Module ที่ Compile ร่วมกัน) Accessibility Levels Modifierเข้าถึงได้จาก privateภายใน Class/Struct เดียวกัน private protectedภายใน Class + Derived Classes ใน Assembly เดียวกัน internal**ทุกที่ใน Assembly เดียวกัน*

  12. คีย์เวิร์ด params ใน C# (Variable Arguments)

    params ใช้เมื่อจำนวน Arguments ไม่แน่ชัด — รับ Array ของ Arguments ได้หลายตัว Syntax ReturnType MethodName(params Type[] parameterName) { } กฎ params ต้องเป็น **Parameter ตัวสุดท้าย** เท่านั้น มีได้ **เพียง 1 params ต่อ Method** Array Type ต้องเป็น Single-dimensional Caller สามารถส่ง: Comma-separ

  13. การเปรียบเทียบ Tuple ใน C# (C# 7.3+)

    ตั้งแต่ C# 7.3 Tuple รองรับ Structural Equality Comparison — เปรียบเทียบค่าแต่ละ Element แทน Reference Tuple Syntax (C# 7+) // Named Tuple Elements var person1 = (Name: John, Age: 30, City: NYC); var person2 = (Name: John, Age: 30, City: NYC); var person3 = (Name: Jane, Age: 25, City: LA); // Impl

  14. ทำไม Array Index ใน C# เริ่มที่ 0 (Zero-based Indexing)

    Zero-based Indexing มาจากแนวคิด Pointer Arithmetic และ Memory Offset — ไม่ใช่การนับลำดับ (Ordinal) Memory Model Explanation Array เป็น Contiguous Memory Block แต่ละ Element มีขนาดเท่ากัน // สมมติ int[] เริ่มที่ Address 0xFF000, int = 4 bytes // Elements: {35, 23, 67, 88, 90} Address Value 0xFF0

  15. คีย์เวิร์ด final ใน C# (sealed)

    C# ไม่มี final — ใช้ sealed แทน สำหรับ Class และ Method 1. sealed Class — ห้ามสืบทอด (Inherit) public sealed class FinalClass { public void DoSomething() { } } // class Derived : FinalClass { } // ❌ Compile Error: Cannot derive from sealed class ใช้เมื่อ: Class เสร็จสมบูรณ์ ไม่ควรขยาย, Securi

  16. การกลับลำดับ Array (Reverse Array) ใน C#

    ใช้ Array.Reverse (Static Method) กลับลำดับ Element ใน Array เดียว (In-place) หรือสร้าง Array ใหม่ด้วย LINQ 1. Array.Reverse (In-place, Standard) int[] arr = { 9, 32, 87, 45, 77, 56 }; Array.Reverse(arr); // Mutates Original Array // arr = { 56, 77, 45, 87, 32, 9 } Overloads Array.Reverse(arr);

  17. HashSet ใน C# (C# Set Collection)

    HashSet<T> เป็น Optimized Set Collection — เก็บค่า Unique, Fast Lookup O(1), ไม่รักษาลำดับ ใช้ System.Collections.Generic คุณสมบัติหลัก ✅ Unique Elements (No Duplicates) ✅ O(1) Average: Add, Remove, Contains ✅ Set Operations: Union, Intersect, Except, SymmetricExcept ❌ No Indexer (ไม่ได้ด้วย

  18. Final Variables ใน C# (readonly / const)

    C# ไม่มี final เหมือน Java — ใช้ readonly (Runtime Constant) หรือ const (Compile-time Constant) แทน const — Compile-time Constant ค่าต้องทราบตอน Compile (Literal หรือ Constant Expression) Implicit static — เข้าถึงผ่าน Class Name Value Types และ String เท่านั้น ไม่สามารถเปลี่ยนได้เลย public class

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:27/134  20-คอมพิวเตอร์/Page Goto:1 21 22 23 24 25 26 27 28 29 30 31 32 33