_V
Help me explain the ref keycode
Hello guys, I'm new to c# , so sorry if I ask a dumb question
I have just read about keycode ref and it's said that It's needed to return a value from a method
But in this w3s tutorial they don't need ref to return the value, just need to call the method by replacing the value in the parameters
using System;
namespace MyApplication
{
class Program
{
static void MyMethod(string fname)
{
Console.WriteLine(fname + " Refsnes");
}
static void Main(string[] args)
{
MyMethod("Liam");
MyMethod("Jenny");
MyMethod("Anja");
}
}
}
This is the ref tutorial on windows
using System;
namespace ref_keyword {
class GFG {
// Main Method
static void Main(string[] args)
{
// Initialize a and b
int a = 10, b = 12;
// Display initial values
Console.WriteLine("Initial value of a is {0}", a);
Console.WriteLine("Initial value of b is {0}", b);
Console.WriteLine();
// Call addValue method by value
addValue(a);
// Display modified value of a
Console.WriteLine("Value of a after addition"+
" operation is {0}", a);
// Call subtractValue method by ref
subtractValue(ref b);
// Display modified value of b
Console.WriteLine("Value of b after "+
"subtraction operation is {0}", b);
}
// Define addValue
// Parameters passed by value
public static void addValue(int a)
{
a += 10;
}
// Define subtractValue
// Parameters passed by ref
public static void subtractValue(ref int b)
{
b -= 5;
}
}
}
Can someone explain this in a simple way to me, I feel really dumb
19 replies