Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. Array Copy ใน C#

    ใช้ Array.Copy() คัดลอกส่วนของอาร์เรย์ไปยังอีกอาร์เรย์หนึ่ง Syntax Array.Copy(sourceArray, sourceIndex, destArray, destIndex, length); sourceArray - อาร์เรย์ต้นทาง sourceIndex - เริ่มต้นที่ Index นี้ในต้นทาง destArray - อาร์เรย์ปลายทาง destIndex - เริ่มต้นที่ Index นี้ในปลายทาง length - จำนวนสมาชิก

  2. Association, Composition และ Aggregation ใน C#

    Association (การเชื่อมโยง) ความสัมพันธ์ระหว่างออบเจ็กต์ทั่วไป สามารถเป็น One-to-One, One-to-Many, Many-to-Many ตัวอย่าง: Employee ↔ Project (Employee หลายคนทำ Project หนึ่ง, Project หนึ่งมี Employee หลายคน) Composition (การรวมส่วน - Strong Ownership) Parent เป็นเจ้าของ Child อย่างแท้จริง ถ้า Parent

  3. Async และ Await ใน C#

    async/await ใช้สำหรับ Asynchronous Programming ทำให้โค้ด Non-blocking เขียนง่าย อ่านง่าย คล้าย Synchronous code ปัญหาของ Synchronous Code (GUI App) // Blocking - UI ค้างจน Download เสร็จ private void OnRequestDownload(object sender, RoutedEventArgs e) { var req = HttpWebRequest.Create(_requested

  4. Boxing และ Unboxing ใน C#

    Boxing (การบรรจุ) การแปลง Value Type → Reference Type (Object) แบบ Implicit (อัตโนมัติ) int val = 12; object boxed = val; // Boxing: คัดลอกค่า 12 ไป Heap, boxed ชี้ไปที่นั่น กระบวนการ: จองหน่วยความจำบน Heap → คัดลอกค่า Value type ไป → Reference ชี้ไปที่ Object บน Heap Unboxing (การแปรงกลับ) การแปลง

  5. Background และ Foreground Thread ใน C#

    Thread คือเส้นทางการทำงานของโปรแกรม แต่ละ Thread มี Flow of control แยกกัน Foreground Threads ทำงานจนกว่าจะเสร็จ หรือถูก Abort Application จะไม่ปิดจนกว่า Foreground threads ทุกตัวจะจบ Default: Thread ที่สร้างด้วย new Thread() เป็น Foreground Background Threads จะถูก Terminate อัตโนมัติเมื่อ Foreg

  6. BackgroundWorker Class ใน C#

    BackgroundWorker ใช้รันงานหนักใน Background thread ไม่บล็อก UI (Windows Forms) สื่อสารกับ UI thread ผ่าน Events Properties สำคัญ Propertyคำอธิบาย CancellationPendingTrue ถ้ามีการร้องขอยกเลิก (CancelAsync()) IsBusyTrue ถ้ากำลังรัน Async operation WorkerReportsProgressTrue = รองรับ ReportProgress(

  7. โปรแกรม Calculator แบบ Basic ใน C# (Web Forms)

    ตัวอย่าง Calculator ใช้ Web Forms (ASP.NET) มีปุ่ม 0-9 และตัวดำเนินการ +, -, ×, ÷ โครงสร้างหลัก TextBox แสดงผล (tbResult) ตัวแปรเก็บค่า: x (operand แรก), y (operator), z (operand ที่สอง) Event handlers สำหรับปุ่มต่างๆ โค้ดสำหรับปุ่มตัวดำเนินการ static float x, y; // x = operand1, y = operator char

  8. BigInteger Class ใน C#

    BigInteger ใช้จัดการเลขจำนวนเต็มขนาดใหญ่ไม่จำกัดขนาด (Limited by memory only) Namespace: System.Numerics (ต้อง Add Reference: System.Numerics.dll) Syntax [Serializable] public struct BigInteger : IFormattable, IComparable, IComparable, IEquatable Constructors หลัก Constructorคำอธิบาย BigInteger(

  9. Binary Search ใน C#

    Binary Search ค้นหาในอาร์เรย์ที่ เรียงลำดับแล้ว โดยแบ่งครึ่งช่วงค้นหาเรื่อยๆ Complexity Worst-caseO(log n) Best-caseO(1) AverageO(log n) SpaceO(1) อัลกอริทึม public static object BinarySearch(int[] arr, int key) { int min = 0; int max = arr.Length - 1; while (min <= max) { in

  10. แปลง Binary เป็น Decimal ใน C#

    แปลงเลขฐาน 2 เป็นฐาน 10 ด้วย While loop คูณกำลังของ 2 อัลกอริทึม 0) { remainder = val % 10; // หาบิตสุดท้าย decimal = decimal + remainder * baseVal; // บวกค่า val = val / 10; // ตัดบิตสุดท้าย baseVal = baseVal * 2; // กำลังถัดไป } ตัวอย่างโค้ดเต็ม 0) {

  11. ตัวดำเนินการ Bitwise Right Shift ใน C#

    Bitwise Right Shift (>>) ขยับบิตของอเปรานด์ซ้ายไปทางขวา จำนวนบิตตามอเปรานด์ขวา ตัวอย่าง: 60 (0011 1100) >> 2 = 15 (0000 1111) โค้ดตัวอย่าง 2; // 0000 1111 = 15 Console.WriteLine(Value of b is {0}, b); Console.ReadLine(); } } } คำอธิบาย: การขยับขวา n บ

  12. โปรแกรม C# ตรวจสอบ Binary Representation เป็น Palindrome

    ตรวจสอบว่าการแทนค่าบाइनนารีของเลขเป็น Palindrome (อ่านหน้าหลังเหมือนกัน) หรือไม่ ตัวอย่าง: 5 = 101 (Binary) → Palindrome เพราะ 101 อ่านกลับก็ได้ 101 อัลกอริทึม (Bitwise Reverse) 0) { rev = 1; // ขยับขวา num } return rev; } bool IsBinaryPalindrome(long num) { return

  13. โปรแกรม C# ตรวจสอบ 1 ติดกัน K ตัวในเลขฐานสอง

    ตรวจสอบว่ามีบิต 1 ติดกัน K ตัวในการแทนค่าบाइनนารีหรือไม่ ใช้ bool array แทนบิต (true=1, false=0) อัลกอริทึม int count = 0, maxCount = 0; foreach (bool bit in bits) { if (bit) { count++; maxCount = Math.Max(maxCount, count); } else { count = 0; // รีเซ็ตเมื่อเจอ 0

  14. โปรแกรม C# แปลง Integer เป็น String

    ใช้เมธอด ToString() แปลง integer เป็น string int num = 299; string s = num.ToString(); ตัวอย่างโค้ดเต็ม using System; class MyApplication { static void Main(string[] args) { string s; int num = 299; s = num.ToString(); Console.WriteLine(String = + s); Con

  15. โปรแกรม C# ตรวจสอบสถานะ Current Thread

    ใช้ property IsAlive ของคลาส Thread ตรวจสอบว่าเธรดยังทำงานอยู่หรือไม่ ตัวอย่างโค้ดเต็ม using System; using System.Threading; namespace Demo { class MyClass { static void Main(string[] args) { Thread thread = Thread.CurrentThread; thread.Name = My New Thread;

  16. โปรแกรม Bubble Sort ใน C#

    Bubble Sort คืออัลกอริทึมจัดเรียงง่ายๆ เปรียบเทียบคู่ที่อยู่ติดกัน สลับที่ถ้าอยู่ผิดลำดับ ตัวอย่างขั้นตอน (Array: 78, 55, 45, 98, 13) 78 ไม่สลับ → 98↔13 → 55,45,78,13,98 ทำซ้ำจนเรียบร้อย → 13, 45, 55, 78, 98 ตัวอย่างโค้ดเต็ม using System; namespace BubbleSort { class MySort { static void

  17. C# และ Multiple Inheritance

    C# ไม่รองรับ Multiple Inheritance (สืบทอดจากหลาย Base class) โดยตรง ใช้ Interfaces แทน รูปแบบการใช้ Interface แทน Multiple Inheritance สร้าง Interface ที่มีเมธอดที่ต้องการ Class ให้ Implement Interface นั้นๆ ได้หลาย Interface ตัวอย่างโค้ดเต็ม using System; namespace MyInheritance { class Shape

  18. Anonymous Methods ใน C#

    Anonymous Methods (เมธอดไร้ชื่อ) ใช้ส่งบล็อกโค้ดเป็นพารามิเตอร์ของ Delegate โดยไม่ต้องประกาศเมธอดแยก รูปแบบการประกาศ delegate void NumberChanger(int n); // ... NumberChanger nc = delegate(int x) { Console.WriteLine(Anonymous Method: {0}, x); }; ตัวอย่างโค้ดเต็ม using System; delegate void Number

  19. ตัวอย่าง C# Hierarchical Inheritance

    Hierarchical Inheritance (การสืบทอดแบบลำดับชั้น) คือมี Derived class หลายตัวสืบทอดจาก Base class เดียว โครงสร้าง Father (Base) / \ Son Daughter (Derived classes) ตัวอย่างโค้ดเต็ม using System; using System.Collections.Generic; using System.Linq; using System.Text; na

  20. โปรแกรม C# ตรวจสอบ Substring ในสตริง

    ใช้เมธอด Contains() ตรวจสอบว่าสตริงมี substring ที่ต้องการหรือไม่ string str1 = United; string str2 = Uni; bool result = str1.Contains(str2); // true ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() { string str1 = United, str2 = Uni; bool res = str1.C

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:2/134  20-คอมพิวเตอร์/Page Goto:1 2 3 4 5 6 7 8