Sobes.tech
Junior — Middle

How is data transfer carried out within a function or method?

sobes.tech AI

Answer from AI

In C#, data can be passed to a function or method in several ways:

  • By value — the argument's value is copied. Changes inside the method do not affect the original data.
  • By reference (ref) — a reference to the variable is passed, and changes inside the method reflect on the original.
  • Output parameter (out) — used to return data from the method back to the caller; the variable does not need to be initialized before the call.
  • Parameters with the in keyword — passed by reference but read-only inside the method.

Example:

void UpdateValue(ref int x) {
    x = 10; // will change the value of the variable passed by reference
}

int a = 5;
UpdateValue(ref a);
// now a == 10