Difference between Ref and Out keywords

พรกมล เอี้ยพิน 650710570

What is Keyword ?

คำสำคัญ (keyword) คือคำที่แสดงการกระทำบางอย่างล่วงหน้า หรือกำหนดคุณสมบัติบางอย่างที่เฉพาะ ทำให้ไม่สามารถตั้งเป็นชื่อตัวแปร/object ได้

ref และ out เป็น keyword สำหรับการส่งค่า arguments ในรูปแบบที่เรียกว่า Pass-by-reference หมายถึง การส่งผ่านข้อมูลที่ขึ้นกับหน่วยความจำนั้นโดยตรง ดังนั้นการเปลี่ยนแปลงใดๆ ในฟังก์ชันจะมีผลกับตัวแปร เช่นกัน

Ref keyword

ref - เป็นคีย์เวิร์ดที่ใช้ในการส่งค่า argument ในรูปแบบ pass by reference โดยวิธีใช้ ref คือ การส่งค่าโดยที่ต้องมีการกำหนดค่าในคลาส main ไว้ก่อน แล้วส่งค่านั้นให้เป็นพารามิเตอร์ต่อไป ซึ่งเมื่อมีการเปลี่ยนแปลงค่าตัวแปร ในเมธอดที่ถูกเรียกใช้ จะส่งผลถึงตัวแปรที่ส่งกลับไป main ด้วย

using System;

class GFG {

    // Main Method
    public static void Main()
    {

        // Assign string value
        string str = "Geek";

        // Pass as a reference parameter
        SetValue(ref str);

        // Display the given string
        Console.WriteLine(str);
    }

    static void SetValue(ref string str1)
    {

        // Check parameter value
        if (str1 == "Geek") {
            Console.WriteLine("Hello!!world");
        }

        // Assign the new value
        // of the parameter
        str1 = "GeeksforGeeks";
    }
}

Out keyword

out - เป็นคีย์เวิร์ดสำหรับส่งค่า argument ไปยังเมธอดในรูปแบบ pass by reference โดยที่ไม่จำเป็นต้องกำหนดค่าไว้ล่วงหน้า แต่ต้องกำหนดการเปลี่ยนแปลงค่าใน parameter ในเมธอดก่อนจะส่งค่ากลับเมธอด main และโดยการส่งค่าจะไม่มีการเปลี่ยนแปลงคุณสมบัติ (property)

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;
    }
}

ความแตกต่างระหว่าง 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 ดังตัวอย่าง ต่อไปนี้

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;
}

C - มีการส่งข้อมูลแบบ pass by reference แต่ไม่ได้ใช้คีย์เวิร์ดว่า ref หรือ out ใน C จะใช้สิ่งที่เรียกว่า pointer ซึ่งวิธีการอาจไม่ได้เหมือนทั้งหมด แต่ได้ผลลัพธ์คล้ายเคียงกัน ดังตัวอย่าง ต่อไปนี้

#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;
}

Python - ไม่มีคีย์เวิร์ด ref และ out เช่นเดียวกับ 2 ภาษาที่กล่าวมา แต่มีการส่งแบบ pass-by-object-reference ซึ่งใกล้เคียงกับภาษา C# โดยการมองข้อมูลที่ต้องการส่งให้เป็น object ทั้งหมด แบ่งได้เป็น 2 ประเภท

  • mutable หมายถึง การเปลี่ยนแปลงค่าในฟังก์ชันส่งผลต่อ object เพราะมี object เดียว ได้แก่ list, dict, set.

  • immutable หมายถึง การเปลี่ยนแปลงค่าในฟังก์ชันจะไม่ส่งผลต่อ object ภายนอก (main) ได้แก่ int, string, tuple.

# 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]

Presentation

Slide

Reference

What is keyword ? :

ankita_saini(2562). C# | Keywords. สืบค้นเมื่อวันที่ 5 ตุลาคม 2567 จาก https://www.geeksforgeeks.org/c-sharp-keywords/?ref=shm

อธิบายความหมายของ ref :

Microsoft(2567). ref(C# Reference). สืบค้นเมื่อวันที่ 5 ตุลาคม 2567 จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

อธิบายความหมายของ out :

Microsoft(2567). out(C# Reference). สืบค้นเมื่อวันที่ 5 ตุลาคม 2567 จาก https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out

เปรียบเทียบความแตกต่างระหว่าง ref และ out :

ankita_saini(2567). Difference between Ref and Out keywords in C#. สืบค้นเมื่อวันที่ 5 ตุลาคม 2567 จาก https://www.geeksforgeeks.org/difference-between-ref-and-out-keywords-in-c-sharp/

การส่ง pass-by-reference ของ java :

javatpoint(n.d.). C++ vs Java. สืบค้นเมื่อวันที่ 6 ตุลาคม 2567 จาก https://www.javatpoint.com/cpp-vs-java

การส่ง pass-by-reference ของ c :

tanisha1803(2566). Pass By Reference In C. สืบค้นเมื่อวันที่ 6 ตุลาคม 2567 จาก https://www.geeksforgeeks.org/pass-by-reference-in-c/

การส่ง pass-by-reference ของ python :

rajukumar19(2567). Pass by reference vs value in Python. สืบค้นเมื่อวันที่ 6 ตุลาคม 2567 จาก https://www.geeksforgeeks.org/pass-by-reference-vs-value-in-python/

Last updated