C#语法学习三(Method)
Code
//简单方法的应用,没有带参数
using System;
namespace Athrun
{
class Method
{
public static void MyMethod()
{
Console.WriteLine("this is a method");
}
static void Main()
{
MyMethod();
}
}
}
Code
//有返回值的方法
using System;
namespace Athrun
{
class Method
{
public static DateTime MyMethod()
{
return DateTime.Now;
}
static void Main()
{
Console.WriteLine("Now time is {0}",MyMethod());
}
}
}
Code
//带参数的方法
using System;
namespace Athrun
{
class Method
{
public static void MyMethod(string aName)
{
Console.WriteLine("this Name is " + aName + "\n");
}
static void Main()
{
string s=" 带参数的方法";
MyMethod(s);
}
}
}
//带参数的方法
using System;
namespace Athrun
{
class Method
{
public static void MyMethod(string aName)
{
Console.WriteLine("this Name is " + aName + "\n");
}
static void Main()
{
string TempName="";
while (TempName!="end")
{
TempName=Console.ReadLine();
MyMethod(TempName);
}
}
}
}
//带参数的方法
using System;
namespace Athrun
{
class Method
{
public static int add(int i,int j)
{
return i+j;
}
static void Main()
{
int i=5;
int j=3;
Console.WriteLine("i+j=" + add(i,j));
}
}
}