(To pass an argument to a method by its reference) ซึ่งการใช้ ref จะอนุญาตให้เมธอดนั้นแก้ไขค่าของตัวแปรต้นฉบับได้
usingSystem;namespaceTomAndJerryGame{classProgram{staticvoidMain(string[]args){ //กำหนดคะแนนเริ่มต้นinttomScore=50;intjerryScore=45;Console.WriteLine($"Initial scores: Tom = {tomScore}, Jerry = {jerryScore}");ModifyScores(reftomScore,refjerryScore);Console.WriteLine($"Modified scores: Tom = {tomScore}, Jerry = {jerryScore}");} // ฟังก์ชัน ModifyScores สำหรับอัปเดตคะแนน โดยรับออบเจ็กต์ Character แบบอ้างอิง (ref)publicstaticvoidModifyScores(refinttomScore,refintjerryScore){tomScore+=15;jerryScore+=10;}}}
(To define a method signature to return a reference of the variable) การทำให้เมธอดสามารถส่งคืนตำแหน่งในหน่วยความจำของตัวแปรได้แทนที่จะส่งคืนแค่ค่าของตัวแปรนั้น
Output
3. ประกาศโครงสร้างเป็น ref struct
(To declare a struct as a ref struct) การประกาศ ref struct จะช่วยให้มั่นใจว่าโครงสร้างนี้จะถูกเก็บใน stack เท่านั้น การที่โครงสร้างอยู่ใน stack จะช่วยลดภาระในการจัดการหน่วยความจำและทำให้การเข้าถึงข้อมูลรวดเร็วขึ้น
Output
4. การอ้างอิงในท้องถิ่น
(As local reference) การสร้างตัวแปรที่เชื่อมโยงกับตัวแปรอื่นภายในพื้นที่เดียวกัน ถ้าเราเปลี่ยนแปลงค่าของตัวแปรที่เชื่อมโยง ตัวแปรเดิมก็จะเปลี่ยนไปด้วย
ในภาษา C สามารถทำสิ่งที่คล้ายกับ ref ได้ด้วยการใช้ pointer (ตัวชี้) ซึ่งเป็นการส่งตัวแปรผ่านการอ้างอิงที่ตรงไปตรงมา
pointer (ตัวชี้)
ตัวชี้ถูกกำหนดให้เป็นชนิดข้อมูลที่ได้รับมาซึ่งสามารถจัดเก็บที่อยู่ของตัวแปร C อื่นๆ หรือตำแหน่งหน่วยความจำ เราสามารถเข้าถึงและจัดการข้อมูลที่จัดเก็บในตำแหน่งหน่วยความจำนั้นได้โดยใช้ตัวชี้
Initial scores: Tom = 50, Jerry = 45
Modified scores: Tom = 65, Jerry = 55
using System;
namespace TomAndJerry {
class Character {
public int Score { get; set; }
public string Name { get; set; }
public Character(string name, int score) {
Name = name;
Score = score;
}
}
class Program {
static void Main(string[] args) {
// สร้างออบเจ็กต์ตัวละคร Tom และกำหนดคะแนนเริ่มต้น
Character tom = new Character("Tom", 2);
Console.WriteLine($"{tom.Name} initial score: {tom.Score}");
// เรียกใช้ฟังก์ชัน UpdateScores โดยส่งตัวแปร Tom แบบ ref
UpdateScores(ref tom);
Console.WriteLine($"After updating, {tom.Name} score: {tom.Score}");
}
// ฟังก์ชัน UpdateScores สำหรับอัปเดตคะแนน โดยรับออบเจ็กต์ Character แบบอ้างอิง (ref)
public static void UpdateScores(ref Character character) {
character.Score += 5;
}
}
}
Tom initial score: 2
After updating, Tom score: 7
using System;
namespace TomAndJerryGame
{
class Program
{
static void Main(string[] args)
{
int[] scores = { 20, 50, 80, 100 };
// ใช้ ref เพื่ออ้างอิงไปยังคะแนนที่ต้องการ (50) และรับการอ้างอิงกลับมา
ref int tomScore = ref FindScore(ref scores, 50);
Console.WriteLine("Tom's current score found: " + tomScore);
tomScore += 20;
Console.WriteLine("Tom's updated score: " + tomScore);
Console.WriteLine("\nUpdated Scores:");
foreach (var score in scores)
{
Console.WriteLine(score);
}
}
// ฟังก์ชัน FindScore สำหรับหาคะแนน โดยรับออบเจ็กต์ Character แบบอ้างอิง (ref) 1 ตัว
public static ref int FindScore(ref int[] scores, int target)
{
for (int i = 0; i < scores.Length; i++)
{
if (scores[i] == target)
{
Console.WriteLine("Score found! Updating Tom's score...");
return ref scores[i];
}
}
throw new InvalidOperationException("Score not found.");
}
}
}
using System;
namespace TomAndJerryGame
{
// กำหนด struct แบบ ref struct ที่จะจำกัดให้ถูกใช้งานเฉพาะกับ stack memory เท่านั้น
public ref struct TomJerryPosition
{
public int TomX, TomY;
public int JerryX, JerryY;
public TomJerryPosition(int tomX, int tomY, int jerryX, int jerryY)
{
TomX = tomX;
TomY = tomY;
JerryX = jerryX;
JerryY = jerryY;
}
public void DisplayPositions(string message)
{
Console.WriteLine(message);
Console.WriteLine($"Tom is at ({TomX}, {TomY})");
Console.WriteLine($"Jerry is at ({JerryX}, {JerryY})");
}
}
class Program
{
static void Main()
{
TomJerryPosition positions = new TomJerryPosition(10, 20, 30, 40);
positions.DisplayPositions("Initial Positions:");
positions.TomX += 5;
positions.JerryY -= 10;
positions.DisplayPositions("Updated Positions:");
}
}
}
Initial Positions:
Tom is at (10, 20)
Jerry is at (30, 40)
Updated Positions:
Tom is at (15, 20)
Jerry is at (30, 30)
using System;
namespace TomAndJerryGame
{
class Program
{
static void Main(string[] args)
{
// สร้างออบเจ็กต์ของตัวละคร Tom และ Jerry พร้อมคะแนนเริ่มต้น
Character tom = new Character("Tom", 30);
Character jerry = new Character("Jerry", 40);
Console.WriteLine("Before change:");
tom.Display();
jerry.Display();
// ใช้ ref เพื่ออ้างอิงถึงออบเจ็กต์ของ Tom โดยตรง
ref Character activeCharacter = ref tom;
activeCharacter.Score += 20;
Console.WriteLine("After changing Tom's score through reference:");
tom.Display();
jerry.Display();
}
}
class Character
{
public string Name { get; set; }
public int Score { get; set; }
public Character(string name, int score)
{
Name = name;
Score = score;
}
public void Display()
{
Console.WriteLine($"{Name} has a score of {Score}");
}
}
}
Before change:
Tom has a score of 30
Jerry has a score of 40
After changing Tom's score through reference:
Tom has a score of 50
Jerry has a score of 40
using System;
class Program
{
static void UpdateValue(ref int number)
{
number = 20;
}
static void Main()
{
int value = 10;
UpdateValue(ref value);
Console.WriteLine(value);
}
}
20
import java.io.*;
class updateValue {
int Number;
void updateValue() {
Number = 0;
}
static void update(updateValue ob) {
ob.Number++;
}
public static void main(String[] args)
{
updateValue ob = new updateValue();
System.out.println("Number value " + (ob.Number));
update(ob);
System.out.println("Updated Number value " + (ob.Number));
}
}
Number value 0
Updated Number value 1
#include <stdio.h>
void updateValue(int* number) {
*number = 20;
}
int main() {
int value = 10;
updateValue(&value);
printf("%d\n", value);
return 0;
}