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

    ประกาศ Tuple โดยไม่กำหนดค่า: Tuple<string, string> myTuple; ตรวจสอบค่าใน Tuple ว่าเป็น null หรือไม่: ตัวอย่าง using System; namespace Demo { class Program { static void Main(string[] args) { Tuple<int, string> tuple = new Tuple<int, string>(10, null);

  2. วิธีแทรกข้อมูลใน List ตำแหน่งที่กำหนดใน C#

    ใช้เมธอด Insert() เพื่อแทรกข้อมูลใน List ที่มีอยู่แล้ว ขั้นแรก เพิ่มข้อมูล: List<int> list = new List<int>(); list.Add(989); list.Add(345); list.Add(654); list.Add(876); list.Add(234); list.Add(909); แทรกข้อมูลที่ตำแหน่งที่ 4 (ดัชนี 3): // inserting element at 4th position list.Insert(3,

  3. วิธีสร้าง Delegate Instance ใน C#

    ใช้คีย์เวิร์ด new เพื่อสร้าง Delegate Instance รูปแบบการส่งอาร์กิวเมนต์คล้ายการเรียกเมธอด แต่ไม่ต้องส่งพารามิเตอร์ ตัวอย่าง: public delegate void printString(string s); printString ps1 = new printString(WriteToScreen); สามารถสร้าง Delegate ด้วย Anonymous Method ได้: delegate void Del(string str); De

  4. วิธีรวม List สองอันใน C#

    ใช้เมธอด AddRange() เพื่อรวม List สองอัน กำหนด List แรก: var list1 = new List<string>(); list1.Add(Keyboard); list1.Add(Mouse); กำหนด List ที่สอง: var list2 = new List<string>(); list2.Add(Hard Disk); list2.Add(Pen Drive); รวมทั้งสอง List: list1.AddRange(list2); ตัวอย่าง using System.Col

  5. วิธีทำ Operator Overloading ใน C#

    Operator Overloading คือการกำหนดการทำงานของตัวดำเนินการสำหรับประเภทข้อมูลที่ผู้ใช้สร้างเอง ใช้คีย์เวิร์ด operator ตามด้วยสัญลักษณ์ตัวดำเนินการ ตัวอย่าง using System; namespace Demo { class Box { private double length; private double breadth; private double height;

  6. วิธีวนลูปผ่าน List ใน C#

    ประกาศ List และเพิ่มข้อมูล: var products = new List<string>(); products.Add(Belts); products.Add(T-Shirt); products.Add(Trousers); ใช้ลูป foreach เพื่อวนลูป: foreach(var p in products) { Console.WriteLine(p); } ตัวอย่าง using System; using System.Collections.Generic; public class Demo {

  7. วิธีล้างข้อมูล List ใน C#

    ใช้เมธอด Clear() เพื่อล้างข้อมูลใน List ขั้นแรก สร้าง List และเพิ่มข้อมูล: List<string> myList = new List<string>() { one, two, three, four, five, six }; ล้าง List: myList.Clear(); ตัวอย่าง using System; using System.Collections.Generic; public class Program { public static void

  8. วิธีวนลูปผ่าน Tuple ใน C#

    ขั้นแรก ประกาศ Tuple และเพิ่มค่า: Tuple<int, string> tuple = new Tuple<int, string>(100, Tom); ใน C# การวนลูปผ่าน Tuple สามารถเข้าถึงสมาชิกแต่ละตัวได้โดยตรง: tuple.Item1 // รายการแรก tuple.Item2 // รายการที่สอง // แสดง Tuple ทั้งหมด Console.WriteLine(tuple); ตัวอย่าง using System; using

  9. วิธีหาขนาดของอาร์เรย์ใน C#

    ขั้นแรก กำหนดอาร์เรย์: int[] arr = {6, 3, 8, 4}; ใช้คุณสมบัติ Length เพื่อหาขนาดของอาร์เรย์: arr.Length ดูโค้ดเต็มตัวอย่างด้านล่าง: ตัวอย่าง using System; namespace Demo { public class Demo { public static void Main(string[] args) { int[] arr = {6, 3, 8, 4}; Conso

  10. ตรวจสอบว่าสตริงเป็นตัวเลขใน C# - IsDigit, TryParse, Regex

    หลายวิธีตรวจสอบว่าสตริงประกอบด้วยตัวเลขเท่านั้น วิธีที่ 1: LINQ All + Char.IsDigit (Simple) char.IsDigit(c)); // False วิธีที่ 2: int.TryParse / long.TryParse (แนะนำ - รองรับ Negative, Overflow) string str = 3456; bool success = int.TryParse(str, out int value); // success = true, value = 3456

  11. หา Substrings ทั้งหมดของสตริงใน C#

    การสร้าง Substrings ทั้งหมด (All Substrings) ของสตริง ใช้ Nested Loop กับ Substring Method อัลกอริทึม: ทุกความยาว × ทุกตำแหน่งเริ่มต้น using System; public class Demo { public static void Main() { string str = pqrz; for (int len = 1; len <= str.Length; len++) // คว

  12. Loop ผ่านอาร์เรย์ใน C# - for, foreach, Span

    การ Iterate อาร์เรย์ใน C# มีหลายวิธี เลือกตาม Use Case 1. for Loop (Index-based) - ควบคุม Index ได้ int[] arr = { 34, 56, 12 }; for (int i = 0; i = 0; i--) { Console.WriteLine(arr[i]); } // Step 2 for (int i = 0; i < arr.Length; i += 2) { Console.WriteLine(arr[i]); } 2. foreach (Read-on

  13. Traversal ใน Singly Linked List ด้วย C# LinkedList<T>

    System.Collections.Generic.LinkedList<T> เป็น Doubly Linked List Implementation ใน .NET ใช้สำหรับ Insert/Delete ที่เร็ว (O(1)) เมื่อมี Node Reference Basic Operations using System; using System.Collections.Generic; public class Demo { public static void Main() { var list = n

  14. Reflection ใน C# - รันไทม์ Type Inspection และ Dynamic Invocation

    Reflection ใน .NET ใช้ Namespace System.Reflection เพื่อตรวจสอบและจัดการ Type, Assembly, Module, Member ใน Runtime หลักการใช้งานหลัก Inspect Type Metadata (Properties, Methods, Fields, Attributes) Dynamic Object Creation (Activator.CreateInstance) Dynamic Method Invocation (MethodInfo.Invoke) Late

  15. แปลง ArrayList เป็น Array ใน C# - ToArray Method

    ArrayList เป็น Legacy Non-generic Collection (ใช้ Object) แปลงเป็น Typed Array ด้วย ToArray(Type) ตัวอย่างพื้นฐาน using System; using System.Collections; public class Program { public static void Main() { ArrayList arrList = new ArrayList(); arrList.Add(one); arrLi

  16. Class Variables และ Static Variables ใน C#

    C# มี 2 ประเภทตัวแปรระดับ Class: Static Variables (Static Fields) และ Instance Variables (Instance Fields) Static Variables (Class Variables) เป็นของ Type เอง ไม่ใช่ Instance แชร์ค่าให้ทุก Instance Count = 0; } public class Program { public static void Main() { var s1 = new Static

  17. การสืบทอดคลาสใน C# - Inheritance Basics

    Inheritance (การสืบทอด) ให้ Derived Class ใช้ Members ของ Base Class ได้ ใช้ Keyword `:` Syntax พื้นฐาน // Base Class public class BaseClass { // Members... } // Derived Class public class DerivedClass : BaseClass { // Members + Inherited Members } ตัวอย่างครบถ้วน: Shape → Rectangle (Wi

  18. File Objects ใน C# - FileStream, FileMode, FileAccess, FileShare

    FileStream เป็น Low-level Stream สำหรับไฟล์ ให้ควบคุมการอ่าน/เขียนแบบ Byte-level ได้เต็มที่ Constructor Parameters var fs = new FileStream( path: test.dat, mode: FileMode.OpenOrCreate, access: FileAccess.ReadWrite, share: FileShare.None, bufferSize: 4096, useAsync: false);

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