didactics (2)

  • docx
  • 02.05.2020
Публикация на сайте для учителей

Публикация педагогических разработок

Бесплатное участие. Свидетельство автора сразу.
Мгновенные 10 документов в портфолио.

Иконка файла материала didactics (2).docx

Function parameters

In the previous chapter, we had a look at functions. We briefly discussed parameters, but only briefly. While parameters are very simple and straight forward to use, there are tricks which can make them a lot more powerful.

The first thing that we will take a look at, is the out and ref modifiers. C#, and other languages as well, differ between two parameters: "by value" and "by reference". The default in C# is "by value", which basically means that when you pass on a variable to a function 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 function, without affecting the original object you passed as a parameter.

With the ref and the out keyword, we can change this behavior, so we pass along a reference to the object instead of its value.

The ref modifier

Consider the following example:

static void Main(string[] args)
{
    int number = 20;
    AddFive(number);
    Console.WriteLine(number);
    Console.ReadKey();
}

static void AddFive(int number)
{
    number = number + 5;
}

We create an integer, assign the number 20 to it, and then we use the AddFive() method, which should add 5 to the number. But does it? No. The value we assign to number inside the function, is never carried out of the function, because we have passed a copy of the number value instead of a reference to it. This is simply how C# works, and in a lot of cases, it's the preferred result. However, in this case, we actually wish to modify the number inside our function. Enter the ref keyword:

static void Main(string[] args)
{
    int number = 20;
    AddFive(ref number);
    Console.WriteLine(number);
    Console.ReadKey();
}

static void AddFive(ref int number)
{
    number = number + 5;
}

As you can see, all we've done is added the ref keyword to the function declaration as well as to the call function. If you run the program now, you will see that the value of number has now changed, once we return from the function call.

The out modifier

The out modifier works pretty much like the ref modifier. They both ensure that the parameter is passed by reference instead of by value, but they do come with two important differences: A value passed to a ref modifier has to be initialized before calling the method - this is not true for the out modifier, where you can use un-initialized values. On the other hand, you can't leave a function call with an out parameter, without assigning a value to it. Since you can pass in un-initialized values as an out parameter, you are not able to actually use an out parameter inside a function - you can only assign a new value to it.

Whether to use out or ref really depends on the situation, as you will realize once you start using them. Both are typically used to work around the issue of only being able to return one value from a function, with C#.

Using the out modifier is just like using the ref modifier, as shown above. Simply change the ref keyword to the out keyword. In the example above, also remember to remove the value assigned to number in the method and declare it in the call function instead.

The params modifier

So far, all of our functions have accepted a fixed amount of parameters. However, in some cases, you might need a function which takes an arbitrary number of parameters. This could of course be done by accepting an array or a list as a parameter, like this:

static void GreetPersons(string[] names) { }

However, calling it would be a bit clumsy. In the shortest form, it would look like this:

GreetPersons(new string[] { "John", "Jane", "Tarzan" });

It is acceptable, but it can be done even smarter, with the params keyword:

static void GreetPersons(params string[] names) { }

Calling it would 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 it as well. Functions with params can even take other parameters as well, as long as the parameter with the params keyword are the last one. Besides that, only one parameter using the params keyword can be used per function. Here is a last and more complete example:

static void Main(string[] args)
{
    GreetPersons(0);
    GreetPersons(25, "John", "Jane", "Tarzan");
    Console.ReadKey();
}

static void GreetPersons(int someUnusedParameter, params string[] names)
{
    foreach(string name in names)
    Console.WriteLine("Hello, " + name);
}

 

 

Ref Vs Out Keywords in C#

Introduction

 

The keywords ref and out are used to pass arguments within a method or function. Both indicate that an argument / parameter is passed by reference. By default parameters are passed to a method by value. By using these keywords (ref and out) we can pass a parameter by reference.

 

Ref Keyword

 

The ref keyword passes arguments by reference. It means any changes made to this argument in the method will be reflected in that variable when control returns to the calling method.

 

Example code

  1. public static string GetNextName(ref int id)  
  2. {  
  3.     string returnText = "Next-" + id.ToString();  
  4.     id += 1;  
  5.     return returnText;  
  6. }  
  7. static void Main(string[] args)  
  8. {  
  9.     int i = 1;  
  10.     Console.WriteLine("Previous value of integer i:" + i.ToString());  
  11.     string test = GetNextName(ref i);  
  12.     Console.WriteLine("Current value of integer i:" + i.ToString());  
  13. }   

Output

 

Ref in C# 

 

Out Keyword

 

The out keyword passes arguments by reference. This is very similar to the ref keyword.

 

Example Code

  1. public static string GetNextNameByOut(out int id)  
  2. {  
  3.     id = 1;  
  4.     string returnText = "Next-" + id.ToString();  
  5.     return returnText;   
  6. }  
  7. static void Main(string[] args)  
  8. {  
  9.     int i = 0;  
  10.     Console.WriteLine("Previous value of integer i:" + i.ToString());  
  11.     string test = GetNextNameByOut(out i);  
  12.     Console.WriteLine("Current value of integer i:" + i.ToString());  
  13. }  

Output

 

Out in C# 

 

Ref Vs Out

 

Ref

Out

The parameter or argument must be initialized first before it is passed to ref.

It is not compulsory to initialize a parameter or argument before it is passed to an out.

It is not required to assign or initialize the value of a parameter (which is passed by ref) before returning to the calling method.

A called method is required to assign or initialize a value of a parameter (which is passed to an out) before returning to the calling method.

Passing a parameter value by Ref is useful when the called method is also needed to modify the pass parameter.

Declaring a parameter to an out method is useful when multiple values need to be returned from a function or method.

It is not compulsory to initialize a parameter value before using it in a calling method.

A parameter value must be initialized within the calling method before its use.

When we use REF, data can be passed bi-directionally.

When we use OUT data is passed only in a unidirectional way (from the called method to the caller method).

Both ref and out are treated differently at run time and they are treated the same at compile time.

Properties are not variables, therefore it cannot be passed as an out or ref parameter.

 

Ref / Out keyword and method Overloading

 

Both ref and out are treated differently at run time and they are treated the same at compile time, so methods cannot be overloaded if one method takes an argument as ref and the other takes an argument as an out.

 

Example code

  1. public static string GetNextName(ref int id)  
  2. {  
  3.     string returnText = "Next-" + id.ToString();  
  4.     id += 1;  
  5.     return returnText;  
  6. }  
  7. public static string GetNextName(out int id)  
  8. {  
  9.     id = 1;  
  10.     string returnText = "Next-" + id.ToString();  
  11.     return returnText;  
  12. }  

Output when the code is compiled:

 

method Overloading 

 

However, method overloading can be possible when one method takes a ref or out argument and the other takes the same argument without ref or out.

 

Example Code

  1. public static string GetNextName(int id)  
  2. {  
  3.     string returnText = "Next-" + id.ToString();  
  4.     id += 1;  
  5.     return returnText;  
  6. }  
  7. public static string GetNextName(ref int id)  
  8. {  
  9.     string returnText = "Next-" + id.ToString();  
  10.     id += 1;  
  11.     return returnText;  
  12. }  

Summary

 

The out and ref keywords are useful when we want to return a value in the same variables as are passed as an argument.