ref - เป็นคีย์เวิร์ดที่ใช้ในการส่งค่า argument ในรูปแบบ pass by reference โดยวิธีใช้ ref คือ การส่งค่าโดยที่ต้องมีการกำหนดค่าในคลาส main ไว้ก่อน แล้วส่งค่านั้นให้เป็นพารามิเตอร์ต่อไป ซึ่งเมื่อมีการเปลี่ยนแปลงค่าตัวแปร ในเมธอดที่ถูกเรียกใช้ จะส่งผลถึงตัวแปรที่ส่งกลับไป main ด้วย
usingSystem;classGFG{ // Main MethodpublicstaticvoidMain(){ // Assign string valuestringstr="Geek"; // Pass as a reference parameterSetValue(refstr); // Display the given stringConsole.WriteLine(str);}staticvoidSetValue(refstringstr1){ // Check parameter valueif(str1=="Geek"){Console.WriteLine("Hello!!world");} // Assign the new value // of the parameterstr1="GeeksforGeeks";}}
Out keyword
out - เป็นคีย์เวิร์ดสำหรับส่งค่า argument ไปยังเมธอดในรูปแบบ pass by reference โดยที่ไม่จำเป็นต้องกำหนดค่าไว้ล่วงหน้า แต่ต้องกำหนดการเปลี่ยนแปลงค่าใน parameter ในเมธอดก่อนจะส่งค่ากลับเมธอด main และโดยการส่งค่าจะไม่มีการเปลี่ยนแปลงคุณสมบัติ (property)
ความแตกต่างระหว่าง ref และ out
keyword
ref
out
การกำหนดค่าก่อนส่งไปเมธอด
จำเป็นต้องกำหนดค่าก่อนส่ง
ไม่จำเป็นต้องกำหนดค่าก่อนส่ง
การกำหนดค่าก่อนส่งกลับ main
ไม่จำเป็นต้องกำหนดค่า
จำเป็นต้องกำหนดค่า
ทิศทางการส่งผ่านข้อมูล
ส่งผ่าน 2 ทิศทาง
ส่งผ่านทิศทางเดียว
เปรียบเทียบกับ Java, C, Python
ในกรณีนี้ ขอเปรียบเทียบกับการส่งข้อมูลในรูปแบบ pass by reference ของทั้ง 3 ภาษา
Java - ไม่มีการส่งแบบ pass by reference อย่างที่ ref กับ out ทำ เพราะในการส่งข้อมูล primitive จะส่งแบบ pass by value แต่ถ้าหากต้องการให้ค่าเปลี่ยนแปลงคล้ายกับการส่งข้อมูล pass by reference สามารถใช้วิธีการสร้าง object ดังตัวอย่าง ต่อไปนี้
C- มีการส่งข้อมูลแบบ pass by reference แต่ไม่ได้ใช้คีย์เวิร์ดว่า ref หรือ out ใน C จะใช้สิ่งที่เรียกว่า pointer ซึ่งวิธีการอาจไม่ได้เหมือนทั้งหมด แต่ได้ผลลัพธ์คล้ายเคียงกัน ดังตัวอย่าง ต่อไปนี้
using System;
class GFG {
// Main method
static public void Main()
{
// Declaring variable
// without assigning value
int G;
// Pass variable G to the method
// using out keyword
Sum(out G);
// Display the value G
Console.WriteLine("The sum of" +
" the value is: {0}", G);
}
// Method in which out parameter is passed
// and this method returns the value of
// the passed parameter
public static void Sum(out int G)
{
G = 90;
G += G;
}
}
The sum of the value is: 180
public class Example {
public static void main(String[] args) {
int a = 10;
modifyPrimitive(a);
System.out.println("After modifyPrimitive: " + a); // ยังเป็น 10 อยู่
MyObject obj = new MyObject();
obj.value = 10;
modifyObject(obj);
System.out.println("After modifyObject: " + obj.value); // เปลี่ยนเป็น 20
}
public static void modifyPrimitive(int x) {
x = 20; // ไม่ส่งผลกระทบต่อ a ใน main
}
public static void modifyObject(MyObject obj) {
obj.value = 20; // ส่งผลกระทบต่อ obj ใน main
}
}
class MyObject {
public int value;
}
After modifyPrimitive: 10
After modifyObject: 20
#include <stdio.h>
void modify(int *a) {
*a = 20; // เปลี่ยนค่าของตัวแปรที่ส่งผ่านพอยน์เตอร์
}
int main() {
int x = 10;
printf("Before modify: %d\n", x);
modify(&x); // ส่งที่อยู่ของ x ไปยังฟังก์ชัน
printf("After modify: %d\n", x); // x จะถูกเปลี่ยนเป็น 20
return 0;
}
Before modify: 10
After modify: 20
# Immutable example
def modify_immutable(x):
x = 20 # เปลี่ยนค่า แต่เป็นการสร้างตัวแปรใหม่ภายในฟังก์ชัน
print("Inside immutable function:", x)
a = 10
modify_immutable(a)
print("Outside immutable function:", a, "\n") # ยังเป็น 10 อยู่
# Mutable example
def modify_mutable(lst):
lst.append(4) # เปลี่ยนแปลงค่าภายในออบเจ็กต์จริง
print("Inside mutable function:", lst)
b = [1, 2, 3]
modify_mutable(b)
print("Outside mutable function:", b) # เปลี่ยนเป็น [1, 2, 3, 4]