Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the functions and differences of the C # keywords in, out and ref

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the functions and differences of the C # keywords in, out and ref". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the functions and differences of the C # keywords in, out and ref"?

Brief introduction:

In: the process does not overwrite the contents of the In. The default way of passing is to send values to the inside of the function.

Out and out: the value passed in will not be read by the procedure. When Out is passed in, the value of the parameter will be cleared, but the process can be written. Only get out, not get in.

Ref: you can pass the value of the parameter to the function, and the process can read and write. In and out.

1. In

The In keyword causes parameters to be passed by value. That is, to send a value to the inside of the function.

For example:

Using System;class gump {public double square (double x) {xroomxroomx; return x;}} class TestApp {public static void Main () {gump doit=new gump (); double axi3; double bicio; Console.WriteLine (\ "Before square- > a = {0}, b = {1}\", aMagne b); / / axiajin3 cognac 0; b=doit.square (a) Console.WriteLine (\ "After square- > a = {0}, b = {1}\", aheline b); / / axi3reparing 9;}} II, ref

The ref keyword causes parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes to the parameters in the method are reflected in that variable. To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword.

For example:

Using System;class gump {public double square (double x) {xroomxroomx; return x;}} class TestApp {public static void Main () {gump doit=new gump (); double axi3; double bill0; Console.WriteLine (\ "Before square- > a = {0}, b = {1}\", aMagne b); / / axiajin3 b=doit.square (ref a) Console.WriteLine (\ "After square- > a = {0}, b = {1}\", aheline b); / / aqyn9;}}

Parameters passed to the ref parameter must be initialized first. This is different from out, whose parameters do not need to be explicitly initialized before they are passed.

III. Out

The out keyword causes parameters to be passed by reference. This is similar to the ref keyword, except that ref requires variables to be initialized before they are passed. To use the out parameter, both the method definition and the calling method must explicitly use the out keyword.

Using System;class gump {public void math_routines (double x out double half,out double squared,out double cubed) / / can be: public void math_routines (ref double x out double half,out double squared,out double cubed) / / but not like this: public void math_routines (out double x out double half,out double squared,//out double cubed). In this case, because the output value is assigned by x, x can no longer be the output value {half=x/2 Squared=x*x; cubed=x*x*x;}} class TestApp {public static void Main () {gump doit=new gump (); double x1 / 600; double half1=0; double squared1=0; double cubed1=0; [Page] / * double x1 / 600; double half1; double squared1; double cubed1 * / Console.WriteLine (\ "Before method- > x1 = {0}\", x1); Console.WriteLine (\ "half1= {0}\", half1); Console.WriteLine (\ "squared1= {0}\", squared1); Console.WriteLine (\ "cubed1= {0}\", cubed1); doit.math_rountines (x1 out half1,out squared1,out cubed1) / / the parameter values modified by Out have changed at this time. Console.WriteLine (\ "After method- > x1 = {0}\", x1); Console.WriteLine (\ "half1= {0}\", half1); Console.WriteLine (\ "squared1= {0}\", squared1); Console.WriteLine (\ "cubed1= {0}\", cubed1);}}

Although variables passed as out parameters do not have to be initialized before they are passed, you need to call the method to assign a value before the method returns.

Class OutExample {static void Method (out int i) {I = 44;} static void Main () {int value; Method (out value); / / value is now 44}} IV. Difference:

The difference between 1.ref and out is in C #, where parameters can be passed either by value or by reference. Passing parameters by reference allows function members to change the value of the parameter and maintain the change. To pass parameters by reference, use the ref or out keyword. Both the ref and out keywords provide a similar effect, much like pointer variables in C.

two。 When using reftype parameters, the parameters passed in must be initialized first. For out, it must be initialized in the method.

3. When using ref and out, add the Ref or Out keyword to both the parameters of the method and the execution of the method. To meet the match.

4.out is suitable for use where multiple return values of retrun are required, while ref is used when the method to be called modifies the caller's reference.

5. The out method parameter keyword on the method parameter causes the method reference to pass to the same variable of the method. When control is passed back to the calling method, any changes made to the parameters in the method are reflected in that variable.

Thank you for your reading, the above is the content of "what is the role and difference of the C # keywords in, out, ref". After the study of this article, I believe you have a more profound understanding of the role and difference of the C # keywords in, out and ref, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report