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

    ภาพรวม StringBuilder รองรับ indexer ([]) สำหรับ อ่าน/เขียนตัวอักษร ที่ตำแหน่งเฉพาะ (mutable string) รูปแบบการใช้งาน o) → pro ตัวอย่างโค้ดเต็ม using System; using System.Text; public class Demo { public static void Main() { StringBuilder str = new StringBuilder(); str.App

  2. โปรแกรม C# ลบตัวอักษรเริ่มจากดัชนีที่กำหนดใน StringBuilder

    ภาพรวม เมธอด Remove(int startIndex, int length) ลบตัวอักษร length ตัว เริ่มจาก startIndex ตัวอย่างโค้ดเต็ม using System; using System.Text; public class Program { public static void Main() { StringBuilder str = new StringBuilder(Airport); Console.WriteLine(String: + str);

  3. โปรแกรม C# ลบช่วงตัวอักษรตามดัชนีด้วย StringBuilder

    ภาพรวม เมธอด StringBuilder.Remove(int startIndex, int length) ลบ ช่วงตัวอักษร เริ่มจากดัชนี startIndex ความยาว length ตัวอักษร รูปแบบการใช้งาน StringBuilder sb = new StringBuilder(Framework); sb.Remove(5, 4); // ลบ 4 ตัวอักษร ตั้งแต่ดัชนี 5 (work) → Frame ตัวอย่างโค้ดเต็ม using System; using Syst

  4. ประกาศอาร์เรย์ตัวอักษร (char array) ใน C#

    ภาพรวม ประกาศอาร์เรย์ char[] ระบุขนาด แล้วกำหนดค่าแต่ละดัชนี หรือใช้ array initializer รูปแบบการใช้งาน // วิธี 1: กำหนดขนาด แล้วเซททีละตัว char[] arr = new char[5]; arr[0] = h; arr[1] = a; arr[2] = n; arr[3] = k; arr[4] = s; // วิธี 2: Array initializer (แนะนำ) char[] arr2 = { h, a, n, k, s }; /

  5. เมธอด CompareTo() ใน C#

    ภาพรวม เมธอด CompareTo() ใช้ เปรียบเทียบค่าสองค่า ของชนิดเดียวกัน (implement IComparable<T>) คืนค่า int แสดงลำดับ ค่าคืน ค่าคืนความหมาย < 0ค่านี้ น้อยกว่า ค่าที่เปรียบเทียบ = 0เท่ากัน 0ค่านี้ มากกว่า ค่าที่เปรียบเทียบ ตัวอย่างโค้ดเต็ม using System; public class Demo { public stati

  6. StreamWriter ใน C#

    ภาพรวม StreamWriter ใช้ เขียนข้อความ (text) ลงไฟล์ หรือสตรีมอื่นๆ รองรับ encoding (ค่าเริ่มต้น UTF-8) และ auto-flush รูปแบบการใช้งาน using (StreamWriter sw = new StreamWriter(hello.txt)) { sw.WriteLine(Hello); sw.WriteLine(World); } ตัวอย่างโค้ดเต็ม using System.IO; public class Program

  7. เมธอด GroupBy() ใน C# LINQ

    ภาพรวม เมธอด GroupBy() ใน LINQ จัดกลุ่มองค์ประกอบในลำดับตาม คีย์ (key selector) คืนค่า IEnumerable<IGrouping<TKey, TElement>> ตัวอย่าง: จัดกลุ่มตัวเลขน้อยกว่าหรือเท่า 50 x a <= 50; } ผลลัพธ์ True 2 30 45 False 60 70 รูปแบบการใช้งานทั่วไป x.Category); // ILookup - immediate executi

  8. Clause OrderBy ใน C# LINQ

    ภาพรวม Clause orderby ใน LINQ query syntax ใช้ จัดเรียงลำดับองค์ประกอบ ตามฟิลด์ที่ระบุ รองรับ ascending (ค่าเริ่มต้น) และ descending ตัวอย่างโค้ดเต็ม using System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { List myList = new List { iOS b

  9. อ่านไฟล์ใน C# ด้วย StreamReader

    ภาพรวม StreamReader ใช้อ่านข้อความจากไฟล์ (text file) แบบลำดับ (sequential) เมธอดหลักคือ ReadLine() อ่านทีละบรรทัด รูปแบบการใช้งาน using (StreamReader sr = new StreamReader(hello.txt)) { string line = sr.ReadLine(); // อ่านบรรทัดแรก Console.WriteLine(line); } ตัวอย่างโค้ดเต็ม (สร้างไฟล์ แ

  10. โปรแกรม C# อ่านเนื้อหาไฟล์ทั้งหมดเป็นสตริงครั้งเดียว

    ภาพรวม ใช้ StreamReader.ReadToEnd() อ่านเนื้อหาไฟล์ทั้งหมดเข้าสตริงเดียวในครั้งเดียว เหมาะสำหรับไฟล์ขนาดเล็ก-ปานกลาง รูปแบบการใช้งาน using (StreamReader sr = new StreamReader(new.txt)) { string content = sr.ReadToEnd(); Console.WriteLine(content); } ตัวอย่างโค้ดเต็ม (สร้างไฟล์ แล้วอ่าน) u

  11. โปรแกรม C# คืนค่าอาร์เรย์จากเมธอด

    ภาพรวม เมธอดใน C# สามารถคืนค่าอาร์เรย์ได้ โดยระบุชนิดคืนเป็น T[] และ return อาร์เรย์ที่สร้าง/เตรียมไว้ ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() { Console.WriteLine(string.Join( , Display())); } static string[] Display() { str

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

    ภาพรวม ดึงองค์ประกอบสุดท้ายของอาร์เรย์ด้วย arr[arr.Length - 1] (index = ความยาว - 1) ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() { string[] str = { Java, HTML, jQuery, JavaScript, Bootstrap }; Console.WriteLine(Array...); foreach (s

  13. โปรแกรม C# วนลูปอาร์เรย์สตริงด้วย for loop

    ภาพรวม ใช้ลูป for วนอ่านอาร์เรย์สตริงผ่านดัชนี (index) เข้าถึงแต่ละองค์ประกอบด้วย arr[i] ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() { string[] str = { Videos, Tutorials, Tools, InterviewQA }; Console.WriteLine(String Array...); for

  14. โปรแกรม C# สร้างอาร์เรย์สตริงว่าง

    ภาพรวม สร้างอาร์เรย์สตริงว่าง (empty string array) ด้วย new string[0] หรือ new string[] { } หรือ Array.Empty<string>() (.NET 4.6+) รูปแบบการใช้งาน string[] empty1 = new string[0]; string[] empty2 = new string[] { }; string[] empty3 = Array.Empty(); // แนะนำ - singleton pattern ประหยัดหน่วยคว

  15. โปรแกรม C# หาดัชนีของคำในอาร์เรย์สตริง

    ภาพรวม ใช้ Array.IndexOf() หา ดัชนีแรก ของค่าที่ระบุในอาร์เรย์ คืน -1 หากไม่พบ รูปแบบการใช้งาน string[] str = { Cat, Mat, Rat }; int index = Array.IndexOf(str, Mat); // 1 ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() { string[] str = { Cat, Mat, Rat

  16. โปรแกรม C# รวมคำเป็นสตริงด้วย string.Join

    ภาพรวม เมธอด string.Join(separator, array) รวมองค์ประกอบในอาร์เรย์/ลิสต์เป็นสตริงเดียว โดยใส่ separator ระหว่างแต่ละค่า รูปแบบการใช้งาน string[] str = { One, Two, Three, Four, Five }; string res = string.Join( , str); // One Two Three Four Five ตัวอย่างโค้ดเต็ม using System; public class Demo {

  17. โปรแกรม C# แยกสตริงที่รวมไว้ด้วย Split

    ภาพรวม ใช้ string.Join() รวมอาร์เรย์สตริงเป็นสตริงเดียว แล้วใช้ Split() แยกกลับเป็นอาร์เรย์ ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() { string[] str = { Java, AngularJS, Python, jQuery, HTML5 }; string res = string.Join( , str); C

  18. อาร์เรย์ Boolean (Bool Array) ใน C#

    ภาพรวม อาร์เรย์ bool[] ใช้เก็บค่า true และ false สร้างด้วย new bool[size] ค่าเริ่มต้นทุกตัวคือ false รูปแบบการใช้งาน bool[] arr = new bool[5]; arr[0] = true; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true; ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Mai

  19. ตัวดำเนินการ default ใน C#

    ภาพรวม Operator default ใช้ดึง ค่าเริ่มต้น (default value) ของชนิดข้อมูลใดๆ ทั้ง value types และ reference types นิพจน์ default ถูกประเมินตอน compile-time รูปแบบการใช้งาน int val1 = default(int); // 0 long val2 = default(long); // 0 bool val3 = default(bool); // false string val4 = default(st

  20. ค่าเริ่มต้น (Default Value) ของ StringBuilder ใน C#

    ภาพรวม StringBuilder เป็น reference type (class) ดังนั้นค่าเริ่มต้นคือ null รูปแบบการใช้งาน StringBuilder str = default(StringBuilder); // null ตัวอย่างโค้ดเต็ม using System; using System.Text; public class Demo { public static void Main() { StringBuilder str = default(StringBuil

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