Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. การดึงรายชื่อไดรฟ์ทั้งหมดใน C# ด้วย DriveInfo.GetDrives()

    คลาส DriveInfo ใน namespace System.IO ให้ข้อมูลเกี่ยวกับไดรฟ์ต่างๆ ในระบบ เมธอดสแตติก GetDrives() คืนค่าอาร์เรย์ของออบเจกต์ DriveInfo แสดงถึงไดรฟ์ทั้งหมดที่ระบบปฏิบัติการตรวจพบ รูปแบบการใช้งาน DriveInfo[] drives = DriveInfo.GetDrives(); โค้ดตัวอย่าง: แสดงชื่อไดรฟ์ทั้งหมด using System; using System

  2. เมธอด TakeWhile() ใน C#: ดึงองค์ประกอบจนกว่าเงื่อนไขจะเป็นเท็จ

    เมธอด TakeWhile() ใน System.Linq ใช้ดึงองค์ประกอบจากจุดเริ่มต้นของลำดับ ต่อเนื่องกัน ตราบใดที่เงื่อนไข (Predicate) ยังคงเป็นจริง จะหยุดทันทีเมื่อพบองค์ประกอบที่ทำให้เงื่อนไขเป็นเท็จ ความแตกต่างระหว่าง TakeWhile() และ Where() เมธอดพฤติกรรม TakeWhile()ดึงจากต้นทางจนกว่าเงื่อนไขจะเป็นเท็จ แล้วหยุด

  3. เมธอด Aggregate() ใน C#: การคำนวณสะสมบนลำดับข้อมูล

    เมธอด Aggregate() ใน System.Linq ใช้สำหรับดำเนินการคำนวณสะสม (accumulation) บนลำดับข้อมูล โดยนำฟังก์ชันที่กำหนดไปประยุกต์ใช้กับองค์ประกอบแต่ละคู่ตามลำดับ เพื่อสร้างค่าผลลัพธ์เดียว สามารถใช้แทนการเขียนลูปสำหรับการคำนวณต่างๆ เช่น ผลรวม ผลคูณ ค่าสูงสุด/ต่ำสุด และอื่นๆ รูปแบบพื้นฐาน TResult Aggregate(

  4. เมธอด All() ใน C#: ตรวจสอบเงื่อนไขของทุกองค์ประกอบ

    เมธอด All() เป็น Extension Method ใน namespace System.Linq ใช้เพื่อตรวจสอบว่า ทุกองค์ประกอบ ในลำดับตรงตามเงื่อนไขที่กำหนดหรือไม่ คืนค่า true หากทุกตัวตรงเงื่อนไข มิฉะนั้นคืน false รูปแบบการใช้งาน condition); ตัวอย่างการใช้งาน ตรวจสอบว่าทุกค่าในอาร์เรย์มากกว่าหรือเท่ากับ 2 หรือไม่: = 2); // คืนค่า

  5. เมธอด Skip() ใน C#: การข้ามองค์ประกอบในอาร์เรย์

    เมธอด Skip() ใน C# ใช้สำหรับข้ามจำนวนองค์ประกอบที่กำหนดจากจุดเริ่มต้นของลำดับ (sequence) เช่น อาร์เรย์หรือลิสต์ เมธอดนี้เป็นส่วนหนึ่งของ System.Linq และคืนค่าเป็นลำดับใหม่ที่ไม่รวมองค์ประกอบที่ข้ามไป ตัวอย่างการใช้งาน สมมติว่าเรามีอาร์เรย์ของจำนวนเต็มดังนี้: int[] arr = { 10, 20, 30, 40, 50 }; เพื

  6. หาองค์ประกอบเฉพาะใน C# List

    ใช้เมธอด List<T>.Find(Predicate<T>) ค้นหาองค์ประกอบ ตัวแรก ที่ตรงตามเงื่อนไข คืนค่า Default (null/0) หากไม่พบ ตัวอย่าง: หาตัวเลขแรกที่หาร 2 ลงตัว using System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { List myList = new List i

  7. เมธอด ToDictionary ใน C#

    เมธอดขยาย ToDictionary() ใน LINQ แปลงลำดับ (IEnumerable) เป็น Dictionary โดยระบุ Key Selector และ Value Selector ตัวอย่าง: แปลง String Array เป็น Dictionary<string, bool> true); foreach (var ele in d) Console.WriteLine({0}, {1}, ele.Key, ele.Value); } } ผลลัพธ

  8. ContainsValue ใน C#

    เมธอด Dictionary<TKey, TValue>.ContainsValue(TValue) ตรวจสอบว่ามีค่า (Value) ที่ระบุอยู่ใน Dictionary หรือไม่ คืน bool ตัวอย่างโค้ด using System; using System.Collections.Generic; class Program { static void Main() { var d = new Dictionary(); d.Add(keyboard, One);

  9. โปรแกรม C# รวม Dictionary ของสองคีย์

    ใช้ HashSet<string> รวม Keys จากสอง Dictionary ด้วย UnionWith (Union Set) ตัวอย่างโค้ด using System; using System.Collections.Generic; public class Program { public static void Main() { var dict1 = new Dictionary { {one, 1}, {Two, 2} }; var dict2 = new Dictionary { {Th

  10. Case-insensitive Dictionary ใน C#

    สร้าง Dictionary<string, TValue> ที่ไม่สนใจตัวพิมพ์เล็ก/ใหญ่ของ Key โดยส่ง StringComparer.OrdinalIgnoreCase เข้า Constructor ตัวอย่างโค้ด using System; using System.Collections.Generic; public class Program { public static void Main() { var dict = new Dictionary(StringComparer

  11. โปรแกรม C# นับจำนวนบรรทัดในไฟล์

    ใช้ File.ReadAllLines(path).Length อ่านทุกบรรทัดเป็น Array แล้วดู Length (เหมาะไฟล์เล็ก-ปานกลาง) ตัวอย่างโค้ด using System; using System.IO; public class Program { public static void Main() { // สร้างไฟล์ทดสอบ using (StreamWriter sw = new StreamWriter(hello.txt)) {

  12. โปรแกรม C# เขียนอาร์เรย์ลงไฟล์

    ใช้ File.WriteAllLines(path, string[]) เขียนอาร์เรย์สตริงลงไฟล์ ทีละบรรทัด (เพิ่ม Newline อัตโนมัติ) ตัวอย่างโค้ด using System; using System.IO; public class Program { public static void Main() { string[] stringArray = { one, two, three }; File.WriteAllLines(new.txt, stringA

  13. โปรแกรม C# แสดงชื่อไดเรกทอรี

    ใช้ DirectoryInfo.Name Property ดึงชื่อโฟลเดอร์ (ไม่รวมพาธเต็ม) ตัวอย่างโค้ด using System; using System.IO; public class Program { public static void Main() { DirectoryInfo dir = new DirectoryInfo(@D:\new\); Console.WriteLine(dir.Name); // new } } ผลลัพธ์ new คุณสมบัติอื

  14. โปรแกรม C# สร้างไดเรกทอรีใหม่

    ใช้ Directory.CreateDirectory(path) สร้างโฟลเดอร์ (รวมโฟลเดอร์แม่ที่ยังไม่มี) คืนค่า DirectoryInfo ของโฟลเดอร์ที่สร้าง ตัวอย่างโค้ด using System; using System.IO; public class Program { public static void Main() { Directory.CreateDirectory(@D:\Tutorial); Console.WriteLine(Di

  15. โปรแกรม C# ดูข้อมูลไฟล์

    ใช้ FileInfo.Attributes Property ดึง FileAttributes (Flags Enum) ที่บอกสถานะไฟล์: ReadOnly, Hidden, System, Directory, Archive, Device, Normal, Temporary, SparseFile, ReparsePoint, Compressed, Offline, NotContentIndexed, Encrypted, IntegrityStream, NoScrubData ตัวอย่างโค้ด using System; using System

  16. โปรแกรม C# ดูเวลาเขียนไฟล์ล่าสุด

    ใช้ FileInfo.LastWriteTime ดึงวันเวลาที่ไฟล์ถูกแก้ไข/เขียนล่าสุด ตัวอย่างโค้ด (รวม Creation, LastAccess, LastWrite) using System; using System.IO; public class Program { public static void Main() { using (StreamWriter sw = new StreamWriter(amit.txt)) { sw.WriteLi

  17. ดูเวลาสร้างไฟล์ใน C#

    ใช้ FileInfo.CreationTime Property ดึงวันเวลาที่ไฟล์ถูกสร้าง ตัวอย่างโค้ด using System; using System.IO; public class Program { public static void Main() { using (StreamWriter sw = new StreamWriter(qa.txt)) { sw.WriteLine(Questions and Answers!); }

  18. คำต่อท้ายตัวพิมพ์เล็กใน C#

    C# อนุญาตให้ใช้ Suffix ตัวพิมพ์เล็ก (l, f, d, m, u) ได้ แต่ ไม่แนะนำ เพราะอาจสับสนกับตัวเลข (โดยเฉพาะ l กับ 1) ตัวอย่าง using System; public class Program { public static void Main() { long a = 29876l; // Warning: l suffix easily confused with 1 float b = 95.10f; // OK แต

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:51/134  20-คอมพิวเตอร์/Page Goto:1 45 46 47 48 49 50 51 52 53 54 55 56 57