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

    ภาพรวม ใช้เมธอด String.Replace แทนที่ตัวอักษรหรือสตริงย่อยทั้งหมดในสตริง คืนค่าสตริงใหม่ (String เป็น immutable) โค้ดตัวอย่าง using System; public class Program { public static void Main() { string str = "dem* text"; Console.WriteLine($"Original: {str}");

  2. หาค่าเฉลี่ยของ Integer Array ใน C# (Alternative)

    ภาพรวม บทความนี้ซ้ำกับบทความก่อนหน้า (1004092264) เนื้อหาเกือบเหมือนกัน นี่คือเวอร์ชันปรับปรุงพร้อมเพิ่มเติม โค้ดตัวอย่าง using System; using System.Linq; public class Program { public static void Main() { int[] myArr = { 8, 4, 2, 5, 9, 14 }; // Manual calculation int

  3. หาค่าเฉลี่ยของสมาชิกใน Array ด้วย C#

    ภาพรวม คำนวณค่าเฉลี่ย (Average) ของอาร์เรย์จำนวนเต็ม โดยหาผลรวมแล้วหารด้วยจำนวนสมาชิก โค้ดตัวอย่าง using System; public class Program { public static void Main() { int[] myArr = { 45, 23, 55, 15, 8, 4, 2, 5, 9, 14 }; int len = myArr.Length; int sum = 0; for (

  4. หาตัวอักษร 10 ตัวแรกของ String ใน C#

    ภาพรวม ใช้ Substring(0, 10) ดึง substring ตัวแรก 10 ตัวอักษร ต้องตรวจสอบความยาวก่อนเพื่อป้องกัน Exception โค้ดตัวอย่าง using System; public class Demo { public static void Main() { string str = "Cricket is a religion in India!"; // วิธีที่ 1: Substring (ต้องเช็ค Len

  5. คูณเลขฐานสอง (Binary Multiplication) ใน C#

    ภาพรวม คูณเลขฐานสองโดยจัดการเป็นตัวเลข decimal ที่ประกอบด้วย 0 และ 1 ใช้หลักการคูณแบบ Manual (shift and add) โค้ดตัวอย่าง using System; class Demo { public static void Main() { long val1 = 11100; // binary as decimal long val2 = 10001; Console.WriteLine($"Binary

  6. หาตัวอักษรแรกของ String ใน C#

    ภาพรวม ใช้ Substring(0, 1) หรือ Indexer [0] หรือ LINQ First() เพื่อดึงตัวอักษรแรก โค้ดตัวอย่าง using System; public class Demo { public static void Main() { string str = "Welcome to the Planet!"; // วิธีที่ 1: Substring (คืนค่า string) string firstChar1 = st

  7. บวกเลขฐานสอง (Binary Addition) ใน C# โดยไม่ใช้ Method

    ภาพรวม ทำการบวกเลขฐานสองโดยตรงใน Main method โดยไม่แยกเป็นเมธอดแยกต่างหาก ใช้ Logic เหมือนกันกับการบวกแบบ Manual โค้ดตัวอย่าง using System; class Demo { public static void Main(string[] args) { long val1 = 11010; // binary as decimal digits long val2 = 10100; Console.

  8. หาความยาวและ Rank ของ Jagged Array ใน C#

    ภาพรวม Jagged Array (int[][]) คือ Array ของ Arrays - แต่ละแถวมีความยาวต่างกันได้ Rank = 1 เสมอ (เพราะเป็น 1D array ของ arrays) โค้ดตัวอย่าง using System; public class Program { public static void Main() { int[][] jagged = new int[][] { new int[] { 0, 0 }, new i

  9. หา Index ของ Item ใน List แบบ Single Step ใน C#

    ภาพรวม ใช้ List<T>.FindIndex พร้อม Lambda expression เพื่อหาดัชนีของ item ที่ตรงเงื่อนไขในบรรทัดเดียว โค้ดตัวอย่าง using System; using System.Collections.Generic; public class Program { public static void Main() { List<string> myList = new List<string> { "Footba

  10. หา Rank (จำนวนมิติ) ของ Array ใน C#

    ภาพรวม ใช้ Property Rank ของ Array เพื่อหาจำนวนมิติ (Dimensions) - 1D=1, 2D=2, 3D=3, ฯลฯ โค้ดตัวอย่าง using System; class Demo { static void Main() { // 1D Array int[] arr1D = new int[5]; Console.WriteLine($"1D Array Rank: {arr1D.Rank}"); // 1 // 2D

  11. คูณเลขสองจำนวนด้วย Recursion ใน C#

    ภาพรวม คูณ a × b โดยการบวก a ซ้ำ b ครั้ง ใช้ recursion โค้ดตัวอย่าง (Naive Recursion) using System; class Calculation { public static void Main() { int val1 = 10, val2 = 20; Demo d = new Demo(); int res = d.Product(val1, val2); Console.WriteLine($"{val1} x

  12. คำนวณปริมาตรและพื้นที่ผิวทรงกลมใน C#

    ภาพรวม สูตรทรงกลม: พื้นที่ผิว = 4πr², ปริมาตร = (4/3)πr³ โค้ดตัวอย่าง using System; public class Demo { public static void Main() { double r = 15; // รัศมี // ใช้ Math.PI แทน 22/7 เพื่อความแม่นยำ double area = 4 * Math.PI * r * r; double volume = (4.0 / 3.0) *

  13. หาขนาดของ List ใน C#: Count vs Capacity

    ภาพรวม List<T> มีสองคุณสมบัติที่สำคัญ: Count (จำนวนองค์ประกอบจริง) และ Capacity (ขนาดภายในที่จัดสรรไว้) - ต่างกันมาก โค้ดตัวอย่าง using System; using System.Collections.Generic; public class Demo { public static void Main() { var list = new List<string>(); Console

  14. หาองค์ประกอบที่พบบ่อยที่สุด (Most Frequent) ใน String ด้วย C#

    ภาพรวม นับความถี่ของแต่ละตัวอักษรในสตริง แล้วหาค่าที่มีความถี่สูงสุด โค้ดตัวอย่าง using System; class Demo { static int MAX_CHARS = 256; static void CalculateFrequency(string s, int[] count) { foreach (char c in s) { count[c]++; } } public static void

  15. ตรวจสอบเลขหาร 2 ลงตัวใน C#

    ภาพรวม ใช้ Modulo operator (%) ตรวจสอบเศษ หาร 2 ลงตัว = เลขคู่ (Even number) โค้ดตัวอย่าง using System; namespace Demo { class Test { static void Main(string[] args) { int num = 18; if (num % 2 == 0) { Console.WriteLine("Divisible by 2 (Ev

  16. ประกาศ Constant Array ใน C#: ใช้ readonly แทน const

    ภาพรวม ใน C# ไม่สามารถใช้ const กับอาร์เรย์ได้ เพราะอาร์เรย์เป็น reference type และ const ต้องเป็น compile-time constant ใช้ readonly หรือ ReadOnlyCollection<T> แทน วิธีที่ 1: static readonly (แนะนำสำหรับ Array) public static class Constants { // สามารถกำหนดค่าได้ที่ runtime (ใน construc

  17. หาขนาดตัวแปรโดยไม่ใช้ sizeof ใน C#

    ภาพรวม ใน C# ใช้ sizeof ได้เฉพาะใน unsafe context สำหรับ primitive types วิธีปลอดภัยคือใช้ BitConverter.GetBytes แปลงค่าเป็น byte array แล้ววัด Length โค้ดตัวอย่าง using System; class Demo { public static void Main() { int x = 42; // ใช้ sizeof (ต้องเป็น unsafe context สำหรับ

  18. นับความถี่ของแต่ละตัวอักษรใน String ด้วย C#

    ภาพรวม ใช้ Array ขนาด 256 (ASCII) หรือ Dictionary นับจำนวนครั้งที่แต่ละตัวอักษรปรากฏในสตริง โค้ดตัวอย่าง: ใช้ Array (ASCII) using System; class Demo { static int MAX_CHARS = 256; static void CalculateFrequency(string s, int[] count) { foreach (char c in s) { count[c]+

  19. บวกเลขฐานสอง (Binary Addition) ใน C#

    ภาพรวม บวกเลขฐานสองโดยจัดการเป็นตัวเลข decimal ที่ประกอบด้วยตัวเลข 0 และ 1 เท่านั้น แล้วทำการบวกแบบ manual digit-by-digit พร้อม carry โค้ดตัวอย่าง using System; class Demo { public static void Main() { long val1 = 11110; // binary representation as decimal number long val2 = 1

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:38/134  20-คอมพิวเตอร์/Page Goto:1 32 33 34 35 36 37 38 39 40 41 42 43 44