成员访问修饰符   返回值   方法名称 (参数列表)

{

     //方法的内容

}

1、方法名称不可重复,大小写视为不同。

2、在小括号中编写参数列表。

3、方法的内容包含在{}之中。

//一个无返回值的方法

static void Main(string[] args)
        {
            myMethod();
        }
        public static void myMethod() 
        {
            Console.WriteLine("Hello Wrod!");
            Console.ReadLine();
        }

 //一个带返回值的方法

static void Main(string[] args)
        {
            Console.WriteLine("the DateTime is Now " + myMethod());
            Console.ReadLine();
        }
        public static DateTime myMethod()
        {
            return DateTime.Now;
        }

//一个带参数的方法

static void Main(string[] args)
        {
            string str = "";
            while (str != "exit")
            {
                str = Console.ReadLine();
                myMethod(str);
            }
        }
        public static void myMethod(string aName) 
        {
            Console.WriteLine("this is {0}",aName);
        }

//一个带多个参数的方法

static void Main(string[] args)
        {
            int i,k=0;
            int.TryParse(Console.ReadLine(), out i);
            int.TryParse(Console.ReadLine(),out k);
            Console.WriteLine(myMethod(i, k));
            Console.ReadLine();
        }
        public static int myMethod(int i,int k) 
        {
            return i + k;
        }

 

相关文章:

  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-28
  • 2021-06-12
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2022-01-05
相关资源
相似解决方案