Four types of parameters for methods in C #

Source: Internet
Author: User

There are four types of parameters for methods in C #:1. Value parameter type (without any modifiers, is the default type)2. Reference-type parameters (declared with ref modifier)3. Output type parameters (declared with out modifier)4. Array-type parameters (declared with the params modifier)

===================================================

1. value passing: A value type is the default parameter type for a method, in the form of a copy of the value. That is, if you are using a value type, you can change the value in the method, but the changed value is not preserved when control is passed back to the calling procedure. Examples of using value types are as follows: (The following swap () failed to implement the function of the interchange, since control is passed back to the caller without preserving the changed values)using System;class Test{ static void Swap (int x, int y) { int temp = x; x = y; y = temp; } static void Main () { int i = 1, j = 2; Swap (i, j); Console.WriteLine ("I = {0}, J = {1}", I, j); }}/** Output: I=1, j=2* Failed to implement swap () program functions*/

2. Reference passing (ref type): The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
    • To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword.
    • The arguments passed to the ref parameter must be initialized first. This differs from out in that the out parameter does not need to be explicitly initialized before it is passed.
    • If a method takes a ref or out parameter, and the other method does not take these two types of arguments, it can be overloaded.
The relevant examples are as follows:  using System;class Test{ static void Swap (ref int x, ref int y) { int temp = x; x = y; y = temp; } static void Main () { int i = 1, j = 2; Swap (ref I, ref j); Console.WriteLine ("I = {0}, J = {1}", I, j); }}/** The reference type implements the swap () plan function:* Output is:* i = 2, J =1*/

3. Output type (out type): The out keyword causes parameters to be passed by reference. This is similar to the REF keyword. differs from ref:
    • Ref requires that a variable be initialized before it is passed, and the variable passed by the out parameter does not need to be initialized before being passed.
    • Although a variable passed as an out parameter does not need to be initialized before it is passed, it needs to be initialized in the calling method to assign a value before the method returns.
Examples are as follows:using System;class Test{ static void Swap (out int x, out int y) { //The initialization of I and J is performed here x = 1; y = 2; int temp = x; x = y; y = temp; } static void Main () { //The initialization of I and J may not be performed here int I, J; Swap (out I, out j); Console.WriteLine ("I = {0}, J = {1}", I, j); }}/** The output type also implements the swap () plan function:* Output is:* i = 2, J =1*/

4. array type parameter type (params type): The params keyword can specify a method parameter that takes a parameter at a variable number of arguments. Other words. Use the params to automatically convert your incoming values to a new array by rule.
    • No additional arguments are allowed after the params keyword in the method declaration, and only one params keyword is allowed in the method declaration.
Examples are as follows:  using System;class App{Public static void Useparams (params object[] list) {For (int i = 0; i < list. Length; i++) { Console.WriteLine (List[i]); } } static void Main () { //The general practice is to construct an array of objects and then use this array as a parameter of the method object[] arr = new Object[3] {A, ' a ', "keywords"}; useparams (arr); //And with the params modifier parameter, we can use a set of objects directly as parameters //Of course this set of parameters needs to conform to the calling method of the parameter requirements useparams ("A", "keywords", ...); ...); }}Reference: Http://msdn.microsoft.com/zh-cn/library/8f1hz171.aspx

Four types of parameters for methods in C #

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.