动态方法可调动态、静态
静态方法只能调静态
using System;
namespace test5
{
class Program
{
static void Test() { }
static void Main(string[] args)
{
Test();
Console.WriteLine("Hello World!");
}
}
}
using System;
namespace test5
{
class Program
{
static String Test() { return "123"; }
static void Main(string[] args)
{
Console.WriteLine(Test());
}
}
}
using System;
using System.Collections.Generic;
namespace test5
{
class Program
{
static double Average1(List<int> list)
{
int sum=0;
foreach (var l in list)
{
sum += l;
}
return (double)sum/list.Count;
}
static void Main(string[] args)
{
var list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(5);
Console.WriteLine(Average1(list));
}
}
}
传值:复制变量值,不改变变量原本的值
传引用:类似数组,传的地址,改变值也改变原先的值,在变量前面加ref
out参数:
using System;
using System.IO;
namespace test5
{
class Program
{
static long FileCount(string path)
{
long count = 0;
//try { } catch(){ } finally { }
try
{
//统计路径文件个数
var files = Directory.GetFiles(path);
count += files.Length;
//统计路径下文件夹内文件个数
var dirs = Directory.GetDirectories(path);
foreach (var dir in dirs)
{
count+=FileCount(dir);
}
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine(ex.Message);
//throw;//抛给当前异常交给外层处理
}
return count;
}
static void Main(string[] args)
{
Console.WriteLine(FileCount("F:/"));
}
}
}