Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#
C#
  1. โปรแกรม C# ตรวจสอบ Palindrome (คำอ่านซ้ำ)

    ในการตรวจสอบว่าสตริงเป็น Palindrome (อ่านจากหน้าหลังได้เหมือนกัน) ให้หาค่าผกผันของสตริง แล้วเปรียบเทียบ หาค่าผกผัน: Array.Reverse() เปรียบเทียบด้วย Equals() หาก true = เป็น Palindrome ตัวอย่าง using System; namespace Demo { class Program { static void Main(string[] args) {

  2. โปรแกรม C# คำนวณแฟกทอเรียลด้วย Recursion

    คำนวณแฟกทอเรียล (Factorial) ด้วยฟังก์ชัน recursive checkFact() ถ้าค่าเป็น 1 คืนค่า 1 (เพราะ 1! = 1): if (n == 1) return 1; ถ้าไม่ใช่ เรียกฟังก์ชันซ้ำ: Iteration 1: 5 * checkFact(4) Iteration 2: 4 * checkFact(3) Iteration 3: 3 * checkFact(2) Iteration 4: 2 * checkFact(1) ตัวอย่าง using System;

  3. การใช้งาน as Operator ใน C#

    ออเปอเรเตอร์ as ทำการแปลงชนิดระหว่างชนิดที่เข้ากันได้ (compatible types) คล้าย cast operation แต่ทำได้เฉพาะ reference conversions, nullable conversions, และ boxing conversions ไม่สามารถทำ user-defined conversions ได้ ตัวอย่าง string s = obj[i] as string; ตัวอย่างเต็ม using System; public class

  4. การใช้งาน is Operator ใน C#

    ออเปอเรเตอร์ is ใน C# ตรวจสอบว่า runtime type ของออบเจ็กต์เข้ากับชนิดที่กำหนดหรือไม่ ไวยากรณ์ expr is type expr = นิพจน์, type = ชื่อชนิดข้อมูล ตัวอย่าง using System; class One { } class Two { } public class Demo { public static void Test(object obj) { One x; Two y;

  5. โปรแกรม C# ตรวจสอบเลขหาร 2 ลงตัว

    ในการตรวจสอบว่าเลขหาร 2 ลงตัวหรือไม่ ให้หาเศษจากการหารด้วย 2 ถ้าเศษเป็น 0 = หาร 2 ลงตัว if (num % 2 == 0) { Console.WriteLine(Divisible by 2); } else { Console.WriteLine(Not divisible by 2); } ตัวอย่าง using System; using System.Collections.Generic; using System.Linq; using System.Text;

  6. โปรแกรม C# ตรวจสอบเลขเฉพาะ (Prime Number)

    ในการตรวจสอบว่าเลขเป็นจำนวนเฉพาะ (Prime) ใช้ลูปและตรวจสอบว่าเศษจากการหารเป็น 0 กี่ครั้ง for (int i = 1; i <= n; i++) { if (n % i == 0) a++; } ตัวนับ a จะเพิ่มแค่ 2 ครั้งถ้าเป็นจำนวนเฉพาะ (หารด้วย 1 และตัวมันเอง) ดังนั้นถ้า a == 2 คือเลขเฉพาะ ตัวอย่างเต็ม using System; namespace Demo {

  7. การใช้ Sleep Method ใน C#

    เมธอด Sleep ของเธรด (thread) ใช้หยุดการทำงานของเธรดเป็นเวลาที่กำหนด การใช้งาน หากต้องการหยุดเป็นวินาที: int sleepfor = 2000; // 2000 milliseconds = 2 วินาที Thread.Sleep(sleepfor); ตัวอย่างเต็ม using System; using System.Threading; namespace MyApplication { class ThreadCreationProgram {

  8. การใช้ Right Shift Operator (>>) ใน C#

    Right Shift Operator (>>) เลื่อนค่าของ operand ด้านซ้ายไปทางขวาตามจำนวนบิตที่ operand ด้านขวาระบุ ตัวอย่าง using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { int a = 60; // 60 = 0011 1100 int b = 0; b =

  9. วิธีการใช้งาน Stack Class ใน C#

    คลาส Stack ใน C# ใช้แทนคอลเลกชันแบบ last-in, first-out (LIFO) ใช้เมื่อต้องการนำสมาชิกที่เพิ่มล่าสุดออกก่อน เช่น กองหนังสือ เมธอดของ Stack Class No.Method & Description 1Clear() - ลบสมาชิกทั้งหมด 2Contains(object) - ตรวจสอบว่ามีสมาชิกอยู่หรือไม่ 3Peek() - ดูสมาชิกบนสุดโดยไม่ลบ 4Pop() - นำสม

  10. Interface ใน C#

    Interface กำหนด properties, methods, events ซึ่งเป็นสมาชิกของ interface มีแต่การประกาศ (declaration) เท่านั้น คลาสที่ derive มาต้องนิยามการทำงาน ช่วยให้โครงสร้างมาตรฐานที่คลาสลูกต้องปฏิบัติ การประกาศ Interface public interface ITransactions { void showTransaction(); double getAmount(); }

  11. วิธีการวนลูปผ่านอาร์เรย์ใน C#

    ในการวนลูปผ่านอาร์เรย์ใน C# ใช้ลูปได้ทุกแบบ while, do-while, for, foreach ซึ่งมีค่าเริ่มต้นและสิ้นสุดสำหรับตรวจสอบค่าผ่านการวนซ้ำ ตัวอย่าง for loop using System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int[] n = new int[10];

  12. วิธีการรวมอาร์เรย์สองตัว (Concatenate Arrays) ใน C#

    ในการรวมอาร์เรย์สองตัวใน C# ให้ประกาศและ initialize อาร์เรย์ก่อน (ตัวอย่าง string array): string[] str = new string[] { Hello, World }; ใช้เมธอด string.Join() เพื่อรวม: string.Join( , str); ตัวอย่างเต็ม using System; class Program { static void Main() { string[] str = new string[]

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

    Attributes ใช้เพิ่ม metadata เช่น compiler instructions, comments, descriptions, methods, classes ให้กับโปรแกรม .NET Framework อนุญาตให้สร้าง custom attributes ที่เก็บข้อมูล declarative และดึงได้ในขณะรันไทม์ Custom attribute ใหม่ต้องสืบทอดจาก System.Attribute [AttributeUsage(AttributeTargets.Clas

  14. Control Statements (คำสั่งควบคุม) ใน C#

    การไหลของการควบคุมโปรแกรมระบุโดย control statements ใน C# ประกอบด้วย: if statement if (boolean_expression) { // statement(s) executes if true } if-else statement if (boolean_expression) { // executes if true } else { // executes if false } for loop for (init; condition; increment) {

  15. Decision Making Statements (คำสั่งตัดสินใจ) ใน C#

    Decision making statements ต้องการให้โปรแกรมเมอร์ระบุเงื่อนไขหนึ่งหรือมากกว่า เพื่อให้โปรแกรมประเมิน ทำคำสั่งหนึ่งหรือหลายคำสั่ง หากเงื่อนไขเป็น true และอาจมีคำสั่งสำหรับกรณี false ประเภทของ Decision Making Statements No.Statement & Description 1if statement - นิพจน์ boolean ตามด้วยคำสั่งอ

  16. Delegates (ตัวแทน) ใน C# คืออะไร

    Delegate ใน C# คือการอ้างอิงไปยังเมธอด เป็นตัวแปรชนิดอ้างอิงที่เก็บ reference ไปยังเมธอด สามารถเปลี่ยน reference ได้ในขณะรันไทม์ Delegate ใช้โดยเฉพาะสำหรับ implement events และ callback methods ทุก delegate สืบทอดมาจาก System.Delegate โดยนัย การประกาศ Delegate delegate <return type> <del

  17. Custom Exceptions (ข้อยกเว้นกำหนดเอง) ใน C#

    ใน C# สร้าง user-defined exception ได้ง่าย คลาสข้อยกเว้นผู้ใช้ต้อง derive จากคลาส Exception เรียกว่า Custom exceptions ตัวอย่าง: สร้าง TempIsZeroException using System; namespace Demo { class TestTemperature { static void Main(string[] args) { Temperature temp = new Temper

  18. Regular Expressions (นิพจน์ปกติ) ใน C#

    Regular expression เป็นรูปแบบ (pattern) ที่จับคู่กับข้อความนำเข้า .NET Framework มี regex engine สำหรับการจับคู่ Pattern ประกอบด้วยตัวอักษร literal, operators, หรือ constructs ตัวอย่าง: จับคู่คำที่ขึ้นต้นด้วย S using System; using System.Text.RegularExpressions; namespace Demo { class Program

  19. Destructors (ตัวทำลาย) ในโปรแกรม C#

    Destructor เป็นเมธอดสมาชิกพิเศษของคลาส ที่ทำงานเมื่อออบเจ็กต์ของคลาสนั้นเลิกใช้งาน (out of scope) ชื่อเหมือนคลาสแต่มี tilde (~) นำหน้า: public Demo() { // constructor Console.WriteLine(Object is being created); } ~Demo() { // destructor Console.WriteLine(Object is being deleted); } ตัวอ

  20. วิธีการส่งผ่านพารามิเตอร์ใน C# (Parameter Passing Methods)

    เมื่อเรียกเมธอดที่มีพารามิเตอร์ ต้องส่งผ่านพารามิเตอร์ด้วยหนึ่งใน 3 วิธี: 1. Reference Parameters (พารามิเตอร์แบบอ้างอิง) คัดลอกการอ้างอิงไปยังตำแหน่งหน่วยความจำของอาร์กิวเมนต์เข้าไปในพารามิเตอร์ การเปลี่ยนแปลงพารามิเตอร์จะส่งผลต่ออาร์กิวเมนต์ต้นฉบับ 2. Value Parameters (พารามิเตอร์แบบค่า) คัดลอกค

Total 2668 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:7/134  20-คอมพิวเตอร์/Page Goto:1 2 3 4 5 6 7 8 9 10 11 12 13