This article is currently in the process of being translated into Polish (~21% done).
Method parameters
W poprzednim artykule omówiliśmy metody i otrzymaliśmy krótkie wprowadzenie do pojęcia parametrów metody/funkcji. W tym artykule zagłębimy się w ten temat we wszystkich jego odmianach. Jak wspomniano, metody mogą działać bez parametrów, ale zazwyczaj będą miały jeden lub więcej parametrów, które pomogą metodzie w realizacji jej zadania.
Już wcześniej widzieliśmy bardzo proste użycie parametrów w poprzednim artykule, tj. Metoda AddNumbers(), która bierze dwie liczby jako parametry i zwraca sumę tych liczb.
public int AddNumbers(int number1, int number2)
{
return number1 + number2;
}
Powyższy przykład bardzo pomaga w zrozumieniu jak dobrym pomysłem są metody, ponieważ można ich użyć jako swoisty kontener na różne funkcjonalności, którego rezultat można zmieniać poprzez wywoływanie metody z parametrami.
AddNumbers(2, 3);
Result: 5
AddNumbers(38, 4);
Result: 42
To jest bardzo podstawowy typ parametrów, ale porozmawiajmy więcej na temat wszelkiego rodzaju modyfikatorów i opcji, których możesz użyć, aby zmienić zachowanie parametrów.
Proszę zwróćcie uwagę, że w tym artykule, naprawdę uderzamy w sam punkt, wszelkiego rodzaju parametrów i tego jak mogą one Tobie pomóc, ale jeżeli dopiero zacząłeś z językiem c# i chcesz po prostu zobaczyć jakieś efekty, to co będzie za chwilę może być zbyt skomplikowane i techniczne na tą chwilę. Bez problemu możesz pominąć tą część poradnika i wrócić tu, gdy będziesz gotowy.
Parametry opcjonalne
Domyślnie, podczas wywoływania metody z jednym lub wieloma parametrami, jesteś zmuszony do podania wartości dla wszystkich tych parametrów. Jednakże, w niektórych sytuacjach, możesz potrzebować utworzyć jeden bądź wiele parametrów opcjonalnych. W niektórych językach programowania można by to zrobić po prostu oznaczając parametr jako opcjonalny, ale w C#, parametr staje się opcjonalny poprzez podanie dla niego wartości domyślnej w deklaracji metody. Jest to w zasadzie fajne rozwiązanie, ponieważ oszczędza ci pisania dodatkowego kodu, aby poradzić sobie z sytuacjami, w których parametr nie jest podawany podczas wywołania.
Oto przykład metody z parametrem opcjonalnym:
public int AddNumbers(int number1, int number2, int number3 = 0)
{
return number1 + number2 + number3;
}
Ostatni parametr (number 3) jest teraz opcjonalnym, ponieważ zadeklarowaliśmy mu wartość domyślną (0). Podczas wywoływania, możesz od teraz podawać dwie lub trzy wartości, jak poniżej:
public void Main(string[] args)
{
AddNumbers(38, 4);
AddNumbers(36, 4, 2);
}
Możesz tworzyć więcej niż jeden parametr opcjonalny - w zasadzie twoja metoda może składać się tylko z opcjonalnych parametrów, jeśli potrzebujesz. Zapamiętaj jednak, że parametry opcjonalne muszą być umieszczone jako ostatnie w deklaracji metody - nie jako pierwsze, ani pomiędzy parametrami nieopcjonalnymi.
The params modifier
As an alternative to a number of optional parameters, you can use the params modifier to allow an arbitrary number of parameters. It could look like this:
public void GreetPersons(params string[] names) { }
Calling it could then look like this:
GreetPersons("John", "Jane", "Tarzan");
Another advantage of using the params approach, is that you are allowed to pass zero parameters to the method as well. Methods with params can also take regular parameters, as long as the parameter with the params modifier is the last one. Besides that, only one parameter using the params keyword can be used per method. Here is complete example where we use the params modifier to print a variable number of names with our GreetPersons() method:
public void Main(string[] args)
{
GreetPersons("John", "Jane", "Tarzan");
}
public void GreetPersons(params string[] names)
{
foreach(string name in names)
Console.WriteLine("Hello " + name);
}
Parameter types: value or reference
C#, and other programming languages as well, differ between two parameter types: "by value" and "by reference". The default in C# is "by value", which basically means that when you pass a variable to a method call, you are actually sending a copy of the object, instead of a reference to it. This also means that you can make changes to the parameter from inside the method, without affecting the original object you passed as a parameter.
We can easily demonstrate this behavior with an example:
public void Main(string[] args)
{
int number = 20;
AddFive(number);
Console.WriteLine(number);
}
public void AddFive(int number)
{
number = number + 5;
}
We have a very basic method called AddFive() which will add 5 to the number we pass to it. So in our Main() method, we create a variable called number with the value of 20 and then call the AddFive() method. Now on the next line, when we write out the number variable, one might expect that value is now 25, but instead, it remains 20. Why? Because by default, the parameter was passed in as a copy of the original object (by value), so when our AddFive() method worked on the parameter, it was working on a copy and thereby never affecting the original variable.
There are currently two ways of changing this behavior, so that our AddFive() method is allowed to modify the original value: We can use the ref modifier or the in/out modifiers.
The ref modifier
The ref modifier is short for "reference" and it basically just changes the behavior of the parameter from the default behavior of "by value" to "by reference", meaning that we are now passing in a reference to the original variable instead of a copy of its value. Here's a modified example:
public void Main(string[] args)
{
int number = 20;
AddFive(ref number);
Console.WriteLine(number);
}
public void AddFive(ref int number)
{
number = number + 5;
}
You will notice that I have added the "ref" keyword in two places: In the method declaration and when passing in the parameter in the method call. With this change, we now get the behavior we originally might have expected - the result is now 25 instead of 20, because the ref modifier allows the method to work on the actual value passed in as a parameter instead of a copy.
The out modifier
Just like the ref modifier, the out modifier ensures that the parameter is passed by reference instead of by value, but there's a major difference: When using the ref modifier, you pass in an initialized value which you may choose to modify inside the method, or leave it as it is. On the other hand, when using the out modifier, you are forced to initialize the parameter inside the method. This also means that you can pass in uninitialized values when using the out modifier - the compiler will complain if you are trying to leave a method with an out parameter without assigning a value to it.
In C#, a method can only return one value, but if you use the out modifier, you are able to circumvent this by passing in several parameters with the out modifier - when the method has been called, all out parameters will have been assigned. Here's an example, where we pass in two numbers and then, using out modifiers, return both an addition and a subtraction using these numbers:
public void Main(string[] args)
{
int addedValue, subtractedValue;
DoMath(10, 5, out addedValue, out subtractedValue);
Console.WriteLine(addedValue);
Console.WriteLine(subtractedValue);
}
public void DoMath(int number1, int number2, out int addedValue, out int subtractedValue)
{
addedValue = number1 + number2;
subtractedValue = number1 - number2;
}
Output:
15
5
As you can see in the beginning of the example, I declare the two variables (addedValue and subtractedValue) before passing them as out parameters. This was a requirement in previous versions of the C# language, but from C# version 6, you can declare them directly in the method call, like this:
DoMath(10, 5, out int addedValue, out int subtractedValue);
Console.WriteLine(addedValue);
Console.WriteLine(subtractedValue);
The in modifier
Just like the out modifier, the in modifier ensures that the parameter is passed as a reference instead of a copy of the value, but unlike the out modifier, the in modifier will prevent you from making any changes to the variable inside the method.
Your first thought might be: If I can't change the value of the parameter, then I might as well just pass it in as a regular parameter, where changes won't affect the original variable. And you're right, the result would appear to be exactly the same, but there's still a very valid reason for using the in modifier: By passing it as a reference instead of a value, you are saving resources because the framework doesn't have to spend time creating a copy of the object when passing it to the method like a regular parameter.
In most cases, this won't make much of a difference, because most parameters are simple strings or integers, but in situations where you repeatedly call the same method many times in a loop or where you pass in parameters with large values, e.g. very large strings or structs, this may give you a nice performance boost.
Here's an example where we use the in modifier:
public void Main(string[] args)
{
string aVeryLargeString = "Lots of text...";
InMethod(aVeryLargeString);
}
public void InMethod(in string largeString)
{
Console.WriteLine(largeString);
}
In our method, InMethod(), the largeString parameter is now a read-only reference to the original variable (aVeryLargeString), so we're able to use it, but not modify it. If we try, the compiler will complain:
public void InMethod(in string largeString)
{
largeString = "We can't do this...";
}
Error: Cannot assign to variable 'in string' because it is a readonly variable
Named parameters
As you have seen in all of the examples above, each parameter has a unique name in the method declaration. This allows you to reference the parameter inside the method. However, when calling the method, you don't use these names - you just supply the values in the same order as they are declared. This is not a problem for simple methods which takes 2-3 parameters, but some methods are more complex and requires more parameters. In those situations, it can be quite hard to figure out what the various values in a method call refers to, like in this example:
PrintUserDetails(1, "John", 42, null);
It's not very clear what the various parameters means in this method call, but if you look at the method declaration, you can see it:
public void PrintUserDetails(int userId, string name, int age = -1, List<string> addressLines = null)
{
// Print details...
}
But it's annoying if you constantly have to look up the method declaration to understand what the parameters are doing, so for more complex methods, you can supply the parameter names directly in the method call. This also allows you to supply the parameter names in any order, instead of being forced to use the declaration order. Here's an example:
PrintUserDetails(name: "John Doe", userId: 1);
As an added bonus, this will allow you to supply a value for any of your optional parameters, without having to supply values for all previous optional parameters in the declaration. In other words, if you want to supply a value for the addressLines parameter in this example you would also have to provide a value for the age parameter, because it comes first in the method declaration. However, if you use named parameters, order no longer matters and you can just supply values for the required parameters as well as one or several of the optional parameters, e.g. like this:
PrintUserDetails(addressLines: new List<string>() { }, name: "Jane Doe", userId: 2);
Here's the complete example of using named parameters:
public void Main(string[] args)
{
PrintUserDetails(1, "John", 42, null);
PrintUserDetails(name: "John Doe", userId: 1);
PrintUserDetails(addressLines: new List<string>() { }, name: "Jane Doe", userId: 2);
}
public void PrintUserDetails(int userId, string name, int age = -1, List<string> addressLines = null)
{
// Print details...
Console.WriteLine(name + " is " + age + " years old...");
}
Summary
As you can see from this article, parameters come in many forms and types. Fortunately, you'll come along way with plain, old regular parameters, but once you start to dig deeper into the C# language, you will benefit from knowing all the types and modifiers as explained in this article.