day07_22_10_9

69 阅读1分钟

C# 基础

out 参数

 // out 参数不能赋初始值
static bool check(out int num1,out string str) {

    num1 = 23;
    str = "ewq";
    return true;
}

int num2 = 12;
string str;
check(out num2, out str);

Console.WriteLine(num2); // 23
Console.WriteLine(str); // euq

ref 参数

// ref 与 out 相似,但是 ref 可以不赋值, out必须赋值
static void getNum(ref int n1) { 

}

int n = 1;

getNum(ref n);
Console.WriteLine(n); // 1

汉诺塔递归算法

// 创建一个函数 这个函数能够将 n个圈从 xx 移到 xx
static void hanno(int n, string start, string middle, string end)
{

    // 出口
    if (n == 1)
    {
        Console.WriteLine("从{0}经过{1}移到{2}",start,middle,end);
        return;
    }
    hanno(n - 1, start, end, middle);
    hanno(1, start, middle, end);
    hanno(n - 1, middle, start, end);
}

            hanno(3, "A", "B", "C");

常量 const

// 常量 const
const int num = 1;

枚举类

// 枚举
enum Day { Mon, Tue, Wed };
enum Role { 战士, 坦克 };
static void Main()
{
    Day day = Day.Mon;
    
    // 遍历枚举
    // GetNames :获取名字
    foreach (string i in Enum.GetNames(typeof(Day)))
    {
        Console.WriteLine(i);
    }

    // GetValues :获取值
    foreach (int i in Enum.GetValues(typeof(Role))) {
        Console.WriteLine(i);
    }
}