Parameters and Arguments
Parameters allow you to pass data into methods, making them flexible and reusable.
Parameters vs Arguments
- Parameters: Variables declared in the method definition
- Arguments: Actual values passed when calling the method
csharp1// 'name' is a parameter2static void Greet(string name)3{4 Console.WriteLine($"Hello, {name}!");5}67// "Alice" is an argument8Greet("Alice");9Greet("Bob");
Single Parameter
csharp1static void PrintDouble(int number)2{3 Console.WriteLine($"{number} doubled is {number * 2}");4}56PrintDouble(5); // Output: 5 doubled is 107PrintDouble(12); // Output: 12 doubled is 24
Multiple Parameters
Separate parameters with commas:
csharp1static void PrintSum(int a, int b)2{3 int sum = a + b;4 Console.WriteLine($"{a} + {b} = {sum}");5}67PrintSum(3, 5); // Output: 3 + 5 = 88PrintSum(10, 20); // Output: 10 + 20 = 30
Order Matters
csharp1static void Divide(double numerator, double denominator)2{3 if (denominator != 0)4 {5 Console.WriteLine($"{numerator} / {denominator} = {numerator / denominator}");6 }7 else8 {9 Console.WriteLine("Cannot divide by zero!");10 }11}1213Divide(10, 2); // Output: 10 / 2 = 514Divide(2, 10); // Output: 2 / 10 = 0.2 (order changed!)
Different Parameter Types
csharp1static void CreateUser(string username, int age, bool isAdmin)2{3 Console.WriteLine($"Creating user: {username}");4 Console.WriteLine($"Age: {age}");5 Console.WriteLine($"Admin: {isAdmin}");6}78CreateUser("alice", 25, false);9CreateUser("admin", 30, true);
ref Parameters
The ref keyword passes a reference, allowing the method to modify the original variable:
csharp1static void DoubleValue(ref int number)2{3 number = number * 2;4}56int value = 10;7Console.WriteLine($"Before: {value}"); // Before: 1089DoubleValue(ref value); // Must use 'ref' when calling too10Console.WriteLine($"After: {value}"); // After: 20
ref vs Regular Parameters
csharp1// Without ref - original is unchanged2static void TryDouble(int number)3{4 number = number * 2; // Only changes local copy5}67int x = 5;8TryDouble(x);9Console.WriteLine(x); // Still 51011// With ref - original is modified12static void ActuallyDouble(ref int number)13{14 number = number * 2; // Changes original15}1617int y = 5;18ActuallyDouble(ref y);19Console.WriteLine(y); // Now 10
out Parameters
The out keyword is for returning multiple values from a method:
csharp1static void Divide(int dividend, int divisor, out int quotient, out int remainder)2{3 quotient = dividend / divisor;4 remainder = dividend % divisor;5}67int q, r;8Divide(17, 5, out q, out r);9Console.WriteLine($"17 / 5 = {q} remainder {r}"); // 17 / 5 = 3 remainder 21011// Modern syntax: declare inline12Divide(23, 7, out int quotient, out int remainder);13Console.WriteLine($"23 / 7 = {quotient} remainder {remainder}");
out Must Be Assigned
csharp1static void GetMinMax(int[] numbers, out int min, out int max)2{3 min = numbers[0];4 max = numbers[0];56 foreach (int n in numbers)7 {8 if (n < min) min = n;9 if (n > max) max = n;10 }11 // min and max MUST be assigned before method ends12}
in Parameters (Read-Only Reference)
The in keyword passes by reference but prevents modification:
csharp1static void PrintDetails(in LargeStruct data)2{3 Console.WriteLine(data.Value);4 // data.Value = 10; // Error! Cannot modify 'in' parameter5}
This is mainly used for large structs to avoid copying while ensuring immutability.
Practical Examples
Greeting with Parameters
csharp1static void Greet(string name, string greeting)2{3 Console.WriteLine($"{greeting}, {name}!");4}56Greet("Alice", "Hello"); // Hello, Alice!7Greet("Bob", "Good morning"); // Good morning, Bob!8Greet("Charlie", "Hi"); // Hi, Charlie!
Drawing Shapes
csharp1static void DrawRectangle(int width, int height, char symbol)2{3 for (int row = 0; row < height; row++)4 {5 for (int col = 0; col < width; col++)6 {7 Console.Write(symbol);8 }9 Console.WriteLine();10 }11}1213DrawRectangle(5, 3, '*');14Console.WriteLine();15DrawRectangle(10, 2, '#');
Calculating with Parameters
csharp1static void CalculateCircle(double radius)2{3 double area = Math.PI * radius * radius;4 double circumference = 2 * Math.PI * radius;56 Console.WriteLine($"Radius: {radius}");7 Console.WriteLine($"Area: {area:F2}");8 Console.WriteLine($"Circumference: {circumference:F2}");9}1011CalculateCircle(5);12CalculateCircle(10);
Validation with ref
csharp1static void ClampValue(ref int value, int min, int max)2{3 if (value < min)4 value = min;5 else if (value > max)6 value = max;7}89int score = 150;10ClampValue(ref score, 0, 100);11Console.WriteLine(score); // 1001213int temp = -20;14ClampValue(ref temp, 0, 50);15Console.WriteLine(temp); // 0
Swap Values
csharp1static void Swap(ref int a, ref int b)2{3 int temp = a;4 a = b;5 b = temp;6}78int x = 10, y = 20;9Console.WriteLine($"Before: x={x}, y={y}"); // x=10, y=2010Swap(ref x, ref y);11Console.WriteLine($"After: x={x}, y={y}"); // x=20, y=10
Summary
In this lesson, you learned:
- Parameters are variables in the method definition
- Arguments are values passed when calling the method
- Multiple parameters are separated by commas
refallows methods to modify the original variableoutis for returning multiple valuesinpasses by reference but is read-only
Next, we'll learn about return values.