Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. วิธีคำนวณ Average ของชุดค่าใน C#

    ขั้นแรก กำหนด Array ที่มีค่า: int[] myArr = new int[] { 34, 23, 77, 67 }; เพื่อหาค่าเฉลี่ย (Average) ให้หาผลรวม (Sum) ของ Elements ใน Array ก่อน แล้วหารผลรวมด้วยความยาวของ Array (Length) จะได้ Average int sum = 0; int average = 0; for (int i = 0; i < len; i++) { sum += myArr[i]; } average

  2. ความแตกต่างระหว่างคีย์เวิร์ด VAR และ DYNAMIC ใน C#

    Dynamic เก็บค่าได้ทุก Type ใน Variable ที่สร้างด้วย dynamic Keyword การตรวจสอบ Type (Type Checking) เกิดขึ้นที่ Run-time Dynamic เป็น Dynamically Typed Variables Syntax สำหรับประกาศ Dynamic Type: dynamic <variable_name> = value; ตัวอย่าง: dynamic val1 = 100; dynamic val2 = 5; dynamic val3 =

  3. การเข้าถึง Array Elements ด้วย Pointer ใน C#

    ใน C# ชื่อ Array และ Pointer ที่ชี้ไปยัง Data Type เดียวกับ Array Data ไม่ใช่ Variable Type เดียวกัน เช่น int* p และ int[] p ไม่ใช่ Type เดียวกัน คุณสามารถ Increment Pointer Variable p ได้เพราะมันไม่ Fixed ใน Memory แต่ Array Address Fixed ใน Memory จึง Increment ไม่ได้ ตัวอย่าง using System; name

  4. ความแตกต่างระหว่าง Virtual และ Abstract Method ใน C#

    ภาพรวม ทั้งคู่ใช้สำหรับ Polymorphism (พอลิมอร์ฟิซึม) แต่มีความแตกต่างสำคัญ: คุณสมบัติVirtual MethodAbstract Method Implementationมี implementation ใน base classไม่มี implementation (แค่ signature) OverrideOptional (เลือก override ได้)บังคับ (derived class ต้อง override) Class Typeอยู่ใน class ธร

  5. โปรแกรม C# ตรวจสอบว่าสองคำเป็น Anagram กันหรือไม่

    ภาพรวม Anagram (อะนาแกรม) คือ คำที่ประกอบด้วยตัวอักษรชุดเดียวกัน แต่เรียงลำดับต่างกัน (เช่น heater และ reheat) อัลกอริทึม แปลงทั้งสองสตริงเป็น lowercase (case-insensitive) แปลงเป็น char array จัดเรียง (sort) ทั้งสองอาร์เรย์ เปรียบเทียบว่าเท่ากันหรือไม่ ตัวอย่างโค้ดเต็ม using System; public cla

  6. โปรแกรม C# แปลงอาร์เรย์เป็น List ธรรมดา

    ภาพรวม แปลงอาร์เรย์เป็น List<T> ได้หลายวิธี ง่ายที่สุดคือ Constructor หรือ LINQ ToList() วิธี 1: Constructor (ตัวอย่างต้นฉบับ) int[] arr = { 23, 66, 96, 110 }; var list = new List(arr); // คัดลอกอัตโนมัติ วิธี 2: LINQ ToList() using System.Linq; var list = arr.ToList(); ตัวอย่างโค้ดเต็ม (ว

  7. โปรแกรม C# ตรวจสอบว่าอาร์เรย์มีค่าซ้ำหรือไม่

    ภาพรวม ใช้ Dictionary<T, int> นับจำนวนครั้งที่แต่ละค่าปรากฏ เพื่อหาค่าที่ซ้ำ (duplicate) อัลกอริทึม สร้าง Dictionary เพื่อนับความถี่ วนลูปอาร์เรย์: ถ้าคีย์มีอยู่แล้ว เพิ่มนับ, ไม่มีเพิ่มใหม่ = 1 1 คือค่าซ้ำ ตัวอย่างโค้ดเต็ม using System; using System.Collections.Generic; namespace Demo

  8. โค้ด Unsafe/Unmanaged ใน C# คืออะไร

    ภาพรวม Unmanaged code = โค้ดที่ ไม่อยู่ภายใต้การจัดการของ CLR (Common Language Runtime) เช่น C/C++ native code Unsafe code ใน C# = โค้ดบล็อกที่ใช้ pointer (ตัวชี้) และต้องคอมไพล์ด้วยตัวเลือก /unsafe อนุญาตให้เข้าถึงหน่วยความจำโดยตรง ตัวอย่าง Unsafe Code using System; class UnsafeCode { static

  9. ความแตกต่างระหว่าง Dictionary และ Hashtable ใน C#

    ภาพรวม Dictionary<TKey, TValue> (generic) เร็วกว่าและปลอดภัยกว่า Hashtable (non-generic) สำหรับ strongly-typed collections Hashtable Non-generic: เก็บ object key และ value ต้อง boxing/unboxing สำหรับ value types ไม่ type-safe - compile time ไม่จับ error ได้ Thread-safe สำหรับ single writer

  10. สิทธิ์การเข้าถึงไฟล์ (File Permissions) ใน C#

    ภาพรวม คลาส FileIOPermission (namespace System.Security.Permissions) ใช้ควบคุม สิทธิ์การเข้าถึงไฟล์และโฟลเดอร์ ใน .NET Framework (Code Access Security - CAS) คุณสมบัติหลัก คุณสมบัติคำอธิบาย AllFilesสิทธิ์เข้าถึงไฟล์ทั้งหมด AllLocalFilesสิทธิ์เข้าถึงไฟล์โลคัลทั้งหมด เมธอดหลัก เมธอดคำอธิบาย A

  11. คลาส Pair ใน C# (KeyValuePair)

    ภาพรวม KeyValuePair<TKey, TValue> (มักเรียกว่า Pair) ใช้เก็บ คู่คีย์-ค่า เดียว ใช้ภายใน Dictionary<TKey, TValue> และสามารถใช้แยกต่างหากได้ รูปแบบการใช้งาน var list = new List

  12. ขอบเขตของตัวแปร (Scope) ใน C#

    ภาพรวม ขอบเขต (Scope) คือบริเวณของโค้ดที่ตัวแปรสามารถเข้าถึงได้ ใน C# มีระดับหลัก 2 ระดับ 1. ระดับเมธอด (Method Level / Local Variables) ประกาศภายในเมธอด เข้าถึงได้เฉพาะภายในเมธอดนั้น (และ nested scopes) สิ้นสุดเมื่อเมธอดจบ 2. ระดับคลาส (Class Level / Fields / Member Variables) ประกาศภายในคลาส

  13. วิธีเคลียร์หน้าจอคอนโซลใน C# (Console.Clear)

    ภาพรวม เมธอด Console.Clear() ใช้ ล้างหน้าจอคอนโซลและบัฟเฟอร์ ย้ายเคอร์เซอร์ไปมุมบนซ้าย (0,0) ตัวอย่างโค้ดเต็ม using System; class Program { static void Main() { ConsoleColor foreColor = Console.ForegroundColor; ConsoleColor backColor = Console.BackgroundColor; Cons

  14. Append StringBuilder ใน C# (Append)

    ภาพรวม เมธอด Append() ของ StringBuilder ใช้ ต่อข้อความท้าย StringBuilder รองรับทุกชนิดข้อมูล (เรียก ToString() อัตโนมัติ) รูปแบบการใช้งาน StringBuilder sb = new StringBuilder(); for (int j = 0; j < 5; j++) sb.Append(j).Append( ); // chaining ตัวอย่างโค้ดเต็ม using System; using System.Text;

  15. Append StringBuilder ใน C# พร้อมขึ้นบรรทัดใหม่ (AppendLine)

    ภาพรวม เมธอด AppendLine() ของ StringBuilder เพิ่มข้อความ พร้อมขึ้นบรรทัดใหม่ (append newline) โดยอัตโนมัติ รูปแบบการใช้งาน StringBuilder sb = new StringBuilder(); sb.AppendLine(Accessories); sb.AppendLine(); // บรรทัดว่าง sb.AppendLine(Electronics); ตัวอย่างโค้ดเต็ม using System; using Sys

  16. แทนที่สตริงด้วย StringBuilder ใน C# (Replace)

    ภาพรวม เมธอด Replace(oldValue, newValue) ของ StringBuilder แทนที่ ทุกครั้งที่พบ oldValue ด้วย newValue รูปแบบการใช้งาน StringBuilder sb = new StringBuilder(Fitness is important); sb.Replace(important, essential); // Fitness is essential ตัวอย่างโค้ดเต็ม using System; using System.Text; class Dem

  17. เปรียบเทียบเนื้อหาสอง StringBuilder ใน C#

    ภาพรวม เมธอด Equals() ของ StringBuilder เปรียบเทียบ เนื้อหา (content) ไม่ใช่ reference คืน bool ตัวอย่างโค้ดเต็ม using System; using System.Text; class Demo { static void Main() { StringBuilder str1 = new StringBuilder(); str1.Append(Tim).Append(Tom).Append(Henry);

  18. วนลูป StringBuilder ใน C# ด้วย foreach

    ภาพรวม StringBuilder ไม่ implement IEnumerable<char> จึง ไม่สามารถ foreach ได้โดยตรง แต่สามารถวนลูปผ่าน ToString() หรือ indexer ตัวอย่าง: ใช้ foreach กับอาร์เรย์สตริง แล้ว Append เข้า StringBuilder using System; using System.Text; public class Demo { public static void Main() {

  19. ล้าง StringBuilder ใน C# (Clear)

    ภาพรวม เมธอด Clear() ของ StringBuilder ใช้ ล้างเนื้อหาทั้งหมด ทำให้ Length = 0 แต่ Capacity คงเดิม ตัวอย่างโค้ดเต็ม using System; using System.Text; public class Demo { public static void Main() { string[] myStr = { One, Two, Three, Four }; StringBuilder str = new StringBu

  20. เข้าถึงตัวอักษรใน C# StringBuilder

    ภาพรวม ใช้ indexer [] ของ StringBuilder เพื่อ อ่านตัวอักษร ที่ตำแหน่งที่ระบุ (read-only access) ตัวอย่างโค้ดเต็ม using System; using System.Text; public class Demo { public static void Main() { StringBuilder str = new StringBuilder(); str.Append(premium); Console.W

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:46/134  20-คอมพิวเตอร์/Page Goto:1 40 41 42 43 44 45 46 47 48 49 50 51 52