C# Parameter: passing parameter by value, by reference, and default parameter


C# Parameter: In C#, no function can be independent of a class (unlike global functions in  C++). Static functions can nevertheless play the role of global functions : when calling, all you have to do is prefix the name of this function with the name of its class, without having to create object.

If we limit ourselves to passing parameters by value, functions in C# are written like functions of C/C++. But there are differences when it comes to passing parameter by reference. As in C/C ++, a function can:

  • admit or not parameters (empty parentheses indicate the absence of c# parameters, as is the case in C++);
  • return or not a value (void indicating that the function does not return any value);
  • have its own variables, these being destroyed when exiting the function. Unlike C/C ++:
  • no prototype or file to include in C#;
  • a C# function accepts an array as an c# parameter much more easily than in C/C++;
  • a C# function can return an array, which is particularly interesting.

C# accepts parameters by default but in a very different form from C ++. C# version 4 (Visual Studio 2010) introduced default parameters as well as parameters named.

parameters of a function in C#:

By default, the c# parameters of a function are passed:

  • by value or by reference in the case of “value” type parameters (int, float, string, etc., but also structures);
  • by value or by reference in the case of array or objects but the effect is very different.

We will write for example (the functions must be here – and here only – qualified as static):

C# parameter: Pass by value:


C# parameter

C/C++ programmers therefore find themselves in a country of knowledge. why the methods f and g had to be called static (in fact so that they could be called from Main which is itself a static function because no object should be created to call Main).

The function f received as parameter the value 3 but f does not know anything about the origin of this value. The i parameter of f (which is a variable of f) took on the value 3 when entering the function (it there was a copy of the value 3 in i of f). Any modification of i of f is limited to i of f (as we will see soon, this is not the case with the  c# parameter pass by reference) and i of f disappears when the execution reaches the end brace of f.

As in C/C++, void indicates that the function returns nothing. A function can also return a and a single value (to simulate the return of several values, you must pass parameters by reference or out parameters, which is explained later.

An array of three elements is created in f (at the moment the program execution causes the entry in f). The reference t is certainly local to f, and will therefore be destroyed when leaving f, but the array itself (i.e. its three cells) is allocated on the heap. It can only be destroyed when no more reference “points” on this table. By returning t, f returns this reference which is copied in your Main. ta de Main now refers to an array of three integers whose cells are initialized at 11, 22 and 33.

Several functions can have the same name. They must then be different in number or the type of the c# parameters, the type of the return value playing no role in the differentiation.

C# parameter: Pass by reference:

The passing parameter by address, well known to C/C++ programmers, is replaced by the passing parameter by reference, which is the same with the code generated: internally, a reference is implemented as a pointer but since you cannot not use the reference with all the freedom and laxity of a C/C++ pointer, this presents much less danger.

However, it would be wrong to say that the passing of parameter by address does not exist in C#: it is necessary to this use pointers which, in C#, are subject to significant restrictions compared to what is possible in C/C++. If a variable (never a value) is passed by reference (the variable a of Main in the example following), the called function can modify this variable of the calling function.

C# requires to mark, much more clearly than in C ++, the passing of parameter by reference, y included in the function call (reserved word ref in the declaration of the function but also in the function call):

Pass parameter by reference:


C# parameter

The c# parameter to function f, which is a variable local to f, is called aa. Because of ref int aa, we knows that aa designates a reference to a variable of type int. In other words, aa will know the location in memory of an integer (and, from there, the value of that integer). How much and when will he know the location of this integer? When calling the function f.

With f (ref a) in Main, we call the function f and we pass as parameter to f the location of a among the variables of Main. aa (of f) therefore refers, for this call, to a of Main. Any modification in f of aa therefore modifies a of Main.

The passage of character strings (type string) behaves as we have just explained in the previous example: ref must qualify the c# parameter (both during the call and in the function) so that the function can modify the string passed as an parameter. The string type, particularly used, is a “reference” type (like the classes) but is processed by the compiler as a “value” type to facilitate the work of the programmer.

Passing an array as a parameter:

Arrays can be passed by value or by reference in c#. Arrays are objects, an array is always designated by its reference (ti of Main in the example below) and is allocated by new (even if this new does not appear when an array is created and initialized in the process, like in the example below). This is when the actual data area of ​​the table (the five integers in the example below) is allocated. The reference then “points” to this area.

When an array is passed by value, the value of the reference is passed as a parameter. Since this value indicates where the data in the table is located, the function can modify these data. But the function is then quite incapable of modifying the size of the array (or canceling it, by placing the value null in the reference) because this requires recreating an array and therefore changing the reference. In the case of a passage of an array by value, the function ignores where this reference is found in the calling function. The function is therefore quite incapable of making such a change in table.

For example, to add 1 to all the elements of the array ti of Main, from the function f, it just write (we change the content of the cells of the array):

Passing an array as a parameter by value:


C# parameter

Although this has nothing to do with passing parameters, a for loop had to be used in f to be array to modify each cell of the array. In writing :

a can only be used for reading (this is a limitation of foreach). We could therefore display a (which successively takes the value of each cell of the table) but we would not change the different table cells by modifying a.

ta of f becomes an array of two integers (array initialized with the values ​​101 and 102) but this modification remains limited to f. It is not reflected in ti of Main (which therefore remains an array of five integers).

If we add ref in the function and the function call, the reference to the array is passed by reference. The function f is then able to change this reference in Main and therefore to change completely the array (for example its size):

Passing an array as an parameter (by reference):


C# parameter

Passing out parameters in c#:

The passing of the out parameter indicates that the variable (never a value) passed as an parameter (always by reference) is initialized by the called function.

Passing out parameters:


C# parameter

It was not mandatory to initialize a before calling f since the passing of a as an parameter out indicates that the function f will not be able to use the contents of a when entering the function. It is f which by initializing its parameter in fact initializes a of Main.

When entering f, aa refers to a of Main. Any modification of aa in f modifies a of Hand. In the case of passing an parameter by ref reference, the fact of not having initialized a of Main would have represented a syntax error: a should have been initialized since the called function would have could, from the start, use the initial value of the parameter (which would have been a random value in our case).

The passing of the out parameter is used in particular in the case where a function is called to initialize an array or a collection:

When entering f, t in Main is still an empty array (null value in reference t). Any change of at in f changes t of Main. After calling the function f, t of Main denotes a array of three integers, the three cells being initialized respectively to 10, 20 and 30. out is necessary here because t is not yet instantiated when entering f. out would not have been necessary neither in f nor in the call of f if t had been instantiated (in any way) in Hand. When entering the function f, at becomes t of Main.

The function f could have instantiated and returned the array:

The reference ta certainly belongs to the function f and will be destroyed when leaving f. you are instantiated in f and the three cells are created in the heap. your “point” on the beginning of this array of three cells. f returns this reference (i.e. the location in memory of the array) and this value of return is copied to the reference that is Main. The reference ta (and the reference alone, not the zone pointed) is then automatically destroyed (since we quit the function). The pointed area (which contains the three cells of the array) is not because the program contains a reference (t) on this zone. t now “points” to an array of three cells allocated on the heap. t has been declared in Main and instantiated in f.

Passing objects into parameters in c#:

let’s see how to pass them as parameters, even if it happens as for tables. An object is designated by its reference and this contains the memory location of the subject (for example the name and age of a person. When passing by value, the function receives the value of the reference. The function therefore knows where to find the object’s data and can therefore modify it. These changes are reflected in the object of the calling function.

Passing an object as an parameter by value:


C# parameter

In the following example, we are passing the subject by reference. The function can then modify much more the object passed as an parameter, including freeing it. Here, the function frees the object created in Main!

Passing an object as a parameter by reference:


C# parameter

C# Parameters variable in number and type:

At first glance, the default parameters don’t exist in C#, but version 4 fixed this. However, two techniques give this possibility, even if a version older than C# 4 (but it’s still easier in C# 4).

The first is very simple and consists of explicitly writing the different shapes. In the example below, the function f has three forms:

  • the first with its two parameters;
  • the second with a single parameter and the value 2 by default;
  • the third without parameter but the values ​​1 and 2 by default.

The second technique is more sophisticated and goes well beyond the default C ++ parameters. Passing an parameter with the reserved word params allows you to pass variable parameters in number but also in type. Consider the following example:

Passing variable number parameters in c#:

The function f contains a fixed parameter (Text, of type string) but we could very well have written a function f without or with several fixed parameters (always at the head of the parameters). In the case of our function f, a first parameter of type string must always be present in a call from f. The c# parameter params (only one, always at the end) can be omitted (example 3), can be designated a variable number of parameters (examples 1 and 2) or even an array (example 4). The c# parameter params must always be denoted by a one-dimensional array. In function f, para.Length gives the number of variable parameters. The c# parameters can also be variable in number and type because all types are derived of the Object class:

Passing variable number and type parameters:


C# parameter

As so often, there is a way to write things in a much more obscure and complicated way. So, the previous foreach could have been written (a switch, even a very long one, is considered to be a single instruction):

Named  / or default parameters in c#:

C# version 4 (Visual Studio 2010) introduced default parameters in C# as well as parameters named. Consider the following function:

This means that 10 is the default for the first parameter and 5 is the default for the second parameter. The call to f can be done by:

  • f (13, n); // No use is made of the default parameters here. num takes the value 13 and den the contents of n.
  • f (13); //The den parameter, absent here, automatically takes the value 5.
  • f (); // Use of both default values.

Default parameters, if present, must appear after parameters with no value by default. Default values ​​must be constants. Writing f (, 7) is not allowed (you cannot not omit c# parameters other than the last but the named parameter technique allows to solve this problem).

Since C# version 4, it is also possible to call a function with named parameters (the order of the c# parameters during the call then no longer matters). Consider the following calls:

  • f (den: 7, num: 21); //The num parameter takes the value 21 and the den parameter takes the value 7.
  • f (den: 17); //The num parameter defaults to (here 10) and the den parameter to 17.


 the syntax (with 🙂 for specifying the value of an c# parameter. The = symbol has been banned to avoid any confusion with the assignment (assignment of a value with, in the process, passing an parameter). AT right of:, we can obviously find the name of a variable or an arithmetic expression. In the case of an parameter passing by reference, we write:

and the call to function f is performed by:

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: