Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. Key-based I/O Collections ใน C# (SortedList)

    Key-based I/O Collection ใน C# คือ SortedList<TKey, TValue> SortedList = Key-Value Pairs เรียงตาม Key เข้าถึงได้ทั้ง Key และ Index s.Add(Sub1, Physics); s.Add(Sub2, Chemistry); s.Add(Sub3, Biology); s.Add(Sub4, Java); ตัวอย่าง using System; using System.Collections; namespace Demo { class

  2. Identifiers ใน C# (ตัวระบุชื่อ)

    Identifier คือชื่อสำหรับ Class, Variable, Function หรือ User-defined Items กฎการตั้งชื่อ ขึ้นต้นตัวอักษร ตามด้วย ตัวอักษร/ตัวเลข/Underscore (_) ตัวแรกไม่ใช่ตัวเลข ไม่得有 Space หรือ Symbol: ? - + ! @ # % ^ & * ( ) [ ] { } . ; : / \ (Underscore ใช้ได้) ห้ามเป็น C# Keyword ตัวอย่าง Identifiers C

  3. Dynamic Data Types ใน C#

    Dynamic Type เก็บค่า Type ใดก็ได้ Type Checking เกิดขึ้นที่ Run-time (ไม่ใช่ Compile-time) Syntax dynamic <variable_name> = value; ตัวอย่าง dynamic val1 = 100; dynamic val2 = 5; dynamic val3 = 20; Dynamic Types คล้าย Object Types แต่ Object Type Checking ที่ Compile-time ส่วน Dynamic ที่ Run-t

  4. Overloaded Indexers ใน C# (Indexer หลายรูปแบบ)

    Indexer ทำให้ Object เข้าถึงได้แบบ Array (用 [ ]) Indexers สามารถ Overload ได้ และรับ Parameters หลายตัว ชนิดต่างกันได้ ตัวอย่าง Overloaded Indexers using System; namespace IndexerApplication { class IndexedNames { private string[] namelist = new string[size]; static public i

  5. Literals ใน C# (ค่าคงที่)

    Literals คือค่าคงที่ (Fixed Values) ที่โปรแกรมไม่เปลี่ยนแปลงระหว่างรัน Constants มีได้ทุก Basic Data Types 1. Integer Literals Decimal หรือ Hexadecimal (Prefix 0x/0X) 20 // int 30u // unsigned int 30l // long 2. Float Literals มี Integer Part, Decimal Point, Fractional Part, Exponent Part 2.6

  6. Lambda Expressions ใน C# (นิพจน์แลมบ์ดา)

    Lambda Expression ใน C# อธิบาย Pattern มี Token => เรียกว่า goes to Operator ตัวอย่าง using System; using System.Collections.Generic; class Demo { static void Main() { List<int> list = new List<int>() { 21, 17, 40, 11, 9 }; int res = list.FindIndex(x => x %

  7. วิธีเปรียบเทียบสอง List ว่าเท่ากันหรือไม่ใน C#

    ใช้ LINQ Except() หาความต่างระหว่างสอง List หากผลลัพธ์ว่าง = เท่ากัน ตัวอย่าง: List ที่ต่างกัน using System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List<string> list1 = new List<string>() {P, Q, R}; L

  8. วิธีแปลงเลขฐานสิบเป็นฐานแปด (Decimal to Octal) ใน C#

    ใช้ While Loop หารด้วย 8 ซ้ำๆ เก็บเศษ (Remainder) ไว้ในอาร์เรย์ แล้วนำมาแสดงจากท้ายสุด อัลกอริทึม while (dec != 0) { oct[i] = dec % 8; // เก็บเศษ dec = dec / 8; // หาร 8 i++; } โค้ดตัวอย่างเต็ม (Decimal 18 → Octal 22) = 0; j--) { Console.Write(oct[j]);

  9. วิธีแปลง 2D Array เป็น 1D Array ใน C#

    ใช้ Nested Loop วนลูปผ่าน 2D Array และเก็บค่าลง 1D Array ตามลำดับ โค้ดตัวอย่างเต็ม using System; namespace Program { class TwoDMatrix { static void Main(string[] args) { // 2D Array 2x2 int[,] a = new int[2, 2] { {1, 2},

  10. วิธีคัดลอกส่วนของ Array ไปยัง Array อื่นใน C#

    ใช้เมธอด Static Array.Copy() เพื่อคัดลอกส่วนของอาร์เรย์หนึ่งไปยังอีกอาร์เรย์หนึ่ง Syntax Array.Copy(sourceArray, destinationArray, length); sourceArray - อาร์เรย์ต้นทาง destinationArray - อาร์เรย์ปลายทาง length - จำนวนอิลิเมนต์ที่จะคัดลอก โค้ดตัวอย่างเต็ม using System; class Program { stat

  11. ความแตกต่างระหว่าง Method Overriding และ Method Hiding ใน C#

    Method Overriding (การลบล้าง/เขียนทับ) ใช้ virtual ใน Base Class และ override ใน Derived Class Polymorphism ทำงาน - เรียกเมธอดตาม Object จริง (Runtime) ใช้กับ Abstract Method ได้ ตัวอย่าง Overriding using System; namespace PolymorphismApplication { abstract class Shape { public a

  12. วิธีนับจำนวนรายการใน C# List

    ใช้ Property Count ของ List เพื่อนับจำนวนรายการ ตัวอย่าง List<string> myList = new List<string>() { electronics, clothing, appliances, accessories }; int count = myList.Count; // 4 โค้ดตัวอย่างเต็ม using System; using System.Collections.Generic; class Program {

  13. วิธีสร้าง Dictionary ใน C#

    Dictionary<TKey, TValue> เป็นคอลเลกชัน Key-Value อยู่ใน Namespace System.Collections.Generic ขั้นตอนสร้างและเพิ่มข้อมูล IDictionary<int, int> dict = new Dictionary<int, int>(); dict.Add(1, 44); dict.Add(2, 34); dict.Add(3, 66); dict.Add(4, 47); dict.Add(5, 76); โค้ดตัวอย่างเต็ม u

  14. Value Type vs Reference Type ใน C#

    Value Type (ชนิดค่า) เก็บข้อมูลโดยตรง (Contains data directly) มาจาก System.ValueType จัดเก็บใน Stack ตัวอย่าง: int, char, float, double, bool, struct, enum การกำหนดค่า = คัดลอกข้อมูล (Copy by value) Reference Type (ชนิดอ้างอิง) เก็บที่อยู่หน่วยความจำ (Reference to memory location) จัดเก็บใน Hea

  15. วิธีสร้าง Directory (โฟลเดอร์) ด้วย C#

    ใช้คลาส System.IO.Directory สำหรับสร้าง ย้าย ลบ โฟลเดอร์ ขั้นตอน Import namespace: using System.IO; ใช้ Directory.CreateDirectory() ตัวอย่าง: สร้างโฟลเดอร์ string myDir = @D:\NEW; if (!Directory.Exists(myDir)) { Directory.CreateDirectory(myDir); } ตัวอย่าง: สร้าง Sub-directory string mySubD

  16. Variable Arguments (Varargs) ใน C#

    ใช้คีย์เวิร์ด params เพื่อรับจำนวนอาร์กิวเมนต์ที่ไม่คงที่ Syntax static int Multiply(params int[] numbers) สามารถเรียกใช้ได้ทั้ง 1, 2 หรือหลายอาร์กิวเมนต์: int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10); int mulVal3 = Multiply(2, 3, 4, 5); โค้ดตัวอย่างเต็ม using System; class Program {

  17. วิธีประกาศ Array ใน C#

    ประกาศ Array ใน C#: int[] runs = new int[10]; // ประกาศขนาด 10 ประกาศและกำหนดค่าเริ่มต้นในบรรทัดเดียว: int[] runs = new int[5] {99, 92, 95, 0, 0}; หรือย่อได้เป็น: int[] runs = {99, 92, 95}; โค้ดตัวอย่างเต็ม using System; namespace Program { class Demo { static void Main(string[] a

  18. การทำ Encapsulation ใน C# อย่างไร

    Encapsulation คือการซ่อนรายละเอียดการทำงานภายใน (Implementation Details) และเปิดเผยเฉพาะสิ่งที่จำเป็น ผ่าน Access Specifiers Access Specifiers ใน C# Specifierขอบเขตการเข้าถึง publicเข้าถึงได้จากทุกที่ privateเข้าถึงได้เฉพาะในคลาสเดียวกัน (ค่าเริ่มต้น) protectedเข้าถึงได้ในคลาสเดียวกันและคลาสลูก

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:25/134  20-คอมพิวเตอร์/Page Goto:1 19 20 21 22 23 24 25 26 27 28 29 30 31