Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. ความแตกต่างระหว่าง int และ Int32 ใน C#

    คำตอบสั้น: เหมือนกันทุกประการ (Alias) int = C# Keyword (Alias) Int32 = .NET Framework Type (System.Int32) Compile เป็น IL Code เดียวกันหมด Runtime ไม่มีความแตกต่างใดๆ การประกาศ // ทั้งคู่เท่ากัน Int32 x = 5; // ใช้ต้อง using System; หรือ System.Int32 int y = 5; // C# Keyword // Type Check

  2. ?? (Null-Coalescing Operator) ใน C# คืออะไร

    Null-Coalescing Operator (??) คืนค่า Left Operand ถ้าไม่ใช่ Null มิฉะนั้นคืนค่า Right Operand result = left ?? right; // เทียบเท่า: result = (left != null) ? left : right; คุณสมบัติ Short-circuit: ถ้า Left ไม่ Null → ไม่ Evaluate Right ทำงานกับ Nullable Value Types (int?, DateTime?) และ Referenc

  3. @ หน้า String ใน C# (Verbatim String) คืออะไร

    @ (Verbatim Identifier) คืออะไร? Prefix @ หน้า String Literal ทำให้เป็น Verbatim String - Compiler จะอ่าน String ตรงตัว ไม่ประมวลผล Escape Sequences คุณสมบัติหลัก Ignore Escape Sequences: \n, \t, \\, \ → เป็นตัวอักษรตรงตัว รองรับ Multi-line String (Line Breaks ตรงๆ) Double Quote ภายใน: เขียน (Dou

  4. Access Modifiers ใน C# มีกี่แบบ ต่างกันอย่างไร

    Access Modifiers 6 ระดับ ควบคุม Visibility/Scope ของ Type และ Member ModifierScopeเข้าถึงได้จาก publicEverywhereAssembly ใดๆ, Derived Class ใดๆ privateContaining Type OnlyClass/Struct เดียวกันเท่านั้น protectedContaining + DerivedClass เดียวกัน + Derived Class (ทุก Assembly) internalCurrent Assemb

  5. ชนิดของ Parameters ใน Method C# มีกี่แบบ

    Parameter คืออะไร? ค่าที่ส่งเข้า Method เพื่อให้ Method ทำงาน ตาม Signature ที่กำหนด 3 ประเภทหลัก ประเภทKeywordทิศทางข้อมูลต้อง Initialize ก่อนส่งใช้เมื่อไหร่ Value Parameter(ไม่มี)In (Copy Value)YesGeneral, Primitive Types Reference ParameterrefIn/Out (Reference)YesModify Callers Variable Output

  6. String vs StringBuilder ใน C# ใช้ตัวไหนดี

    ความแตกต่างหลัก: Mutability คุณสมบัติString (System.String)StringBuilder (System.Text.StringBuilder) NatureImmutable (ไม่เปลี่ยนแปลงได้)Mutable (เปลี่ยนแปลงได้) Concatenationสร้าง Object ใหม่ทุกครั้งแก้ไขใน Buffer เดียว MemoryAllocate ใหม่ทุกครั้งResizable Buffer (Capacity) Performanceช้าเมื่อ Conc

  7. ทำไม Interface ใน C# ไม่สามารถระบุ Access Modifier ได้

    เหตุผลหลัก: Interface เป็น Contract (สัญญา) Interface กำหนด ว่าต้องทำอะไรได้บ้าง ไม่ใช่ ทำอย่างไร วัตถุประสงค์: ให้ Type อื่นเข้าถึง Class/Struct ผ่าน Contract นี้ Member ทั้งหมดจึงเป็น Public โดยปริยาย (Implicitly Public) การใส่ Access Modifier จะขัดกับหลักการ Public Contract ประวัติความเปลี่ยนแ

  8. วิธี Return หลายค่าจาก Function ใน C#

    4 วิธีหลักในการ Return Multiple Values วิธีเหมาะสำหรับข้อดีข้อเสีย Tuple (ValueTuple)General Purpose, 2-7 ValuesClean Syntax, Named Members, DeconstructionMutable Struct, Limited Serialization out ParametersTryParse Pattern, LegacyClear Intent, No AllocationVerbose, Must Initialize ref ParametersMo

  9. วิธีให้ Auto-Property มี Default Value ใน C#

    Auto-Property คืออะไร? Property ที่ Compiler สร้าง Backing Field ให้อัตโนมัติ Syntax: public Type Name { get; set; } วิธีตั้ง Default Value 1. C# 6.0+ Property Initializer (แนะนำ) public class Demo { // Inline Initializer - สะดวก อ่านง่าย public string FirstName { get; set; } = Default Na

  10. เมื่อไหร่ควรใช้ Tuple ใน C#

    Tuple คืออะไร? โครงสร้างข้อมูลที่จัดกลุ่มหลายค่า (Multiple Values) ไว้ใน Object เดียว โดยไม่ต้องสร้าง Class/Struct Return หลายค่าจาก Method (แทน ref/out) Pass หลายค่าเข้ Method ผ่าน Parameter เดียว Nesting ได้ (Tuple ใน Tuple) C# 7+: ValueTuple (Struct) - Performance ดีกว่า Tuple Class เดิม Synta

  11. Static Constructor ใน C# เรียกเมื่อไหร่ และวิธีใช้

    Static Constructor คืออะไร? Constructor מיוחדสำหรับ Initialize Static Members หรือ ทำ Action ที่ต้องทำครั้งเดียว เมื่อไหร่ถูกเรียก? ก่อนสร้าง Instance แรก ของ Class ก่อนเข้าถึง Static Member แรก (Field, Property, Method) รับประกัน: รันครั้งเดียวต่อ AppDomain, Thread-Safe ไม่สามารถคาดเดา/ควบคุม Tim

  12. วิธีเรียก Constructor อีกตัวใน Class เดียวกัน (Constructor Chaining) ใน C#

    Constructor Chaining คืออะไร? การเรียก Constructor อีกตัวหนึ่งใน Class เดียวกัน เพื่อ Reuse Code ลด Duplication ใช้ Keyword: this() ต้องอยู่ใน Constructor Initializer (หลัง Parameter List ก่อน Body) Syntax public MyClass(int a, int b) : this(a) // เรียก Constructor ที่รับ 1 Parameter { // B

  13. พิสูจน์ว่า Static Class สร้าง Instance ได้เพียง 1 ตัวใน C#

    Static Class ใน C# ไม่สามารถ new สร้าง Instance ได้ Member ทั้งหมดต้องเป็น Static Sealed และ Abstract โดยปริยาย (Cannot Inherit, Cannot Instantiate) Constructor: มีได้แค่ Static Constructor (Parameterless, ไม่มี Access Modifier) การพิสูจน์: Static Constructor เรียกเพียงครั้งเดียว using System;

  14. Destructor ใน C# มีได้กี่ตัวใน Class เดียว

    คำตอบสั้น: เพียง 1 ตัวเท่านั้น ใน C# Class หนึ่งสามารถมี Destructor (Finalizer) ได้ สูงสุด 1 ตัว หากประกาศมากกว่า 1 จะเกิด Compile Error คุณสมบัติของ Destructor ชื่อเดียวกับ Class ขึ้นต้นด้วย ~ (Tilde) ไม่มี Parameter, ไม่มี Access Modifier, ไม่มี Return Type ไม่สามารถ Overload ได้ ไม่สามารถเรียกโ

  15. วิธีเรียก Base Class Constructor จาก Child Class ใน C#

    Keyword สำคัญ 2 ตัว this() - เรียก Constructor อีกตัวใน Class เดียวกัน (Constructor Chaining) base() - เรียก Constructor ของ Base Class (Parent) Syntax // เรียก Base Constructor public ChildClass(parameters) : base(baseParameters) { // Child Constructor Body } // เรียก Constructor อีกตัวใน

  16. Build, Rebuild, Clean ใน C# ใช้ตัวไหนดี

    ความแตกต่างของ Build Actions Actionคำอธิบายใช้เมื่อไหร่ BuildIncremental Build - Compile เฉพาะไฟล์ที่เปลี่ยนแปลงDevelopment ปกติ เร็วที่สุด RebuildClean + Build - ลบ Output ทั้งหมด แล้ว Build ใหม่ทั้ง Solutionแก้ปัญหา Cache, Reference Error, Switch Branch Cleanลบไฟล์ Compiled (bin/obj) ทั้งหมด ไม่

  17. Union, Intersect และ Except Operators ใน LINQ C#

    Set Operators ใน LINQ ใช้จัดการ Collections แบบ Set Theory (ทฤษฎีเซต) 1. Union - รวม Unique รวม 2 Collections ลบค่าซ้ำ (Distinct) var result = collection1.Union(collection2); 2. Intersect - หาค่าร่วม คืนค่าที่มีใน ทั้งสอง Collections var result = collection1.Intersect(collection2); 3. Except -

  18. Explicit Interface Implementation ใน C# คืออะไร และเมื่อไหร่ใช้

    Explicit Interface Implementation คืออะไร? เมื่อ Class Implement Interface 2 ตัวที่มี Member ชื่อและ Signature เหมือนกัน ใช้ Explicit Implementation เพื่อแยก Implementation แยกตาม Interface Syntax: InterfaceName.MemberName Member นี้ ไม่เป็น Public ของ Class เข้าถึงได้ เฉพาะผ่าน Interface Reference

  19. ความแตกต่างระหว่าง Finalize และ Dispose ใน C#

    Finalize (Finalizer/Destructor) Method: ~ClassName() (Syntax C#) ถูกเรียกโดย Garbage Collector (GC) ก่อน Reclaim Memory ไม่รับประกันเวลา (Non-deterministic) ใช้ Cleanup Unmanaged Resources (File Handle, DB Connection, Native Pointer) เป็นของ Object Class ไม่ควบคุมได้ เรียกบังคับได้ด้วย GC.Collect()

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:121/134  20-คอมพิวเตอร์/Page Goto:1 115 116 117 118 119 120 121 122 123 124 125 126 127