General understanding C #(14. Parameter modifiers)

Source: Internet
Author: User

14. Parameter Modifier

(1) ref parameter Modifier

C # (compared with Java) allows you to pass parameters by reference. The most obvious example to describe this is the general exchange method. Unlike C ++, the ref directive must be added when calling a statement: [Note: Do not misunderstand this sentence. Of course, there is no ref keyword in C ++]

Public class Test

{

Public static void Main ()

{

Int a = 1;

Int B = 2;

Swap (ref a, ref B );

}

Public static void swap (ref int a, ref int B)

{

Int temp =;

A = B;

B = temp;

}

}

(2) out parameter Modifier

The out keyword is a natural supplement to the ref parameter modifier. The Ref modifier requires that the parameter must be assigned a value before the method is passed in. The out modifier explicitly assigns a value to the parameter when the method is returned ,.

(3) params parameter Modifier

The params modifier can be added to the final parameter of the method. The method will accept any number of parameters of the specified type, only one params parameter is allowed ]. For example:

Public class Test

{

Public static void Main ()

{

Console. WriteLine (add (1, 2, 3, 4). ToString ());

}

Public static int add (params int [] array)

{

Int sum = 0;

Foreach (int I in array)

Sum + = I;

Return sum;

}

}

[Note] one of the most surprising things in learning Java is that Java cannot pass parameters by reference. Although you will rarely want this function in the near future, and you do not need it when writing code. When I read the C # specification for the first time, I often thought, "Why did they add this function and I can write code without it ". After reflection, I realized that this is not actually a question about whether some functions are useful. It is also a question about how you can achieve it without it.

When considering how C ++ works, Java is a good thing, which simplifies how parameters are transmitted. In C ++, there is no method in Method, parameters and methods called "functions" or "member functions" are called by passing values, references, and pointers &], makes the code unnecessary. C # explicitly transmits references, whether in method declaration or call. This statement should be understood as follows: Due to the syntax problem of C ++, sometimes you do not know whether you are using an object or an object reference, this section provides examples and achieves the same goal as Java, but the C # method is more expressive. Obviously, this is the main idea of C #-it does not enclose programmers in a circle, so that they must turn around a big bend to do something. Do you still remember Java? In the Java guide, we recommend that you solve the problem of transferring references. You should pass an array of one element to save your value, or create another class to save this value.

Note:

# Include "stdafx. h"

Class ParentCls

{

Public:

Virtual void f () {printf ("ParentCls ");}

};

Class ChildCls: public ParentCls

{

Public:

Virtual void f () {printf ("ChildCls ");}

};

Void Test1 (ParentCls pc) {pc. f ();}

Void Test2 (ParentCls & pc) {pc. f ();}

Int main (int argc, char * argv [])

{

ChildCls cc;

Test1 (cc); // output ParentCls

Test2 (cc); // output ChildCls

File: // onlyCheck whether the call site does not know whether you are using the reference or object, but the running results are quite different!

Return 0;

}

]

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.