簡單的函數定義:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 定義一個方法,並提供參數傳遞
public static int GetMax(int x, int y)
{
return x > y ? x : y;
}
// 定義一個判斷閏年的方法
public static bool IsRun(int year)
{
bool ret = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
return ret;
}
static void Main(string[] args)
{
int max = Program.GetMax(100, 200);
Console.WriteLine("最大值: {0}", max);
bool ret = Program.IsRun(2020);
Console.WriteLine("閏年: {0}", ret);
Console.ReadKey();
}
}
}
方法傳遞數組/字符串:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 方法傳遞數組
public static int GetSum(int[] Array)
{
int sum = 0;
for (int x = 0; x < Array.Length; x++)
sum += Array[x];
return sum;
}
// 方法傳遞字符串
public static string IsString(string str)
{
return str;
}
static void Main(string[] args)
{
int[] Array = new int[] { 1, 2, 3, 4, 5 };
int ret_sum = Program.GetSum(Array);
Console.WriteLine("相加結果: {0}", ret_sum);
string ret_str = Program.IsString("hello lyshark");
Console.WriteLine("字符串: {0}", ret_str);
Console.ReadKey();
}
}
}
Out 方法返回多個參數: 類似與C++中的多指針傳遞,就是說可以一次性傳出多個參數。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 返回 max => 最大值 / main => 最小值
public static void GetNum(int[] Array, out int Max, out int Min)
{
int max = int.MinValue;
int min = int.MaxValue;
for (int i = 0; i < Array.Length; i++)
{
if (Array[i] > max)
max = Array[i];
if (Array[i] < min)
min = Array[i];
}
Max = max;
Min = min;
}
static void Main(string[] args)
{
int[] Array = new int[] { 2,6,9,3,10 };
int Max = 0;
int Min = 0;
GetNum(Array,out Max,out Min);
Console.WriteLine("最大值: {0}", Max);
Console.WriteLine("最小值: {0}", Min);
Console.ReadKey();
}
}
}
Out 實現參數返回:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 自己實現TryParse
public static bool MyTryParse(string Str,out int result)
{
result = 0;
try
{
result = Convert.ToInt32(Str);
return true;
}
catch
{
return false;
}
}
static void Main(string[] args)
{
int number = 0;
bool sys_ret = int.TryParse("123", out number);
Console.WriteLine("系統轉換結果輸出: {0} 狀態: {1}", number,sys_ret);
bool my_ret = Program.MyTryParse("456", out number);
Console.WriteLine("My轉換結果輸出: {0} 狀態:{1}", number,my_ret);
Console.ReadKey();
}
}
}
Ref 變量指針交換:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 變量交換,類似於指針傳遞
public static void Exchange(ref int x,ref int y)
{
int tmp = x;
x = y;
y = tmp;
}
static void Main(string[] args)
{
int x = 100;
int y = 200;
Exchange(ref x, ref y);
Console.Write("交換后: x = {0} y = {1}", x, y);
Console.ReadKey();
}
}
}
params 傳遞可變參數:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 指定可變參數
public static void GetName(int Number,params string[] Str)
{
for (int x = 0; x < Str.Length; x++)
Console.Write(Number + " " + Str[x] + " ");
}
static void Main(string[] args)
{
GetName(1001,"admin", "lyshark", "guest");
Console.ReadKey();
}
}
}
實現方法重載
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
// 方法重載
public static double Sum(double x,double y)
{
return x + y;
}
public static int Sum(int x, int y)
{
return x + y;
}
static void Main(string[] args)
{
Console.WriteLine("int => {0}", Sum(10, 20));
Console.WriteLine("double => {0}", Sum(10.5, 20.5));
Console.ReadKey();
}
}
}