Method Parameters
นิสิต หนูนวน 650710560
1. Named parameters
Named parameters คือ การส่ง parameters โดยระบุชื่อ parameter ในการส่ง ซึ่งมีข้อดีคือ ไม่ต้องระบุค่าตามลำดับของ parameters ที่กำหนดใน method
Named parameters เป็นสิ่งที่อำนวยความสะดวกในการเขียน Code โดยเฉพาะ method ที่มี parameter จำนวนมาก ทำให้ง่ายต่อการทำความเข้าใจ และลดความสับสนใจการเขียน
1.1 ข้อควรระวัง !!
หากมีการใช้งานร่วมกับ Positional parameters จะต้องส่งค่าของ Positional parameters มาก่อน Named parameters เสมอ
1.2 Example
public class GFG {
// addstr มี 3 parameters
public static void addstr(string s1, string s2, string s3)
{
string result = s1 + s2 + s3;
Console.WriteLine("Final string is: " + result);
}
// Main Method
static public void Main()
{
// เรียกใช้ Method โดยใช้ Named parameter ในการส่งค่า
addstr(s1: "Geeks", s2: "for", s3: "Geeks");
}
}
Output
Final string is: GeeksforGeeks
หากใส่ Named parameters ก่อน Positional parameters จะทำให้ compiler แจ้งเตือนข้อผิดพลาด เช่น
ตัวอย่าง
public class GFG
{
public static void addstr(string s1, string s2, string s3)
{
string result = s1 + s2 + s3;
Console.WriteLine("Final string is: " + result);
}
static public void Main()
{
// กำหนด Named parameters ก่อน
// Positional parameters
// ทำให้แจ้งเตือน error
addstr(s1: "Geeks", s3: "for", "Geeks");
}
}
s3: "for"
เป็น Named arguments ซึ่งประกาศก่อน "Geeks"
ที่เป็น positional argument ทำให้เกิดการแจ้งเตือนข้อผิดพลาดจาก compiler
1.3 ความแตกต่าง
ความแตกต่างของ Named parameters ระหว่าง C, Python และ Java มีดังนี้
C ไม่รองรับการใช้ Named Parameters
ซึ่งในการส่งค่า Parameters จำนวนหลายค่า ต้องแน่ใจว่าจำนวน Parameters ที่ส่ง เท่ากับจำนวน Parameter ใน function และมีการเรียงลำดับค่าที่ถูกต้อง
#include<stdio.h>
int add(int first_number, int second_number); pe
void main() {
int first_number = 4;
int second_number = 6;
// เรียกใช้ function และ กำหนดค่า parameter และ
// เรียงลำดับให้ตรงกับ parameters ใน function ที่เรียก
int sum = add(first_number, second_number);
printf("Sum of %d and %d is %d", first_number, second_number, sum);
}
int add(int a, int b) {
int c = a + b;
return c;
}
Output
Sum of 4 and 6 is 10
2. Reference parameters
Reference parameters หรือ Passing by reference ใน C# ใช้ keyword คือ ref
เป็นการส่งค่า parameter ไปยัง method โดยการอ้างอิงตัวแปรนั้นในการทำงาน ซึ่งหากมีการเปลี่ยนแปลงค่าใน method จะมีผลต่อตัวแปรนั้นเมื่อส่งค่ากลับไปยังตัวแปรต้นทาง ซึ่งก่อนที่จะใช้งาน จะต้องทำการกำหนดค่าของตัวแปรนั้นก่อน
2.1 ข้อควรระวัง !!
การใช้งาน Reference parameter ควรคำนึงผลลัพธ์ที่เกิดขึ้น เนื่องจากมีการเปลี่ยนแปลงค่าของตัวแปรเมื่อส่งกลับมายัง method ที่เรียกใช้ ทำให้อาจเกิดการแสดงผลที่ไม่ตรงตามความต้องการได้
2.2 Example
class GFG {
public static void Main()
{
// กำหนดค่าเริ่มต้นของตัวแปร
string val = "Dog";
// เรียกใช้ method และส่งค่าด้วย ref
CompareValue(ref val);
Console.WriteLine(val);
}
static void CompareValue(ref string val1)
{
// Compare the value
if (val1 == "Dog")
{
Console.WriteLine("Matched!");
}
// เปลี่ยนค่าของตัวแปรและส่งกลับ
val1 = "Cat";
}
}
Output
Matched!
Cat
2.3 ความแตกต่าง
ความแตกต่างของ Reference parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ไม่รองรับการใช้งาน Reference parameters หรือ Passing by reference แต่จะใช้ pointer เข้าถึงตำแหน่งของค่า ซึ่งอาจใช้ชื่อว่า Call by reference, Call by pointers, and Pass by pointers
#include <stdio.h>
//ฟังก์ชันจะรับค่าของพารามิเตอร์โดยใช้ pointer (*)
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int n1 = 5;
int n2 = 10;
// value before swapping
printf(" Before swapping : n1 is %d and n2 is %d\n", n1,n2);
// เรียกใช้ function และใช้ (&) เพื่อส่งที่อยู่ของตัวแปร n1,n2
swap(&n1, &n2);
// value after swapping
printf(" After swapping : n1 is %d and n2 is %d\n", n1,n2);
return 0;
}
Output
Before swapping : n1 is 5 and n2 is 10
After swapping : n1 is 10 and n2 is 5
3. Out Parameters
Out parameters ใน C# ใช้ keyword คือ out
เป็นการส่งค่า parameter ไปยัง method โดยการอ้างอิงตัวแปรนั้นในการทำงาน เช่นเดียวกับ reference parameters แต่มีความแตกต่างคือ ต้องมีการกำหนดค่าของตัวแปรภายใน method และไม่จำเป็นต้องกำหนดค่าของตัวแปรเริ่มต้นก่อนใช้งาน เพราะจะไม่มีผลต่อค่าของตัวแปรที่กำหนดใน method
3.1 Example
using System;
class GFG
{
static public void Main()
{
// สร้างตัวแปรโดยไม่ต้องหนดค่าของตัวแปร
int num;
// ส่งตัวแปร num ไปยัง method
// โดยใช้ keyword 'out'
AddNum(out num);
Console.WriteLine("The sum of"
+ " the value is: {0}", num);
}
// method จะส่งค่าของ num กลับไปยังตัวแปรต้นทาง
public static void AddNum(out int num)
{
// กำหนดค่าของตัวแปรใน method
num = 40;
num += num;
}
}
Output
The sum of the value is: 80
3.2 ความแตกต่าง
ความแตกต่างของ Out parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ไม่รองรับการส่ง multiple values จาก function โดยตรง แต่สามารถใช้ "Pass by pointers" ในการรับข้อมูลของตัวแปร จากการใช้ pointer อ้างอิงตำแหน่งของข้อมูล
#include<stdio.h>
void div(int a, int b, int *quotient, int *remainder) {
*quotient = a / b;
*remainder = a % b;
}
main() {
int a = 76, b = 10;
int q, r;
div(a, b, &q, &r);
printf("Quotient is: %d\nRemainder is: %d\n", q, r);
}
Output
Quotient is: 7
Remainder is: 6
4. Default or Optional Parameters
Optional Parameters คือ parameter ที่มีการกำหนดค่า default value ไว้ใน method ซึ่งหากเรียกใช้ method และส่ง parameter โดยไม่ระบุค่าของตัวแปรที่เป็น Optional parameter ระบบจะนำค่า default value ที่กำหนดใน method มาใช้
4.1 ข้อควรระวัง !!
การกำหนดค่าของ optional parameters จะต้องอยู่หลัง regular parameter เสมอ
4.2 Example
using System;
class GFG {
// method นี้มีการกำหนด parameter คือ
// regular parameters : ename , eid
// optional parameters : bgrp , dept
static public void detail(string ename,
int eid,
string bgrp = "A+",
string dept = "Review-Team")
{
Console.WriteLine("Employee name: {0}", ename);
Console.WriteLine("Employee ID: {0}", eid);
Console.WriteLine("Blood Group: {0}", bgrp);
Console.WriteLine("Department: {0}", dept);
}
// Main Method
static public void Main()
{
// ไม่กำหนดค่าให้กับ optional parameter
detail("XYZ", 123);
// กำหนดค่าให้กับ optional parameter
detail("ABC", 456, "B-");
detail("DEF", 789, "B+", "Software Developer");
}
}
Output
Employee name: XYZ
Employee ID: 123
Blood Group: A+
Department: Review-Team
Employee name: ABC
Employee ID: 456
Blood Group: B-
Department: Review-Team
Employee name: DEF
Employee ID: 789
Blood Group: B+
Department: Software Developer
4.3 ความแตกต่าง
ความแตกต่างของ Optional parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ไม่รองรับการใช้ Optional parameter แต่สามารถใช้ การทำ null pointer ในการตรวจสอบการส่งค่า parameter
#include <stdio.h>
int fun1(int *ptrvarB)
{
if (ptrvarB == NULL)
{
// Handle NULL pointer input
printf("It is null pointer");
}
else
{
printf("It is not a null pointer");
}
}
int main()
{
int *ptrvarA = NULL;
fun1(ptrvarA);
}
Output :
It is null pointer
5. Dynamic Parameters
Dynamic parameters คือ parameter ที่กำหนดขึ้นโดยใช้ keyword dynamic
ทำให้ไม่ต้องระบุชนิดของตัวแปร เนื่องจาก Compiler จะไม่มีการตรวจสอบชนิดของตัวแปรในขณะ compile-time แต่จะรับชนิดของตัวแปรในขณะ run time
Dynamic parameters มีประโยชน์สำหรับการจัดการกับชนิดข้อมูลที่ไม่แน่นอน ช่วยเพิ่มความยืดหยุ่นในการเขียน Code
5.1 Example
using System;
class program
{
// กำหนดตัวแปรที่ไม่มีชนิดข้อมูล โดยใช้ dynamic keyword
public static void add(dynamic r1, dynamic r2)
{
dynamic result = r1+r2;
Console.WriteLine(r1 + r2+" : "+result.GetType().ToString());
}
// Main method is called
static public void Main()
{
// ส่งค่าของตัวแปรไปยัง method
//String + String
add("H", "I");
add("Welcome to", " dynamic type");
//Integer + Interger
add(20, 20);
//Double + Double
add(20.5, 1.5);
//Integer + String
add(100, "fun");
}
}
Output
HI : System.String
Welcome to dynamic type : System.String
40 : System.Int32
22 : System.Double
100fun : System.String11
5.2 ความแตกต่าง
ความแตกต่างของ Dynamic parameters ระหว่าง C, Python และ Java มีดังนี้
C เป็นภาษาแบบ Static typing และไม่มี keyword สำหรับการเปลี่ยนแปลงค่า (dynamic
) แบบ C#
6. Value Parameters
Value parameters คือ parameter ที่ส่งข้อมูลหรือค่าของตัวแปรที่กำหนดขึ้น ไปยัง method ซึ่งหากมีการเปลี่ยนแปลงค่าของตัวแปรใน method จะไม่ส่งผลต่อค่าของตัวแปรต้นทางที่เรียกใช้
Value parameters เป็นการส่ง parameter แบบปกติในภาษา C#
6.1 Example
using System;
public class GFG {
static public void Main()
{
// กำหนดค่าของตัวแปร
string str1 = "Geeks";
string str2 = "geeks";
//เรียกใช้ method โดยส่ง parameter เป็นค่าของตัวแปร
string res = addstr(str1, str2);
Console.WriteLine(res);
}
public static string addstr(string s1, string s2)
{
return s1 + s2;
}
}
Output
Geeksgeeks
6.2 ความแตกต่าง
ความแตกต่างของ Value parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C รองรับ Value parameters ในชื่อ Pass by value
#include <stdio.h>
void swap (int a, int b)
{
int temp = a;
a = b;
b = temp;
printf("swap in function, a = %d, b = %d\n", a,b);
}
int main ()
{
int a = 10;
int b = 20;
printf ("Before swap, a = %d, b = %d\n", a, b);
swap (a, b);
printf ("After swap, a = %d, b = %d\n", a, b);
return 0;
}
Output :
Before swap, a = 10, b = 20
swap in function, a = 20, b = 10
After swap, a = 10, b = 20
7. Params
keyword params
เป็นการกำหนดให้สามารถส่งค่า argument ได้จำนวนหลายค่าตามที่ต้องการโดยไม่ต้องกำหนดจำนวนของ parameters ก่อน ซึ่งจะเป็นประโยชน์เมื่อไม่ทราบจำนวนของ argument ที่ต้องการจะส่ง
7.1 ข้อควรระวัง !!
ในหนึ่ง method สามารถใช้ params
ได้เพียงตัวเดียว และ parameter ที่ใช้ params
ต้องเป็นชนิด Array เท่านั้น และจะต้องกำหนดให้เป็น parameters สุดท้ายของ method
7.1 Example
using System;
class Param {
// ใช้ params keyword
public static int Add(params int[] numbers)
{
int sum = 0;
foreach (int number in numbers)
{
sum += number;
}
return sum;
}
static public void Main()
{
int result = Add(1, 2, 3, 4);
Console.WriteLine("Result = " + result);
}
}
// ไม่ใช้ params keyword (multiple overloads)
public static int Add(int a, int b)
{
return a + b;
}
public static int Add(int a, int b, int c)
{
return a + b + c;
}
public static int Add(int a, int b, int c, int d)
{
return a + b + c + d;
}
Output
Result = 10
7.2 ความแตกต่าง
ความแตกต่างของ Params ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ใช้ Variadic function สำหรับการรับ argument ที่ไม่จำกัดจำนวน ซึ่งฟังก์ชันที่ใช้บ่อยที่สุดอย่าง printf()
, scanf()
ก็เป็น Variadic function เนื่องจากสามารถส่งจำนวน argument ได้ตามต้องการ
โดยจะต้องทำการเพิ่ม #include <stdarg.h>
ไว้บน header และ ต้องกำหนด fixed argument อย่างน้อย 1 ค่า แล้วตามด้วย (...
) เพื่อให้ compiler เข้าใจว่า สามารถรับ arguments ที่ไม่จำกัดจำนวนได้
#include <stdio.h>
#include <stdarg.h>
// Variadic function to add numbers
int addition(int n, ...){
va_list args;
int i, sum = 0;
va_start (args, n);
for (i = 0; i < n; i++){
sum += va_arg (args, int);
}
va_end (args);
return sum;
}
int main(){
printf("Sum = %d ", addition(5, 1, 2, 3, 4, 5));
return 0;
}
Output
Sum = 15
Video Presentation
Slide Presentation
Resource
Key resource
Saini, A. (2019). C# | Method Parameters. GeeksforGeeks. https://www.geeksforgeeks.org/c-sharp-method-parameters/
Additional Resources
IronPdf. (2024). C# Optional Parameters (How It Works For Developers). https://ironpdf.com/blog/net-help/csharp-optional-parameters/
Javatpoint. (n.d.). C# Named and Optional Arguments. https://www.javatpoint.com/csharp-named-and-optional-arguments
Javatpoint. (n.d.). C# Out Parameter. https://www.javatpoint.com/c-sharp-out-parameter
Narayan, B. (2015). Different Types Of Method Parameters in C#. C#Corner.https://www.c-sharpcorner.com/UploadFile/efa3cf/different-types-of-method-parameters-in-C-Sharp/
Pal, V. (2023). Understanding Ref and Out Parameters in C#. Medium. https://medium.com/@vndpal/understanding-ref-and-out-parameters-in-c-3259e936a6eb
Shivakumar, S. (2023). C# Dynamic. EDUCBA. https://www.educba.com/c-sharp-dynamic/
Tutorials.EU. (n.d.). C# params in easy words. https://tutorials.eu/a-guide-to-using-csharp-params-to-work-with-parameters/
Wagner et al., (2024). Named and Optional Arguments (C# Programming Guide). Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments?redirectedfrom=MSDN
Wagner et al., (2024). Methods in C#. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/methods#passing-parameters
Last updated