I have a understanding of keywords REF and OUT

xiaoxiao2021-03-06  14

Recently I just started to contact C # this language, I feel very interesting. Yesterday I saw the keyword ref and out, I didn't figure out how the two concepts were going on. Today, I finally understand them.

basic introduction

In Microsoft's MSDN, these two keywords explain this:

Ref: The ref method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method. . to use a ref parameter, the argument must explicitly be passed to the method as a ref argument The value of a ref argument will be passed to the ref parameter An argument passed to a ref parameter must first be initialized.Out:.. The out method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method will be reflected in that variable when control passes back to the calling method. Declaring an out method is useful when you want a method to return multiple values. A method that uses an out parameter can still return a value. A method can have more than one out parameter. to use an out parameter, the argument must e xplicitly be passed to the method as an out argument. The value of an out argument will not be passed to the out parameter. A variable passed as an out argument need not be initialized. However, the out parameter must be assigned a value before the Method Returns.

Type introduction

In almost all OOP languages, there are two types of values.

Value type

Reference type

Take C # Take: its value type is Sbyte, Byte, CHAR, SHORT, USHORT, INT, UINT, LONG, and ULONG, FLOAT and DOUBLE, of course Decimal and Bool. The reference type is String and Object.

I want to say

What I want to say is --Ref and Out The reason why I can't confuse me is that I didn't seriously analyze the different movements of different types.

For value types.

The effect of using REF and OUT is almost the same as the pointer variables in almost C. It allows you to operate directly, not to operate the original number of COPY. Lift a small example: use system;

A summary description of Namespace consoleApplication4 {///

/// class1. /// class class1 {/// /// The primary entry point of the application. /// [stathread] static void main (string [] args) {int a = 5; INT B;

Squareref (Ref A); Squareout (OUT B);

Console.writeline ("The a in the main is:" a); console.writeline ("The b in the main is:" b);}

Static void Squareref (Ref INT X) {x = x * x; console.writeline ("The x in the squareref is:" x);}

Static void Squareout (out int y) {y = 10; y = y * y; console.writeline ("the y in the squareout is:" y);}}}}}}}}}}}

The result of the display is - 25 100 25 100.

In this case, the same role as the pointer variable in C is reached.

For reference types.

It is more difficult to understand the reference type.

First, I will understand this layer - just when a method receives a variable of a reference type, it will get a copy of this reference (Reference). Since the REF keyword can be used to pass reference to the method. So, if this function is misused - such as: When a reference object such as an array type is passed with a keyword REF, the method that is called actually has controlled the passage of the reference itself. This will enable the modified method to replace the caller's original reference with different objects or even null!

Figure. The memory address is 2000 variables in Arraya, in fact, the memory start address 10000 of the array {1, 2, 3, 4, ...} is stored. If a method fun () uses Fun (Arraya []), it will get a smooth data 10000, but this 10000 will be placed in a copy without placing the 2000 position of memory. At this time, if we use fun (Ref Arraya []), the value we get is 2000 (that is, the called method can modify the reference in Arraya, making it no longer pointing 10000, can even use null Instead of 10,000, this, the data in that 10000 address may be cleaned up by the garbage collection mechanism.)

There is an example:

Using system.drawing; using system.componentmodel; using system.windows.form; using system.data;

A summary description of Namespace Refout {///

///form1. /// public class form1: system.windows.Forms.form {private system.windows.Forms.Button button1; private system.windows.forms.Label label1; /// /// must be Designer variable. /// private system.componentmodel.container Components = NULL; public form1 () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method. /// private (this.button1 = new system.windows.Forms.Button (); this.label1 = new system.windows.forms.Label (); this.suspendlayout (); / / // button1 // this.button1.dock = system.windows.forms.dockstyle.top; this.button1.location = new system.drawing.point (0, 0); this.button1.name = "button1"; This.Button1.Size = new system.drawing.size (480, 32); this.button1.tabindex = 0; this.button1.text = "Show output"; this.button1.click = new system.eventhandler (this .Button1_click; // // label1 // this.label1.location = new system.drawing.point (8, 48); this.label1.name = "label1"; this.label1.size = new system.drawing. Size (456, 336); this.label1.tabindex = 1; this.label1.text = "label1"; // // Form1 // this.doscaleBaseSize = new system.drawing.size (6, 14); this. Clientsize = new system.drawing.size (480, 405); this.controls.add (this.label1); this.Controls.add (this.button1); this.maximizebox = false This.minimizebox = false; this.name = "form1"; this.text = "ref & out"; this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

Private void button1_click (object sender, system.eventargs e) {int [] firstArray = {1, 2, 3}; int [] firstTARRAYCOPY = firstTARRAY;

THISLABEL1.TEXT = "Test Passing FirstDarray Reference By Value"; this.Label1.Text = "/ N / N / N / N / N / N / N / N / N / N / N / N / N / T"; for (int i = 0; i

THIS.Label1.Text = "/ N / N / N / NContents of Firstdouble./N/T";

For (int i = 0; i

IF (firstARRAY == firstArraycopy) this.label1.text = "/ nthe references refer to the same array./n"; else this.label1.text = "/ nthe reasonT =" / nthe reasonT = "/ nthe reasonT =" / nthe reasonT = "/ nthe reasonT =" / nthe reasonT = "/ nthe reason N ";

Int [] SecondArray = {1, 2, 3}; int [] secondaraycopy = second = "/ ntest passing second =" / ntest passing secondaryRray Reference by reference. "; this.label1.text =" / n / nContents Of SecondArray Before Calling Seconddouble: / N / T ";

For (INT i = 0; i

Seconddouble; this.label1.text = "/ n / nContents of secondaryRayAfter Calling seconddouble: / n / t"; for (int i = 0; i

IF (SecondArray == SecondArrayCopy This.label1.text = "/ N / NTHE REFERENCE TO The Same Array."; else this.label1.text = "/ nthe reference refer to Different Arrays."

this.label1.Text = "/ n ___________________ heshi _________________ / nsecondarray / n"; for (int i = 0; i

} Void firstduble (int [] array) {for (int i = 0; I

to sum up

In general, these two keywords that REF and OUT can provide similar effects, which also use pointer variables in C. A slight difference is:

When using the REF type parameter, the incoming parameters must be initialized first. And OUT does not need. For OUT, it must be initialized in the method. When using REF and OUT, you must pay attention to the parameters of the method and the method of performing the method, add the Ref or OUT keyword. To meet the match. OUT is more suitable for places where multiple returns needed to return, while REF is used to modify the call from the method that needs to be called. Call, long one. I haven't eaten dinner at 20 o'clock (the cafeteria ?!) is dizzy, only eating bubble! Don't do it! I want to eat ramen! Eat Naruto to eat ramen!

转载请注明原文地址:https://www.9cbs.com/read-50125.html

New Post(0)