C#学习日志 1

发布于 2024-03-05  311 次阅读


1.类型

强制类型转换

类型1 x1 = (类型1)x2 (类型2强制转化为类型1)

double test = 50;
int test2 = (int)test;


使用System.Convert类的静态成员方法,ToString,ToInt,ToDouble等等

double test = 50;
string test2 = System.Convert.ToString(test)


使用.Net框架提供的Parse()方法

string test = 50
double test2 = float.Parse(test)


C#的数组命名方式

类型[] 数组名 = new [数组] or 类型[] 数组名 = {数组值}

int[] a = { 1, 2, 3 };
string[] b = { "red", "blue", "test" };

2.方法

当需要从方法中返回两个及两个以上的值可用

输出参数OUT(输出赋值) 无需先对参数赋值

class Test1
{
    static void Main()
    {
        int A, B;
        Test2 k1 = new Test2();
        k1.Test1(out A, out B);//直接从Test1中获取参数A,B的值
        Console.WriteLine(B);
        Console.WriteLine(A);
    }
}

class Test2
{
   public void Test1(out int A,out int B)
    {
        A = System.Convert.ToInt16(Console.ReadLine());//输入 A 的值
        B = System.Convert.ToInt16(Console.ReadLine());//输入 B 的值
    }
}


ref 引用赋值 需要先对参数赋值

class Test1
{
    int A,B;
    static void Main()
    {
        int A = 200, B = 200;
        Test2 k1 = new Test2();
        Console.WriteLine(B);
        Console.WriteLine(A);
        k1.Test1(ref A, ref B);
        Console.WriteLine(B);
        Console.WriteLine(A);
    }
}

class Test2
{
   public void Test1(ref int A,ref int B)
    {
        A = System.Convert.ToInt16(Console.ReadLine());
        B = System.Convert.ToInt16(Console.ReadLine());
    }
}
一个练习时长还差一年半的切图仔,有需求可联系 QQ:963827384
最后更新于 2024-03-05