2.2.txt

来自「《Microsoft Visual C# .NET 2003开发技巧大全》源代码」· 文本 代码 · 共 45 行

TXT
45
字号
Listing 2.2 Reflecting Parameter Changes by Using the ref Modifier
namespace _3_Methods
{
public class Television
{
// current channel tv is set to
private static int channel = 2;
private const int maxChannels = 200;
// changes the channel to a specified channel
public bool ChangeChannel(int newChannel)
{
// only supports 200 channels
if( newChannel > maxChannels )
return false;
// set private channel variable
channel = newChannel;
return true;
}
public void GetChannel( int param )
{
param = channel;
}
// overloaded version. Note ref parameter
public void GetChannel( ref int param )
{
param = channel;
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
// create Television object
Television tv = new Television();
int refChan = 0;
int chan = 0;
// chan remains 0 after call
tv.GetChannel( chan );
// refChan’s value will change
tv.GetChannel( ref refChan );
Console.WriteLine( “noref={0} ref={1}”, chan, refChan );
}
}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?