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

    C# ใช้ Numeric Suffixes ระบุชนิดข้อมูลของ Literal ตัวเลข โดยตรง Suffixes พบบ่อย L / l - long (Int64) D / d - double F / f - float (Single) M / m - decimal U / u - uint (UInt32) UL / ul - ulong (UInt64) ตัวอย่างโค้ด using System; public class Program { public static void Main() {

  2. เมธอด ElementAt() ใน C#

    เมธอด ElementAt(index) ใน LINQ ดึงองค์ประกอบที่ดัชนี (Index) ที่ระบุ (0-based) โยน ArgumentOutOfRangeException หาก Index ไม่ถูกต้อง ตัวอย่างโค้ด using System; using System.Linq; public class Demo { public static void Main() { string[] arr = { One, Two, Three, Four, Five }; s

  3. เมธอด SkipWhile ใน C#

    เมธอด SkipWhile() ข้ามองค์ประกอบตั้งแต่ต้นลำดับ ตราบใดที่เงื่อนไขยังเป็นจริง หยุดข้ามทันทีที่พบองค์ประกอบที่ทำให้เงื่อนไขเป็นเท็จ ตัวอย่าง: ข้ามเลขคู่ (Even Numbers) ele % 2 == 0); Console.WriteLine(New array after skipping even elements...); foreach (int val in res) Consol

  4. โปรแกรม C# หาองค์ประกอบมากที่สุดจากอาร์เรย์ด้วย Lambda Expressions

    ใช้ Max() ร่วมกับ Lambda Selector เพื่อเปรียบเทียบค่าตามเงื่อนไขเฉพาะ (เช่น ค่าสัมบูรณ์) ตัวอย่าง: หาค่าสัมบูรณ์มากที่สุด Math.Abs(element))); // 99 } } ผลลัพธ์ 99 ประโยชน์: เปลี่ยน Logic การเปรียบเทียบได้ยืดหยุ่น (เช่น หาสตริงที่ยาวที่สุด, วันที่ล่าสุด, เป็นต้น)

  5. เมธอดขยาย Any ใน C#

    เมธอด Any() ใน System.Linq ตรวจสอบว่ามีองค์ประกอบ อย่างน้อย 1 ตัว ตรงตามเงื่อนไขหรือไม่ คืน bool = 20 ในอาร์เรย์หรือไม่ = 20); Console.WriteLine(res); // True } } ผลลัพธ์ True Overload ไม่มีพารามิเตอร์: Any() ตรวจสอบว่าลำดับมีองค์ประกอบอย่างน้อย 1 ตัวหรือไม่ (ไม่ว่าง)

  6. เมธอด SequenceEqual ใน C#

    เมธอด SequenceEqual() ตรวจสอบว่าสองลำดับมีองค์ประกอบเท่ากันทุกตำแหน่งและลำดับเดียวกัน ตัวอย่างโค้ด using System; using System.Linq; class Program { static void Main() { string[] arr1 = { This, is, it }; string[] arr2 = { My, work, report }; string[] arr3 = { This, is

  7. แสดงรายการไฟล์แบบ Recursive ใน C#

    ใช้ Directory.GetFiles(path, *.*, SearchOption.AllDirectories) ดึงไฟล์ทั้งหมดในโฟลเดอร์และโฟลเดอร์ย่อยทั้งหมด ตัวอย่างโค้ด using System; using System.IO; class Program { static void Main() { string[] myFiles = Directory.GetFiles(@D:\New\, *.*, SearchOption.AllDirectories); f

  8. หาจำนวนไบต์ในไฟล์ด้วย C#

    ใช้ FileInfo.Length Property ดึงขนาดไฟล์เป็นไบต์ (long) ตัวอย่างโค้ด using System; using System.IO; class Program { static void Main() { FileInfo file = new FileInfo(@D:\new); long res = file.Length; Console.WriteLine(Bytes: + res); } } ผลลัพธ์ (ตัวอย่าง) Bytes:

  9. โปรแกรม C# เขียนตัวเลขในรูปแบบฐานสิบหก

    ใช้ Composite Formatting {0:X} หรือ {0:x} แสดงตัวเลขในฐานสิบหก X = พิมพ์ใหญ่, x = พิมพ์เล็ก สามารถระบุความกว้างได้ (เช่น X8) ตัวอย่างโค้ด using System; class Demo { static void Main() { int a = 12250; Console.WriteLine({0:x}, a); // 2fda Console.WriteLine({0:x8}, a

  10. เมธอด Tuple.Create ใน C#

    เมธอด Static Tuple.Create เป็น Factory Method สร้าง Tuple ได้สะดวกโดยไม่ต้องระบุ Generic Type Parameters ให้เอง (Type Inference) ตัวอย่างโค้ด using System; class Demo { static void Main() { var myTuple = Tuple.Create(marks, 100); Console.WriteLine(myTuple); // (marks, 100)

  11. ตั้งค่า 6-item tuple ใน C#

    สร้าง Tuple 6 องค์ประกอบ (Tuple<T1,T2,T3,T4,T5,T6>) โดยใช้ Constructor หรือ Tuple.Create ตัวอย่าง: Tuple ประกอบ String, String[], int, int, int, int using System; class Demo { static void Main() { var myTuple = new Tuple( electronics, new string[] { sho

  12. โปรแกรม C# หาองค์ประกอบมากที่สุดจากอาร์เรย์

    ใช้เมธอด Max() ของ LINQ (หรือ Array.Max) หาค่าสูงสุดในอาร์เรย์ ตัวอย่างโค้ด using System; using System.Linq; class Demo { static void Main() { int[] arr = { 20, 50, -35, 25, 60 }; Console.WriteLine(arr.Max()); // 60 } } ผลลัพธ์ 60 รองรับ: ทุกชนิดที่ Implement IComparable

  13. โปรแกรม C# ตรวจสอบว่าลำดับสองลำดับเหมือนกันหรือไม่

    ใช้เมธอด SequenceEqual() เปรียบเทียบสองลำดับ ว่ามีองค์ประกอบเท่ากัน ทุกตำแหน่ง และลำดับเดียวกัน ตัวอย่างโค้ด using System; using System.Linq; class Program { static void Main() { string[] arr1 = { This, is, it }; string[] arr2 = { My, work, report }; string[] arr3 =

  14. ตัวดำเนินการ Group By ใน C#

    ใช้คำสั่ง group ... by ... ใน LINQ Query Syntax เพื่อจัดกลุ่มองค์ประกอบตาม Key ที่คำนวณจากฟังก์ชัน ตัวอย่าง: จัดกลุ่มตัวเลขตาม มากกว่าหรือเท่ากับ 20 = 20; } ผลลัพธ์ False 5 10 15 True 20 25 30 รูปแบบ Method Syntax: = 20)

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