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

    ใช้ bool.Parse(string) แปลงสตริง True หรือ False (ไม่สนใจพิมพ์เล็ก/ใหญ่) เป็น Boolean โยน FormatException หากรูปแบบไม่ถูกต้อง ตัวอย่างโค้ด using System; class Demo { static void Main() { string str = true; bool res = bool.Parse(str); Console.WriteLine(res); // True

  2. เมธอด Long.parse ใน C#

    ใช้ long.Parse(string) แปลงสตริงเป็นจำนวนเต็ม 64 บิต (long/Int64) โยน Exception หากแปลงไม่ได้ ตัวอย่างโค้ด using System; class Demo { static void Main() { string str = 7864646475767; long res = long.Parse(str); Console.WriteLine(res); } } ผลลัพธ์ 7864646475767 ตั

  3. โปรแกรม C# ลบตัวเลขหลังจุดทศนิยม

    ใช้ decimal.Truncate() ตัดส่วนทศนิยมทิ้ง (Truncate toward zero) ไม่ได้ปัดเศษ ตัวอย่าง using System; class Demo { static void Main() { decimal val = 9.15M; Console.WriteLine(decimal.Truncate(val)); // 9 } } ผลลัพธ์ 9 ต่างจาก Round: Truncate(9.9) = 9, Round(9.9) = 10, Trun

  4. ปัดเศษจำนวนเป็นเลขคู่ที่ใกล้ที่สุดใน C#

    ใช้ decimal.Round(value, decimals, MidpointRounding.ToEven) เพื่อปัดเศษแบบ Bankers Rounding (ปัดไปทางเลขคู่เมื่ออยู่ตรงกลาง) ตัวอย่าง: ปัด 25.55 เป็นจำนวนเต็ม using System; class Demo { static void Main() { decimal val = 25.55M; // ปัด 0 ตำแหน่งทศนิยม แบบ ToEven Cons

  5. ชนิดข้อมูล Decimal ใน C#

    decimal เป็น Value Type (Struct) ใช้สำหรับการคำนวณทางการเงินและการเงินที่ต้องการความแม่นยำสูง รองรับตัวดำเนินการ +, -, *, /, % ตัวอย่าง: บวก Decimal using System; class Demo { static void Main() { decimal d1 = 5.8M; decimal d2 = 3.2M; d1 = d1 + d2; Console.Wr

  6. ค่าคงที่ Decimal ใน C#

    โครงสร้าง decimal มีค่าคงที่สำหรับขอบเขตค่าสูงสุดและต่ำสุด decimal.MaxValue = 79,228,162,514,264,337,593,543,950,335 decimal.MinValue = -79,228,162,514,264,337,593,543,950,335 ตัวอย่างโค้ด using System; class Demo { static void Main() { decimal d = 5.8M; Console.WriteLine

  7. จัดการทศนิยมด้วยตัวดำเนินการตัวเลขใน C#

    ชนิด decimal รองรับตัวดำเนินการคณิตศาสตร์มาตรฐาน: +, -, *, /, % ทำงานคล้ายชนิดตัวเลขอื่น แต่มีความแม่นยำสูง ตัวอย่าง: บวกและลบ Decimal using System; class Demo { static void Main() { decimal d1 = 9.5M; decimal d2 = 4.2M; d1 = d1 + d2; Console.WriteLi

  8. โปรแกรม C# แสดงชื่อไฟล์ชั่วคราว

    ใช้ Path.GetTempPath() ดึงพาธโฟลเดอร์ Temporary ของระบบ (Environment Variable TEMP/TMP) ตัวอย่างโค้ด using System; using System.IO; class Demo { static void Main() { string tempFile = Path.GetTempPath(); Console.WriteLine(tempFile); } } ผลลัพธ์ (ตัวอย่าง Linux) /tmp/ เมธ

  9. สุ่มลำดับสตริงใน C#

    ใช้ OrderBy ร่วมกับ Random เพื่อสับเปลี่ยนลำดับอักขระในสตริง (Shuffle) ตัวอย่างโค้ด (r.Next(2) % 2) == 0) .ToArray()); Console.WriteLine(String = {0}, str); Console.WriteLine(Random String = {0}, random); } } ผลลัพธ์ (ตัวอย่าง) String = electronics Random St

  10. โปรแกรม C# ลบส่วนท้ายของสตริง

    ใช้ Regex.Replace แทนที่ส่วนท้ายที่ต้องการลบด้วยสตริงว่าง () ตัวอย่าง: ลบเครื่องหมาย ! ท้ายสตริง using System; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(string[] args) { string s1 = Demo Text!; string s2 =

  11. แทนที่ส่วนของสตริงด้วย C# Regex

    ใช้ Regex.Replace(input, pattern, replacement) เพื่อค้นหาและแทนที่ส่วนของสตริงที่ตรงกับ Regular Expression ตัวอย่าง: แทนที่ B.t (B ตามด้วยตัวอักษรใดๆ 1 ตัว แล้วตามด้วย t) ด้วย BAT using System; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(s

  12. โปรแกรม C# หาจำนวนโคร์ CPU

    ใช้ Environment.ProcessorCount เพื่อดึงจำนวนโปรเซสเซอร์/โคร์ตรรกะ (Logical Processors) ที่ระบบรายงาน ตัวอย่างโค้ด using System; namespace Demo { class Program { static void Main(string[] args) { Console.WriteLine(Environment.ProcessorCount); } } } หมา

  13. โปรแกรม C# ดึงนามสกุลไฟล์

    ใช้ Path.GetExtension(path) เพื่อดึงนามสกุลไฟล์ (รวมจุดนำหน้า เช่น .txt) ตัวอย่างโค้ด using System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { string myPath = @D:\new\quiz.txt; Console.WriteLine(Path.GetExtensi

  14. โปรแกรม C# ดึงชื่อไฟล์

    ใช้ Path.GetFileName(path) เพื่อดึงชื่อไฟล์พร้อมนามสกุลจากพาธเต็ม ตัวอย่างโค้ด using System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { string myPath = @D:\new\quiz.txt; Console.WriteLine(Extension

  15. Environment.NewLine ใน C#

    Environment.NewLine เป็น Property ที่คืนค่าตัวอักษรขึ้นบรรทัดใหม่ที่เหมาะสมกับ Platform ปัจจุบัน (\r\n บน Windows, \n บน Unix/Linux) ตัวอย่างการใช้งาน using System; namespace Demo { class Program { static void Main(string[] args) { string str = This is demo text!

  16. GetLogicalDrives ใน C#

    เมธอด Environment.GetLogicalDrives() คืนอาร์เรย์สตริงของไดรฟ์ที่ระบบรู้จัก (เช่น C:\\, D:\\) ตัวอย่างโค้ด using System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { Console.WriteLine(string.Join(,, Environment.GetLogicalDriv

  17. เมธอด EnumerateFiles ใน C#

    เมธอด Directory.EnumerateFiles() คืน IEnumerable<string> ของพาธไฟล์ ทำงานแบบ Lazy (Streaming) ไม่โหลดทั้งหมดเข้าหน่วยความจำ เหมาะสำหรับโฟลเดอร์ที่มีไฟล์เยอะมาก พารามิเตอร์ Path - พาธโฟลเดอร์ SearchPattern - รูปแบบชื่อไฟล์ (เช่น *.*, *.txt) SearchOption - AllDirectories (รวมโฟลเดอร์ย่อย) หรือ

  18. ประเภท Func แบบ Generic ใน C#

    Func<T1, T2, ..., TResult> เป็น Delegate แบบ Generic ที่อ้างอิงเมธอดที่มี ค่าคืน (Return Value) รองรับพารามิเตอร์ 0-16 ตัว ตัวสุดท้ายคือ Return Type ตัวอย่าง 4 รูปแบบ Func using System; namespace Demo { class Program { static void Main(string[] args) { // 1

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