Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. ตรวจสอบว่าเลขเป็น Power of 2 ใน C#

    Power of 2 = 2ⁿ (n = Integer ≥ 0) → 1, 2, 4, 8, 16, 32, 64... ใน Binary มีแค่ Bit เดียวที่เป็น 1: 1 (1), 10 (2), 100 (4), 1000 (8)... วิธี 1: Bitwise Operation (Fastest) 0 && (x & (x - 1)) == 0; } Logic: x & (x-1) จะเป็น 0 ก็ต่อเมื่อ x มี Bit 1 อยู่ตัวเดียว วิธี 2: Loop Division bool IsPowerOfTwo(u

  2. แปลง DateTime เป็น Format YYYYMMDDHHMMSS ใน C#

    ใช้ ToString(yyyyMMddHHmmss) (Case-sensitive) DateTime now = DateTime.Now; string formatted = now.ToString(yyyyMMddHHmmss); // 20200727080325 Format Specifiers Specifierความหมายตัวอย่าง yyyyYear 4 digits2020 MMMonth 01-1207 ddDay 01-3127 HHHour 00-23 (24h)08 mmMinute 00-5903 ssSecond 00-5925 F

  3. สร้างโฟลเดอร์ถ้ายังไม่มีใน C#

    ใช้ Directory.Exists เช็คก่อน แล้ว Directory.CreateDirectory ตัวอย่างพื้นฐาน using System.IO; string folder = @D:\Demo Folder; if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } สร้าง D:\Demo Folder: สร้าง Subfolder ทีเดียวได้เลย string folder = @D:\Demo Folder\Sub Folder\Su

  4. ตรวจสอบว่า String เป็นตัวเลขใน C#

    ใช้ int.TryParse (แนะนำ) หรือ int.Parse int.TryParse - Safe, No Exception string input = 123; int number = 0; if (int.TryParse(input, out number)) { Console.WriteLine($Parsed: {number}); // 123 } else { Console.WriteLine(Not a valid number); } Return bool + out int ไม่ Throw Exception int.P

  5. ความแตกต่าง Func Delegate และ Action Delegate ใน C#

    ทั้งคู่เป็น Delegate ใน Namespace System แตต่างกันที่ Return Type Func<T1, T2, ..., TResult> มี Return Type (TResult) Parameter 0-16 ตัว + 1 Return Last Type Parameter = Return Type Func first + last; string fullName = append(Michael, Jackson); // MichaelJackson Action<T1, T2, ...> **

  6. แปลง Byte Array เป็น Object Stream ใน C#

    ใช้ MemoryStream (ใน System.IO) Wrap Byte Array ให้เป็น Stream MemoryStream stream = new MemoryStream(byteArray); ตัวอย่างอ่าน Byte Array ผ่าน Stream using System; using System.IO; namespace DemoApplication { class Program { static void Main() { byte[] byteArray

  7. Verify Exception ใน Unit Testing C#

    มี 2 วิธีหลักใน MSTest: 1. Assert.ThrowsException (แนะนำ) จับ Exception ได้ ตรวจสอบ Message/Type แบบ Explicit [TestMethod] public void Test_StringAppend_ThrowsException() { var program = new Program(); var ex = Assert.ThrowsException program.StringAppend(Michael, Jackson)); Assert.AreEqu

  8. สร้าง Comma Separated String จาก List ใน C#

    ใช้ string.Join() รวม Array/List ด้วย Separator ที่กำหนด List of String List fruits = new List { banana, apple, mango }; string csv = string.Join(,, fruits); // banana,apple,mango Property จาก List of Objects var students = new List s.Name)); // John,Jack ใช้งานจริง เก็บ Checkbox Values → Join เป็น

  9. แปลง Byte Array เป็น String ใน C#

    String ใน .NET มี Encoding ต้องใช้ Encoding เดียวกันทั้ง Encode/Decode ใช้ Encoding.GetString() using System.Text; byte[] bytes = Encoding.Default.GetBytes(Hello World); string str = Encoding.Default.GetString(bytes); // Hello World Encoding ที่ใช้บ่อย Encoding.UTF8 - มาตรฐานเว็บ (แนะนำ) Encoding.

  10. ดึงค่า Property แบบ Dynamic ด้วย Reflection ใน C#

    ใช้ Reflection** (System.Reflection) เข้าถึง Property ผ่านชื่อ String รันไทม์ ตัวอย่าง Set/Get Property แบบ Dynamic using System; using System.Reflection; namespace DemoApplication { class Program { static void Main() { Type empType = typeof(Employee);

  11. ความแตกต่าง int.Parse() และ Convert.ToInt32() ใน C#

    ทั้งคู่แปลง String → Int32 แต่ต่างกันที่การจัดการ Null Convert.ToInt32(null) คืนค่า 0** ไม่ Throw Exception string s = null; int result = Convert.ToInt32(s); // 0 int.Parse(null) Throw ArgumentNullException string s = null; int result = int.Parse(s); // Unhandled Exception: Value cannot be null. ตัว

  12. อัปเดตค่าใน Collection ด้วย LINQ ใน C#

    List.ForEach()** (Extension Method) ใช้ Update ทุก Item ได้ Update ทุก Item List fruits = new List f.Size = Large); // Update ทุกตัว Update เฉพาะเงื่อนไข (Where) IEnumerable fruits = new List f.Size == Small)) { fruit.Size = Large; } // Apple, Orange → Large | Mango → Medium (ไม่เปลี่ยน) ข้อสังเ

  13. แทนที่ Line Breaks ใน String ใน C#

    กำจัด Newline (\n), Carriage Return (\r), Tab (\t) จาก String 1. ใช้ String.Replace() string input = Hello \n\r beautiful \n\t world; string result = input.Replace(\n\r, _).Replace(\n\t, _); // Hello _ beautiful _ world ต้อง Replace ทีละ Pattern 2. ใช้ Regex.Replace() (แนะนำ) using System.Text.Regul

  14. วิธี Return Null จาก Generic Method ใน C#

    Generic Method ไม่สามารถ Return null โดยตรงได้ เพราะ T อาจเป็น Value Type (struct) ที่ไม่รองรับ Null ปัญหา public static T Add(T a, T b) { return null; // Compile Error: Cannot convert null to type parameter T } แก้ด้วย default(T) default(T) คืนค่า Default ของ Type T: Reference Type → null Valu

  15. การใช้งาน yield return ใน C#

    yield return ใช้สร้าง Iterator แบบ Lazy Evaluation (Deferred Execution) ควบคุม State ระหว่าง Caller และ Iterator ตัวอย่าง: Running Total using System; using System.Collections.Generic; namespace DemoApplication { class Program { static List numbers = new List { 1, 2, 3, 4, 5 };

  16. ความแตกต่างระหว่าง Last() และ LastOrDefault() ใน LINQ C#

    Last() vs LastOrDefault() ใน LINQ ทั้งสองเมธอดใช้ดึง Element ตัวสุดท้าย ของ Sequence ที่ตรงเงื่อนไข แตแตกต่างที่พฤติกรรมเมื่อ ไม่พบข้อมูล ความแตกต่างหลัก เมธอดพบข้อมูลไม่พบข้อมูล (Empty Sequence)โยน Exception? Last()คืน Element ตัวสุดท้ายโยน InvalidOperationException✅ ใช่ Last

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