C#編程--函數

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        //聲明一個函數
        static int readInt()
        {
            string s = Console.ReadLine();
            return Convert.ToInt32(s);
        }
        //空的返回值
        static void sayHello()
        {
            Console.WriteLine("hello");
        }
        //這個函數用來求和
        static int getSum(int a, int b)
        {
            return a + b;
        }
        //可變參數的函數
        static void VFunc(params string[] values)
        {
            foreach(string str in values){
                Console.WriteLine(str);
            }
        }

        // 函數的重載
        //函數的名字一樣的
        static void PrintAge(string age)
        {
            Console.WriteLine(age);
        }
        static void PrintAge(int age)
        {
            Console.WriteLine(age);
        }
        static void Main(string[] args)
        {
            //函數
            PrintAge("123");
            PrintAge(23);
            Console.WriteLine(readInt());
            VFunc("2");
            VFunc("1", "2");
            VFunc();
            Console.ReadKey();
        }
    }
}
 

本文出自 “Kenan_ITBlog” 博客,請務必保留此出處http://soukenan.blog.51cto.com/5130995/1076240

發佈了115 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章