📄 refouttest.cs
字号:
// Fig. 4.5: RefOutTest.cs
// Demonstrating ref and out parameters.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace RefOutTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class RefOutTest : System.Windows.Forms.Form
{
private System.Windows.Forms.Button showOutputButton;
private System.Windows.Forms.Label outputLabel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public RefOutTest()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.showOutputButton = new System.Windows.Forms.Button();
this.outputLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// showOutputButton
//
this.showOutputButton.Location = new System.Drawing.Point(123, 8);
this.showOutputButton.Name = "showOutputButton";
this.showOutputButton.Size = new System.Drawing.Size(83, 23);
this.showOutputButton.TabIndex = 0;
this.showOutputButton.Text = "Show Output";
this.showOutputButton.Click += new System.EventHandler(this.showOutputButton_Click);
//
// outputLabel
//
this.outputLabel.Location = new System.Drawing.Point(8, 56);
this.outputLabel.Name = "outputLabel";
this.outputLabel.Size = new System.Drawing.Size(312, 216);
this.outputLabel.TabIndex = 1;
//
// RefOutTest
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 285);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.outputLabel,
this.showOutputButton});
this.Name = "RefOutTest";
this.Text = "RefOutTest";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run( new RefOutTest() );
}
// x passed in by value and method cannot modify
// original variable's value
void Square( int x )
{
x = x * x;
outputLabel.Text +=
"Value of variable within Square, " +
"after calculation: " + x + "\n";
}
// x passed in by reference and method modifies
// original variable's value
void SquareRef( ref int x )
{
x = x * x;
outputLabel.Text +=
"Value of variable within SquareRef, " +
"after calculation: " + x + "\n";
}
// x passed in as out parameter and method initializes
// and modifies original variable's value
void SquareOut( out int x )
{
x = 6; // initialize
x = x * x; // modify
outputLabel.Text +=
"Value of variable within SquareOut, " +
"after calculation: " + x + "\n";
}
private void showOutputButton_Click(
object sender, System.EventArgs e )
{
int y = 5; // create new int and initialize to 5
int z; // declare z, but do not initialize it
// display original value of y
outputLabel.Text =
"Value of y before calling SquareRef: " + y + "\n";
// use ref keyword to pass y by reference
SquareRef( ref y );
outputLabel.Text +=
"Value of y after exiting SquareRef: " + y + "\n";
// display original value of z
outputLabel.Text +=
"\nValue of z before calling SquareOut: " +
"uninitialized\n";
// z not yet initialized; must use
// out keyword to pass z by reference
SquareOut( out z );
outputLabel.Text +=
"Value of z after exiting SquareOut: " + z + "\n";
outputLabel.Text +=
"____________________________________\n\n";
// display value of y
outputLabel.Text +=
"Value of y before calling Square: " + y + "\n";
// pass y by value
Square( y );
outputLabel.Text +=
"Value of y after exiting Square: " + y + "\n";
// display value of z
outputLabel.Text +=
"\nValue of z before calling Square: " + z + "\n";
// pass z by value
Square( z );
outputLabel.Text +=
"Value of z after exiting Square: " + z + "\n";
} // end method showOutputButton_Click
} // end class RefOutTest
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -