Ref, out, And params parameters in c #

Source: Internet
Author: User

Ref, out, And params parameters in c #
Comparison Between out parameters and c ++ references

The out parameter can be used to pass the method return value, which is a bit similar to the reference in c ++, but there are some differences:
-The out parameter must be written when a method is called.
-You must allocate space before calling the method.
-You do not need to assign a value before calling a method.
-The out parameter must be assigned a value within the method;

Implement a tryparse function by yourself.
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace out parameter {class Program {public struct Mid {public int [] _ num; public string _ name;} static void Main (string [] args) {// write a method to calculate the maximum, minimum, sum, and average values of an array. Int [] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // put the four values to be returned in an array and return them; but the problem is that the array is a group of data of the same type, so it can be placed in an array. // If it is of different types, it can only be returned from the parameter. at this time, the structure can be encapsulated and returned. However, this structure is not necessarily meaningful and requires subsequent operations. // Mid mid = GetMaxMinSumAvg (numbers); // Console. writeLine ("Name: {0}", mid. _ name); int max, min, sum; double avg; GetTest (numbers, out max, out min, out sum, out avg); Console. writeLine ("maximum value: {0}, minimum value: {1}, total value: {2}, average value: {3}", max, min, sum, avg); Console. readKey ();}////// Calculate the maximum, minimum, average, sum, and average values of an integer array ;//////The array to be calculated ///Maximum value returned in excess ///Minimum value returned in excess ///For the total number of returned results ///Public static void GetTest (int [] nums, out int max, out int min, out int sum, out double avg) {// The out parameter must be assigned inside the method; max = nums [0]; min = nums [0]; sum = nums [0]; for (int I = 1; I <nums. length; I ++) {if (nums [I]> max) {max = nums [I];} if (nums [I]
  
   
/// Implement an int-type TryParse () function by yourself ;//////
   ///
   ///
   Public static bool MyTryParse (string str, out int num) {try {num = int. parse (str);} catch (OverflowException) {num = 0; return false;} return true ;}}}
  
Ref Parameter

Ref is equivalent to a reference in c ++. It is more like a reference than out;

Note:
-Assign a value before calling a method;
-Make sure to write ref during the call;
-Make sure to construct a space
Next, we will write an operation to exchange the content of two variables:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; // ref is equivalent to a reference in c ++; it is a little more like out, and out focuses on returning 20 years; namespace ref parameter {class Program {static void Main (string [] args) {int n1 = 10; int n2 = 20; // pay attention to two points; Be sure to assign a value before calling the method; be sure to write ref; Be sure to construct the space Exchange (ref n1, ref n2); Console. writeLine ("after switching, the first variable is {0}, and the second parameter is {1}", n1, n2); Console. readKey ();}////// Exchange two variables //////First variable ///The second variable public static void Exchange (ref int n1, ref int n2) {n1 = n1-n2; n2 = n1 + n2; n1 = n2-n1 ;}}}
Params Parameters

Params is a variable-type parameter, but it is different from the variable-length parameter in c ++:
-The elements in the real parameter list that are consistent with the variable parameter array type are treated as array elements.
-The params variable parameter must be the last element in the parameter list.
-Uniqueness: A function can have only one params variable parameter.
-There are two transmission methods for calling: array and direct data transmission;

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Params parameter {class Program {static void Main (string [] args) {int [] a = {1, 2, 3}; Test ("Haha", );} public static void Test (string name, params int [] par) the {}}/ ** param variable parameter can be used to calculate the maximum value of an array of any length without the ** uniqueness * Final * Transfer Method ;**/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.