ทุกตัวแปรใน C# มี Type เฉพาะ กำหนดขนาด Memory, ช่วงค่า และ Operation ที่ทำได้ Syntax <data_type> <variable_list>; ตัวอย่างประกาศพื้นฐาน int x, y; // หลายตัวแปร Type เดียวกัน float z; // Float short p; // Short double salary; // Double char grade;
ทุกตัวแปรใน C# มี Type เฉพาะ กำหนดขนาด Memory, ช่วงค่า และ Operation ที่ทำได้ Syntax <data_type> <variable_list>; ตัวอย่างประกาศพื้นฐาน int x, y; // หลายตัวแปร Type เดียวกัน float z; // Float short p; // Short double salary; // Double char grade;
Prefix Increment/Decrement (++a, --a) เพิ่ม/ลดค่าก่อน แล้วค่อยคืนค่าใหม่ พฤติกรรม int a = 10; int b = ++a; // a = 11, b = 11 (เพิ่มก่อน แล้วคืนค่า) int x = 10; int y = --x; // x = 9, y = 9 (ลดก่อน แล้วคืนค่า) โค้ดตัวอย่าง using System; class Program { static void Main() { int a
Params Keyword ใช้เมื่อไม่ทราบจำนวนอาร์กิวเมนต์ล่วงหน้า ทำให้ส่งได้ทั้ง Array และค่าเดี่ยวๆ Syntax public int AddElements(params int[] numbers) { } โค้ดตัวอย่างเต็ม using System; namespace Program { class ParamArray { public int AddElements(params int[] arr) {
Two-Dimensional Array (2D Array) เป็นอาร์เรย์ของอาร์เรย์ จัดเก็บในรูปตาราง แถวและคอลัมน์ Syntax // ประกาศ int[,] arrayName; // ประกาศและกำหนดขนาด int[,] arr = new int[3, 2]; // 3 แถว, 2 คอลัมน์ // ประกาศและกำหนดค่าเริ่มต้น int[,] arr = new int[3, 2] { {0, 0}, {1, 2}, {2, 4} }; โ
var คือ Implicitly Typed Local Variable - ให้ Compiler สรุป Type จากค่าที่ Assign (Compile-time Type Inference) ตัวอย่างพื้นฐาน using System; namespace Demo { class Program { static void Main(string[] args) { var myInt = 5; // int var myStr
Thread.Abort() ใช้ทำลาย Thread โดยโยน ThreadAbortException ไม่สามารถ Catch ได้ (จะ Re-throw อัตโนมัติหลัง finally) ตัวอย่าง using System; using System.Threading; namespace MultithreadingApplication { class ThreadCreationProgram { public static void CallToChildThread() {
ใช้ Dns.GetHostEntry() และ IPHostEntry.AddressList Property โค้ดตัวอย่าง using System; using System.Net; class Program { static void Main() { string hostName = Dns.GetHostName(); Console.WriteLine(Hostname: + hostName); IPHostEntry hostEntry = Dns.GetHostE
#error Directive สร้าง Compile-time Error ขึ้นเอง หยุดการ Build ทันที #if (!DEBUG) #error This code requires DEBUG mode! #endif #warning Directive สร้าง Compile-time Warning แสดงเตือน แต่ Build ผ่านได้ #if RELEASE #warning Performance optimizations disabled in RELEASE build #endif ตัวอย่าง #erro
ใช้ Property Name ของ Thread.CurrentThread โค้ดตัวอย่าง using System; using System.Threading; namespace Demo { class MyClass { static void Main(string[] args) { Thread thread = Thread.CurrentThread; thread.Name = My Thread; C
Preprocessor Directives เริ่มด้วย # ใช้ควบคุมการ Compile แบบ Conditional (Compile-time) Directives หลัก #define SYMBOL - กำหนด Symbol #undef SYMBOL - ยกเลิก Symbol #if SYMBOL - ทดสอบ Symbol #elif SYMBOL - Else If #else - Else #endif - จบเงื่อนไข ตัวอย่าง #define ONE #undef TWO using System; na
Postfix Increment/Decrement (++, -- หลังตัวแปร) คืนค่าเดิมก่อน แล้วค่อยเพิ่ม/ลด พฤติกรรม int a = 10; int b = a++; // b = 10, a = 11 (คืนค่าเก่า แล้วค่อย ++) int x = 10; int y = x--; // y = 10, x = 9 (คืนค่าเก่า แล้วค่อย --) โค้ดตัวอย่าง using System; class Program { static void Main()
Interface ICollection ใน C# ICollection (Non-generic) เป็น Base Interface สำหรับ Non-generic Collections ทั้งหมดใน System.Collections กำหนด Size, Enumerators, Synchronization Properties Propertyคำอธิบาย Countจำนวน Element ใน Collection IsSynchronizedThread-safe หรือไม่ SyncRootObject สำหรับ Lock
Interface IEnumerable ใน C# IEnumerable (Non-generic) เป็น Base Interface สำหรับ Collection ที่สามารถ Enumerate (วนลูป) ได้ มีเมธอดเดียว GetEnumerator() คืน IEnumerator Definition public interface IEnumerable { IEnumerator GetEnumerator(); } การทำงานกับ Foreach Compiler แปลง foreach (var ite
Interface IList ใน C# IList (Non-generic) แทนคอลเลกชันที่เข้าถึงด้วย Index ได้ มีขนาดคงที่หรือไดนามิกได้ อยู่ใน System.Collections Legacy - ใช้ IList<T> แทน Properties Propertyคำอธิบาย Countจำนวน Element IsFixedSizeขนาดคงที่หรือไม่ IsReadOnlyRead-only หรือไม่ IsSynchronizedThread-safe หรือไ
Interface ICloneable ใน C# ICloneable ให้วิธีมาตรฐานในการสร้าง Copy (Clone) ของ Object มีเมธอดเดียวคือ Clone() Definition public interface ICloneable { object Clone(); } Shallow Copy vs Deep Copy ประเภทคำอธิบายICloneable Default Shallow CopyCopy ค่า Field ทั้งหมด: Value Types คัดลอกค่า, Re
Interface IStructuralComparable ใน C# IStructuralComparable รองรับการเปรียบเทียบโครงสร้าง (Structural Comparison) ของ Collection Objects (Array, Tuple) คือเปรียบเทียบ Element ตามลำดับ ไม่ใช่ Reference เมธอดเดียว int CompareTo(object other, IComparer comparer); คืนค่า: 0 (มากกว่า) ตาม Sort Order
การแสดงตัวเลขแบบสามเหลี่ยม (Pascals Triangle) ใน C# ใช้ 2D Array คำนวณค่า Pascals Triangle: แต่ละค่า = ผลรวมของค่าด้านบนซ้ายและบนขวา ขอบซ้าย-ขวา = 1 อัลกอริทึม สร้าง 2D Array ขนาด [n, n] Loop แถว (i) 0 ถึง n-1 Loop คอลัมน์ (j) 0 ถึง i ถ้า j==0 หรือ j==i → ค่า = 1 (ขอบ) อื่นๆ → arr[i,j] = arr[i-1,j
ชนิดข้อมูลของ Array (Array Data Types) ใน C# Array ใน C# สามารถสร้างจากชนิดข้อมูลใดก็ได้: Primitive Types, Reference Types, User-defined Types การประกาศ Array // Syntax dataType[] arrayName; // ตัวอย่าง int[] intArray; // Integer Array double[] doubleArray; // Double Array string[]
Parameter Arrays (params) ใน C# params Keyword ใช้เมื่อไม่ทราบจำนวน Argument ล่วงหน้า ช่วยให้ส่ง Argument ได้หลายค่า หรือ Array ได้ ไวยากรณ์ returnType MethodName(params type[] parameterName) { // parameterName เป็น Array } ตัวอย่าง: Sum แบบ Param Array using System; namespace Program {
Conditional Statements (คำสั่งเงื่อนไข) ใน C# ใช้ควบคุม Flow ของโปรแกรมตามเงื่อนไข Boolean 5 ประเภทหลัก #Statementคำอธิบาย 1ifBoolean Expression ตามด้วย Statement(s) หนึ่งหรือหลาย 2if-elseif พร้อม else สำหรับกรณี False 3nested ifif/else if ภายใน if/else if อีกชั้น 4switchเปรียบเทียบ Variable กับร