参数数列
参数数列能够使多个相关的参数被单个数列代表,换就话说,参数数列就是变量的长度。
using
System;
class Test
{
static void F(params int[] args)
{
Console.WriteLine("# 参数: {0}", args.Length);
for (int i = 0; i < args.Length; i++)
Console.WriteLine("/targs[{0}] = {1}", i, args[i]);
}
static void Main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(new int[] {1, 2, 3, 4});
}
}
以下为输出结果:
# 参数: 0
# 参数: 1
args[ 0 ] = 1
# 参数: 2
args[ 0 ] = 1
args[ 1 ] = 2
# 参数: 3
args[ 0 ] = 1
args[ 1 ] = 2
args[ 2 ] = 3
# 参数: 4
args[ 0 ] = 1
args[ 1 ] = 2
args[ 2 ] = 3
args[ 3 ]
using System;
public class TextBox
{
TextBox()
{
this.GetMessage += new EventHandler(Print);
}
public delegate void EventHandler(string message);
public event EventHandler GetMessage;
//OnGetMessage方法用于触发GetMessage
public void OnGetMessage(string message)
{
if (GetMessage != null)
{
GetMessage(message);
}
}
public static void Main()
{
TextBox myTextBox = new TextBox();
//myTextBox.GetMessage += new EventHandler(Print);
myTextBox.OnGetMessage("dfkd");
}
public static void Print(string message)
{
Console.WriteLine(message);
}
}
class Test
{
static void F(params int[] args)
{
Console.WriteLine("# 参数: {0}", args.Length);
for (int i = 0; i < args.Length; i++)
Console.WriteLine("/targs[{0}] = {1}", i, args[i]);
}
static void Main()
{
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(new int[] {1, 2, 3, 4});
}
}
以下为输出结果:
# 参数: 0
# 参数: 1
args[ 0 ] = 1
# 参数: 2
args[ 0 ] = 1
args[ 1 ] = 2
# 参数: 3
args[ 0 ] = 1
args[ 1 ] = 2
args[ 2 ] = 3
# 参数: 4
args[ 0 ] = 1
args[ 1 ] = 2
args[ 2 ] = 3
args[ 3 ]
using System;
public class TextBox
{
TextBox()
{
this.GetMessage += new EventHandler(Print);
}
public delegate void EventHandler(string message);
public event EventHandler GetMessage;
//OnGetMessage方法用于触发GetMessage
public void OnGetMessage(string message)
{
if (GetMessage != null)
{
GetMessage(message);
}
}
public static void Main()
{
TextBox myTextBox = new TextBox();
//myTextBox.GetMessage += new EventHandler(Print);
myTextBox.OnGetMessage("dfkd");
}
public static void Print(string message)
{
Console.WriteLine(message);
}
}