Roadmap › C# Language — Basics to Advanced › Module: Methods and Functional C#
Lessons in this course (38)
21. Method parameters: ref, out, in, params, optional, named
Basic★ Must knowLesson 21 of 38
csharp
void Swap(ref int a, ref int b) { (a, b) = (b, a); } // caller's variables change
bool TryParseAge(string s, out int age) => int.TryParse(s, out age); // must assign
double Length(in Vector3 v) => Math.Sqrt(v.X * v.X); // read-only by reference (avoids copy of big struct)
int Sum(params int[] xs) => xs.Sum(); // Sum(1,2,3)
void Send(string to, string subject = "Hi", bool urgent = false) { }
int x = 1, y = 2;
Swap(ref x, ref y);
Send("a@b.com", urgent: true); // named argument skips optional ones- Pass by value (default): for reference types the reference is copied, so the method can mutate the object but cannot make the caller point to a different one — unless you use
ref.