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

    ภาพรวม เมธอด Zip() ใน LINQ ใช้ รวมสองลำดับ (sequences) เข้าด้วยกัน โดยจับคู่องค์ประกอบตามตำแหน่ง (index) และใช้ฟังก์ชัน predicate สร้างผลลัพธ์ใหม่ หยุดเมื่อลำดับใดลำดับหนึ่งหมด รูปแบบการใช้งาน num + + name); ตัวอย่างโค้ดเต็ม one + + two); foreach (var ele in mergedSeq) Console.Write

  2. ดึงอันดับ (Rank) ของอาร์เรย์ 3 มิติใน C#

    ภาพรวม คุณสมบัติ Rank ของอาร์เรย์ คืนค่า จำนวนมิติ (number of dimensions) ของอาร์เรย์ สำหรับอาร์เรย์ 3 มิติ จะคืนค่า 3 รูปแบบการใช้งาน int[,,] arr = new int[3, 4, 5]; int rank = arr.Rank; // 3 ตัวอย่างโค้ดเต็ม using System; class Program { static void Main() { int[,,] arr = new i

  3. ดึงความกว้างและความสูงของอาร์เรย์ 3 มิติใน C#

    ภาพรวม สำหรับอาร์เรย์ 3 มิติ int[,,] arr = new int[3, 4, 5] สามารถใช้ GetLength() ดึงขนาดแต่ละมิติ โดยมักเรียกมิติ 0 ว่า แถว (rows/ความสูง) และมิติ 1 ว่า คอลัมน์ (columns/ความกว้าง) รูปแบบการใช้งาน int[,,] arr = new int[3, 4, 5]; int rows = arr.GetLength(0); // 3 - ความสูง/แถว int cols = arr.Ge

  4. ขอบเขตของอาร์เรย์ 3 มิติใน C#

    ภาพรวม ใช้เมธอด GetUpperBound() และ GetLowerBound() เพื่อดึงขอบเขตบน (upper bound) และขอบเขตล่าง (lower bound) ของแต่ละมิติในอาร์เรย์ 3 มิติ รูปแบบการใช้งาน int[,,] arr = new int[3, 4, 5]; // มิติ 0: Upper=2, Lower=0 (ขนาด 3) // มิติ 1: Upper=3, Lower=0 (ขนาด 4) // มิติ 2: Upper=4, Lower=0 (ขนาด 5

  5. ArgumentNullException ใน C#

    ภาพรวม ArgumentNullException โยนเมื่อ ส่งอ้างอิง null ให้เมธอดที่ไม่ยอมรับ null เป็นอาร์กิวเมนต์ ตัวอย่างที่ทำให้เกิด ArgumentNullException using System; class Demo { static void Main() { string val = null; int res = int.Parse(val); // โยน ArgumentNullException } } ข้

  6. โปรแกรม C# แปลงค่า Int32 เป็น Decimal

    ภาพรวม ใช้ Convert.ToDecimal() แปลง int (Int32) เป็น decimal ซึ่งเป็นการแปลงแบบขยายชนิด ทำได้เสมอโดยไม่สูญเสียความแม่นยำ รูปแบบการใช้งาน int val = 2923; decimal decVal = Convert.ToDecimal(val); // 2923.00 ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() {

  7. โปรแกรม C# แปลงค่า Byte เป็น Int32

    ภาพรวม ใช้ Convert.ToInt32() แปลง byte เป็น int (Int32) ซึ่งเป็นการแปลงแบบขยายชนิด (widening) ทำได้เสมอโดยไม่สูญเสียข้อมูล รูปแบบการใช้งาน byte val = Byte.MaxValue; // 255 int intVal = Convert.ToInt32(val); // 255 ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main()

  8. โปรแกรม C# แปลงค่า Double เป็น Int64

    ภาพรวม ใช้ Convert.ToInt64() แปลง double เป็น long (Int64) โดยจะปัดเศษทศนิยมแบบ Bankers Rounding เหมือน Convert.ToInt32 รูปแบบการใช้งาน double val = 23.951213e12; // 2.3951213 × 10¹³ long longVal = Convert.ToInt64(val); // 23951213000000 ตัวอย่างโค้ดเต็ม using System; public class Demo { pub

  9. เมธอด Any ใน C# LINQ

    ภาพรวม เมธอด Any() ใน LINQ ตรวจสอบว่า มีองค์ประกอบอย่างน้อยหนึ่งรายการ ที่ตรงตามเงื่อนไขหรือไม่ คืนค่า bool รูปแบบการใช้งาน 10); // true ตัวอย่างโค้ดเต็ม 10); Console.WriteLine(res); // True } } ผลลัพธ์ True Any() แบบไม่มีเงื่อนไข bool hasElements = arr.AsQueryable().Any(); // tru

  10. โปรแกรม C# แปลง Double เป็นค่าจำนวนเต็ม (Integer)

    ภาพรวม เพื่อแปลงค่า double เป็น int (Int32) ให้ใช้เมธอด Convert.ToInt32() ซึ่งจะ ปัดเศษทศนิยม ตามกฎ Bankers Rounding (ปัดเป็นเลขคู่เมื่ออยู่กึ่งกลาง) รูปแบบการใช้งาน double val = 21.34; int res = Convert.ToInt32(val); // 21 ตัวอย่างโค้ดเต็ม using System; public class Demo { public static voi

  11. การแปลงโดยนัย (Implicit Conversion) จาก Byte เป็น Decimal ใน C#

    ภาพรวม byte (8-bit unsigned integer) สามารถแปลงเป็น decimal แบบโดยนัย (implicit) ได้โดยตรง เพราะ decimal มีขอบเขตครอบคลุม byte หมด รูปแบบการใช้งาน byte val = 16; decimal dec = val; // implicit conversion ตัวอย่างโค้ดเต็ม using System; public class Demo { public static void Main() {

  12. การแปลงโดยนัย (Implicit Conversion) จาก UInt32 เป็น Decimal ใน C#

    ภาพรวม การแปลงโดยนัย (implicit conversion) จาก uint (32-bit unsigned integer) เป็น decimal เป็นไปได้โดยตรง เพราะ decimal มีขอบเขตและความแม่นยำครอบคลุม uint หมด รูปแบบการใช้งาน uint val = 342741539; decimal dec = val; // implicit conversion - ไม่ต้อง cast ตัวอย่างโค้ดเต็ม using System; public cla

  13. เมธอด Average ใน C# LINQ

    ภาพรวม เมธอด Average() ใน LINQ คำนวณ ค่าเฉลี่ย (mean) ของลำดับตัวเลข คืนค่า double (สำหรับ int, long, float, double, decimal) รูปแบบการใช้งาน int[] arr = { 10, 17, 25, 30, 40, 55, 60, 70 }; double avg = arr.AsQueryable().Average(); // หรือ Queryable.Average(arr.AsQueryable()) ตัวอย่างโค้ดเต็ม usi

  14. เมธอด Cast ใน C# LINQ

    ภาพรวม เมธอด Cast<T>() ใน LINQ ใช้ แปลง (cast) องค์ประกอบในลำดับให้เป็นชนิดที่ระบุ T เหมาะสำหรับแปลงจาก IEnumerable (non-generic) หรือ List<object> เป็นลำดับเจเนอริก รูปแบบการใช้งาน List list = new List { Mac, Windows, Linux }; IEnumerable strings = list.Cast(); ตัวอย่างโค้ดเต็ม: ดึง

  15. เมธอด Linq Contains ใน C#

    ภาพรวม เมธอด Contains() ใน LINQ ใช้ตรวจสอบว่าองค์ประกอบเฉพาะมีอยู่ในลำดับหรือไม่ คืนค่า bool (true หากพบ, false หากไม่พบ) รูปแบบการใช้งาน string[] arr = { Java, C++, Python }; string search = Python; bool hasPython = arr.AsQueryable().Contains(search); // true ตัวอย่างโค้ดเต็ม using System; using

  16. โปรแกรม C# ค้นหาสตริงในอาร์เรย์สตริง

    ภาพรวม ใช้เมธอด LINQ Contains() เพื่อตรวจสอบว่าสตริงเฉพาะมีอยู่ในอาร์เรย์สตริงหรือไม่ คืนค่า bool รูปแบบการใช้งาน string[] arr = { Bag, Pen, Pencil }; string search = Pen; bool found = arr.AsQueryable().Contains(search); // true ตัวอย่างโค้ดเต็ม using System; using System.Linq; using System.Colle

  17. เมธอด Linq Count ใน C#

    ภาพรวม เมธอด Count() ใน LINQ ใช้นับจำนวนองค์ประกอบในลำดับ (sequence) คืนค่า int รูปแบบการใช้งาน string[] arr = { Java, C++, Python }; int count = arr.AsQueryable().Count(); // หรือ arr.Count() ตัวอย่างโค้ดเต็ม using System; using System.Linq; using System.Collections.Generic; class Demo { st

  18. เมธอด DefaultIfEmpty ใน C#

    ภาพรวม เมธอด DefaultIfEmpty() ใน LINQ ใช้จัดการกับ ลำดับว่าง (empty sequence) โดยคืนค่าลำดับที่มีองค์ประกอบเดียวคือ ค่าเริ่มต้น (default value) ของชนิดข้อมูล แทนที่จะเป็นลำดับว่าง รูปแบบการใช้งาน List emptyList = new List(); var result = emptyList.DefaultIfEmpty(); // คืนลำดับที่มี { 0.0 } ตัวอย่

  19. ดัชนีบนและล่างของอาร์เรย์ 3 มิติใน C#

    ภาพรวม เมธอด GetUpperBound(dimension) และ GetLowerBound(dimension) ใช้ดึง ดัชนีสูงสุด และ ดัชนีต่ำสุด ของมิติที่ระบุในอาร์เรย์ (0-based index) รูปแบบการใช้งาน int[,,] arr = new int[2, 3, 4]; // ขนาด: 2x3x4 // มิติ 0: ดัชนี 0 ถึง 1 (Upper=1, Lower=0) // มิติ 1: ดัชนี 0 ถึง 2 (Upper=2, Lower=0) // ม

  20. OverflowException ใน C#

    ภาพรวม OverflowException โยนเมื่อ ค่าพารามิเตอร์อยู่นอกขอบเขตของชนิดข้อมูล เช่น พยายามแปลงสตริงที่มีค่ามากเกินไปให้เป็น int ตัวอย่างที่ทำให้เกิด OverflowException using System; class Demo { static void Main() { string str = 757657657657657; // มากกว่า Int32.MaxValue int re

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:64/134  20-คอมพิวเตอร์/Page Goto:1 58 59 60 61 62 63 64 65 66 67 68 69 70