TOC

This article is currently in the process of being translated into Italian (~23% done).

Classi:

Method parameters

Nel precedente articolo, abbiamo parlato dei metodi e abbiamo avuto una breve introduzione al concetto di parametri metodo / funzione. In questo articolo, approfondiremo questo argomento in tutte le sue varianti. Come accennato, i metodi possono funzionare senza parametri, ma di solito avranno uno o più parametri, che aiuteranno il metodo a svolgere il suo compito.

Abbiamo già visto uno scenario di utilizzo molto semplice per i parametri nell'articolo precedente: Il nostro metodo AddNumbers (), che accetta due numeri come parametri e restituisce la somma di questi due numeri:

public int AddNumbers(int number1, int number2)
{
	return number1 + number2;
}

Questo mostra molto sucos'è un metodo intelligente, perché con i metodi è possibile incapsulare la funzionalità nel metodo, ma essere in grado di influenzare il risultato quando si chiama questo metodo attraverso i parametri:

AddNumbers(2, 3);

Result: 5

AddNumbers(38, 4);

Result: 42

Questo è il tipo base di parametri, ma parliamo di più di tutti i vari modificatori e opzioni che è possibile utilizzare per modificare il comportamento dei parametri.

Nota che in questo articolo analizzeremo tutti i vari tipi di parametri e come possono aiutarti, ma se hai appena iniziato con C # e vuoi solo vedere alcuni risultati, i seguenti potrebbe essere un po 'troppo complessi e tecnici per ora. Sentiti libero di saltare il resto dell'articolo e tornare più tardi quando sei pronto.

Parametri opzionali

Per impostazione predefinita, quando si chiama un metodo con uno o più parametri, si è obbligati a fornire valori per tutti questi parametri. Tuttavia, in alcune situazioni, potrebbe essere necessario rendere uno o più parametri facoltativi. In alcuni linguaggi di programmazione, è possibile farlo semplicemente contrassegnando il parametro come facoltativo, ma in C #, un parametro viene reso facoltativo fornendo un valore predefinito nella dichiarazione del metodo. Questa è in realtà una buona soluzione, perché ti salva dalla scrittura di codice extra per affrontare situazioni in cui il parametro non viene fornito dal chiamante.

Qui un esempio di un metodo con un parametro opzionale:

public int AddNumbers(int number1, int number2, int number3 = 0)
{
	return number1 + number2 + number3;
}

L'ultimo parametro (numero3) è ora facoltativo, perché è stato fornito un valore predefinito (0). Quando lo chiami, ora puoi fornire due o tre valori, in questo modo:

public void Main(string[] args)
{
	AddNumbers(38, 4);
	AddNumbers(36, 4, 2);
}

Puoi rendere opzionale più di un parametro: in effetti, il tuo metodo può consistere in parametri opzionali solo se necessario. Ricorda solo che i parametri opzionali devono arrivare per ultimi nella dichiarazione del metodo - non prima o tra parametri non opzionali.

Modificatore di parametri

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.