Didactics
Оценка 5

Didactics

Оценка 5
docx
02.05.2020
Didactics
didactics.docx

 

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. 

 

 

1.      https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/


 

Скачано с www.znanio.ru

Ref Vs Out Ref

Ref Vs Out Ref

Text = "Next-" + id.ToString(); return returnText; }

Text = "Next-" + id.ToString(); return returnText; }
Материалы на данной страницы взяты из открытых истончиков либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.
02.05.2020