C#总结(3)
這次我們來談談函數。
C#的函數分為靜態函數,和普通函數。
先上代碼。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CS總結3
{
class Program
{
static void Main(string[] args)
{
Program.Show1();
Program x = new Program();
x.Show2();
} static void Show1()
{
Console.WriteLine("調用靜態函數");
}
void Show2()
{
Console.WriteLine("調用普通函數");
}
}
}
所看到的結果:
可以看到,靜態函數可以通過類名調用,當然,在本類中,靜態Main方法其實可以直接調用本身的靜態方法。
而普通方法,是存在在類當中的,只有創建該類的實例,才能調用里面的普通函數。
再上一個代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CS總結3
{
class Program
{
static void Main(string[] args)
{
Program.Show1();
Program x = new Program();
x.Show2();
if (i < )
{
++i;
Console.WriteLine(i);
Program.Main(null); }
}
static int i = ;
static void Show1()
{
Console.WriteLine("調用靜態函數");
}
void Show2()
{
Console.WriteLine("調用普通函數");
}
}
}
這是一個遞歸調用Main函數的程序,調用了101次。
總結
- 上一篇: UISearchBar 自定义处理
- 下一篇: c#错误捕获如何定位到某一行?