05. C#中的方法(1)
1.
namespace _05.方法_1_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("this is a Method");
}
}
}
2.
namespace _05.方法_1_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Method();
}
public static void Method()
{
MessageBox.Show("this is a Method");
}
}
}
3.带返回值得方法
namespace _05.方法_1_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("the time is " + Method());
}
public static DateTime Method()
{
return DateTime.Now;
}
}
}
4. 带参数的方法
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string TempName = "Me !";
MyMethod(TempName);
}
public static void MyMethod(string AName)
{
MessageBox.Show("this is "+AName);
}
}
5. 带有多参数的方法
namespace _05.方法_1_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int i = 5;
int k = 6;
MessageBox .Show(""+Add(i,k));
}
public static int Add(int i,int k)
{
return i + k;
}
}
}
6. 共享参数
namespace _05.方法_1_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
A a = new A();
a.addi();
A b = new A();
b.addi();
MessageBox.Show(""+A.i);
}
class A
{
public static int i = 0;
public void addi()
{
i = i + 1;
}
}
}
}