ในภาษา C# จะมีเมธอดอยู่ 2 ประเภท
1. predefined method มันคือเมธอดที่สร้างมากับไลบรารี่มาตรฐานของภาษา C# ซึ่งจะอยู่ในคลาส ยกตัวอย่างเช่น System.WriteLine() นี่บ่งบอกว่าเมธอด WriteLine นั้นอยู่ในคลาส System
2. user-defined method เป็นเมธอดที่เขียนขึ้นเองโดยโปรแกรมเมอร์
User-defined method
เป็นเมธอดที่ถูกสร้างขึ้นเพื่อทำงานบางอย่างโดยผู้ใช้
รูปแบบ
access_modifier type identifier (parameter1, parameter2, ... ) { // statements return result; }
อธิบายส่วนต่าง ๆ ของการประกาศเมธอด
access_modifier : บ่งบอกถึงระดับการเข้าถึงได้ของเมธอดมรคลาส โดยจะมีอยู่ 4 แบบคือ public protected private และ internal ถ้าหากคุณปล่อยให้มันว่างปล่าวนั้นจะเป็นแบบ protected
type : เป็นประเภทของข้อมูลที่คุณอยากจะส่งค่ากลับ มันสามารถเป้นชนิดข้อมูลพื้นฐานในภาษา C# หรือเป็นอ็อบเจ็ค เช่น int float String Class_Name เป็นต้น เมื่อเมธอดไม่มีค่าอะไรที่จะส่งกลับเราจะใช้คำสั่ง void แทนเหมือนที่คุณเห็นในรูปแบบที่สอง
parameters : เป็นค่าของตัวแปรที่ส่งเข้ามาในเมธอด และเป็นทางเลือก return เป็นคำสั่งที่ใช้ส่งค่าข้อมูลกลับ ยกตัวอย่างเช่น เมธอดที่มี return เป็น int นั้นค่าที่ส่งกลับจะต้องเป็นตัวเลขจำนวนเต็ม
static: คำสั่ง static ถูกใช้ประกาศหลังจาก access modifier ถ้าเมธอดที่ถูกสร้างขึ้นนั้นจำเป็นที่จะต้องแชร์บางอย่างที่เหมือนกันสำหรับทุก ๆ อ็อบเจ็คที่สร้างจากคลาส
การสร้างเมธอด
ตัวอย่าง
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace creat_method { class Program { static void Main(string[] args) { String name = "Henry"; int sex = 0; String sport = "Football"; Explore_Sports(name, sex, sport); // ในการส่งค่า ส่งครบ 3 ค่า Explore_Sports("Girud", 1, "Goft"); Explore_Sports("Carl", 0, "Muzzle"); Console.ReadLine(); } static void Explore_Sports(String n,int s,String sp)//รับค่าพารามิเตอร์ 3 ค่า { if (s == 0) { Console.WriteLine("Name : "+ n + " Sex : Male Sport : " + sp ); } else { Console.WriteLine("Name : " + n + " Sex : Female Sport : " + sp); } } } }
ผลการรัน
Name : Henry Sex : Male Sport : Football
Name : Girud Sex : Female Sport : Goft
Name : Carl Sex : Male Sport : Muzzle
สร้างเมธอดที่มีการส่งค่ากลับ
เมธอดนั้นจะต้องมีค่าที่จะส่งกลับไปนำไปใช้ในส่วนที่ถูกเรียกของโปรแกรม
ตัวอย่าง
using System; namespace creat_method_return { class Program { static void Main(string[] args) { int a = 10; int b = 25; Console.WriteLine(getText(a) + getSum(a)); Console.WriteLine(getmutiply(a, b)); Console.WriteLine(getEndText()); bool y = (getSum(a) == 55); Console.WriteLine("boolean Y = {0} ", y); Console.ReadLine(); } static String getText(int a) { return "a = " + a + " sum_loop " + a + " : "; } static string getmutiply(int a ,int b) { int sum = 0; sum = a + b; return "a + b " + sum; } static int getSum(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } static String getEndText() { return "Program End."; } } }
ผลการรัน
a = 10 sum_loop 10 : 55
a + b 35
Program End.
boolean Y = True
static void MyMethod(string country = "Norway") { Console.WriteLine(country);}static void Main(string[] args){ MyMethod("Sweden"); MyMethod("India"); MyMethod(); MyMethod("USA");}// Sweden// India// Norway// USA
cr. www.w3schools.com