Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. การตรวจสอบรูปแบบวันที่ด้วย Regex ใน C#

    ภาพรวม บทความนี้แนะนำการใช้ DateTime.TryParseExact เพื่อตรวจสอบว่าสตริงวันที่ตรงตามรูปแบบที่กำหนดหรือไม่ วิธีนี้ปลอดภัยกว่า Regex เพราะตรวจสอบความถูกต้องของวันที่จริง (เช่น ไม่ให้ 30 กุมภาพันธ์ผ่าน) โค้ดตัวอย่าง using System; using System.Globalization; namespace Demo { class Program {

  2. โปรแกรม C# สำหรับหาความแตกต่างระหว่างสองลิสต์

    ภาพรวม บทความนี้สาธิตวิธีใช้เมธอด Except() จาก LINQ เพื่อหาความแตกต่างระหว่างสองลิสต์ (รายการที่อยู่ในลิสต์แรกแต่ไม่อยู่ในลิสต์ที่สอง) โค้ดตัวอย่าง using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List<string> list

  3. โปรแกรม C# สำหรับรวมอาร์เรย์ที่จัดเรียงแล้วสองอาร์เรย์เป็นอันเดียว

    ภาพรวม บทความนี้แสดงวิธีการรวมอาร์เรย์ที่จัดเรียงแล้วสองอาร์เรย์ให้กลายเป็นอาร์เรย์เดียวใน C# โดยใช้ลูป for ธรรมดา เพื่อคัดลอกองค์ประกอบจากอาร์เรย์ต้นทางไปยังอาร์เรย์ปลายทาง โค้ดตัวอย่าง using System; class Program { static void Main() { int[] arr1 = { 5, 15, 25, 30, 47 }; int[

  4. ชนิดข้อมูลของ Jagged Array ใน C#

    Jagged Array คือ Array of Arrays โดย Elements เป็น Reference Types และถูก Initialize เป็น null ประกาศ: int[][] marks; Initialize: int[][] marks = new int[][]{ new int[]{ 40,57 }, new int[]{ 34,55 }, new int[]{ 23,44 }, new int[]{ 56, 78 }, new int[]{ 66, 79 } }; ตัวอย่าง using Sy

  5. Unboxing ใน C# คืออะไร

    Boxing เป็น Implicit Conversion จาก Value Type เป็น Reference Type (Object) Unboxing เป็น Explicit Conversion กลับจาก Object เป็น Value Type เดิม ตัวอย่าง: int x = 30; object obj = x; // Boxing int unboxInt = (int)obj; // Unboxing ตัวอย่างกับ ArrayList: int x = 5; ArrayList arrList = new Arra

  6. Unmanaged Code ใน C# คืออะไร

    Unmanaged Code คือโค้ดที่ไม่อยู่ภายใต้การควบคุมของ CLR (Common Language Runtime) แอปพลิเคชันที่ไม่ได้รันภายใต้ CLR โค้ดที่ใช้ Pointer Variable (Unsafe Code) ใช้คีย์เวิร์ด unsafe เพื่ออนุญาตให้ใช้ Pointer ตัวอย่าง static unsafe void Main(string[] args) { int var = 20; int* p = &

  7. Flow Control ใน Try Catch Finally ใน C#

    ทำความเข้าใจ Flow Control ด้วยตัวอย่างหารเลข: ตัวอย่าง using System; namespace ErrorHandlingApplication { class DivNumbers { int result; DivNumbers() { result = 0; } public void division(int num1, int num2) { try { result = num1 / num2;

  8. วิธีแปลงเลขฐานสิบเป็นฐานสองใน C#

    แปลงเลข 48 เป็นฐานสอง ใช้หารและโมดูลู 2 ในลูป while = 1) { val = decVal / 2; a += (decVal % 2).ToString(); decVal = val; } จากนั้นกลับลำดับสตริงได้ผลลัพธ์ ตัวอย่าง = 0; i--) { binValue = binValue + a[i]; } Console.WriteLine(Binary = {0}, binValue);

  9. วิธีตรวจสอบ Palindrome ด้วย C#

    กำหนดสตริง: str = Level; แปลงเป็น Char Array แล้ว Reverse: char[] ch = str.ToCharArray(); Array.Reverse(ch); เปรียบเทียบด้วย Equals แบบไม่สนใจ Case: bool res = str.Equals(rev, StringComparison.OrdinalIgnoreCase); ตัวอย่าง using System; namespace Demo { class Program { static void Main(st

  10. วิธีเขียน Hello World ใน C#

    ใช้ Console.WriteLine แสดงข้อความ ตัวอย่าง using System; using System.Collections.Generic; using System.Text; namespace Program { class MyApplication { static void Main(string[] args) { Console.WriteLine(Hello World); Console.Read(); } } } ผลลัพธ์ Hell

  11. LinkedList ใน C#

    Namespace System.Collections.Generic มีคลาส LinkedList<T> ใช้แทรกและลบข้อมูลได้เร็ว รองรับข้อมูลซ้ำ ตัวอย่าง using System; using System.Collections.Generic; class Demo { static void Main() { LinkedList<string> l = new LinkedList<string>(); l.AddLast(one);

  12. วิธีค้นหาไฟล์ด้วย C#

    ใช้ Directory.GetDirectories หาโฟลเดอร์ย่อย แล้ววนลูปค้นหาในโฟลเดอร์ย่อย string path = @d:/New/Myfile; string[] myDir = Directory.GetDirectories(path, xml, SearchOption.AllDirectories); Console.WriteLine(myDir.Length.ToString()); foreach (string res in myDir) Console.WriteLine(res);

  13. โปรแกรม C# รวม Dictionary สองอัน

    กำหนด Dictionary 2 อัน: Dictionary<string, int> dict1 = new Dictionary<string, int>(); dict1.Add(laptop, 1); dict1.Add(desktop, 2); Dictionary<string, int> dict2 = new Dictionary<string, int>(); dict2.Add(desktop, 3); dict2.Add(tablet, 4); dict2.Add(mobile, 5); ใช้ HashSet แล

  14. Deadlock และ Starvation ใน C#

    Deadlock เกิดเมื่อเธรดล็อก Resource และอีกเธรดต้องการ Resource เดียวกันในเวลาเดียวกัน พบบ่อยในระบบ Multi-processing ตัวอย่าง 2 เธรด: Thread OneThread Two Takes Lock PTakes Lock Q Requests Lock QRequests Lock P Thread One รอ Lock Q ที่ Thread Two ถือครอง

  15. การซิงโครไนซ์ ArrayList ใน C#

    ใช้ ArrayList.Synchronized สร้าง Wrapper ที่ปลอดภัยต่อเธรด ล็อคด้วย SyncRoot: ArrayList arr = new ArrayList(); lock(arr.SyncRoot) { foreach (object ele in arr) { } } ตัวอย่าง using System; using System.Collections; public class Demo { public static void Main() { ArrayList arr1 = new

  16. Semaphore ใน C#

    คลาส Semaphore จำกัดจำนวนเธรดที่เข้าถึง Critical Section ใช้ควบคุมการเข้าถึง Resource Pool อยู่ใน namespace System.Threading Constructor หลัก: ลำดับConstructor & Description 1Semaphore(Int32,Int32) - ระบุจำนวนเริ่มต้นและสูงสุดของเธรดที่เข้าถึงได้พร้อมกัน 2Sema

  17. วิธี Clone Generic List ใน C#

    List เป็น Generic Collection เก็บข้อมูลชนิดเดียวกัน ใช้ CopyTo เพื่อ Clone ประกาศ List และเพิ่มข้อมูล: List<string> myList = new List<string>(); myList.Add(Programming); myList.Add(Web Dev); myList.Add(Database); สร้างอาร์เรย์ใหม่และ Copy: string[] arr = new string[10]; myList.CopyTo(arr

  18. วิธีใช้ Attributes ใน C#

    Attribute คือแท็กประกาศ (declarative tag) ใช้สื่อข้อมูลให้ Runtime เกี่ยวกับพฤติกรรมของสมาชิกต่างๆ เช่น คลาส เมธอด โครงสร้าง Enum Assembly เป็นต้น วางไว้เหนือสมาชิกในวงเล็บเหลี่ยม [ ] ไวยากรณ์: [attribute(positional_parameters, name_parameter = value, ...)] Element .NET มี 2 ประเภท: Pre-defined attr

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:40/134  20-คอมพิวเตอร์/Page Goto:1 34 35 36 37 38 39 40 41 42 43 44 45 46