C# Method Parameters and Arguments
Parameters are variables listed in a method's definition, while arguments are the actual values passed to the method when it is called. Parameters allow methods to accept input and perform operations based on that input.
Key Topics
1. Defining Parameters
Example: Method with Parameters
static void GreetUser(string name)
{
Console.WriteLine("Hello, " + name + "!");
}
2. Passing Arguments
Example: Calling Method with an Argument
GreetUser("Alice"); // Outputs: Hello, Alice!
3. Multiple Parameters
Example: Method with Multiple Parameters
static void DisplayInfo(string name, int age)
{
Console.WriteLine($"Name: {name}, Age: {age}");
}
DisplayInfo("Bob", 30); // Outputs: Name: Bob, Age: 30
Key Takeaways
- Parameters define what inputs a method can accept.
- Arguments are the actual values passed to the method.
- Methods can have multiple parameters to accept various inputs.
- Proper parameter definition enhances method flexibility and reusability.