文章

C#学习笔记一 20210720

using System;
using Tools;
namespace MyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var person1 = new { Name = "Vivian", Age = 18 };
            person.Ffly();
        }
    }
    class person
    {
        public static void Ffly()
        {
            var PP = new { Name = "Vivian", Age = 18 };
            string[] myArray = new string[] { "azhu", "么么哒" };
            Console.WriteLine(myArray[1]);
            int[] myInt = new int[20];
            var Persons = new { Name = "Aliy", Age = 19 };
            int x = 123;
            Console.WriteLine(~x + 1);//取反操作符~,实际是二进制按位取反,0变1,1变0,所以这里需要+1
            string x1 = "133144";
            int y = 1;
            int x2 = Convert.ToInt32(x1);
            Console.WriteLine(x2 + y);
            int? a1 = null; //允许null值的int
            int a2 = a1 ?? 5;//a1如果为null 则赋值5;
            string str = string.Empty;//赋值为空
            str = a2 == 5 ? "a2等于5" : "a2不等于5";//语法糖,a2等于5吗?
            Console.WriteLine(a2);
            Console.WriteLine("str是:" + str);
            a2 <<= 2;//左移赋值,将a2的值左移两位后再赋值给a2,左移为乘法,右移为除法,他们直接移位比乘除法速度更快,位移一次就是*2,这里位移两次就是a2 = a2 *2 *2
            Console.WriteLine(a2);//结果是20
            var yy = Math.Pow(2, 3);//求2的3次方 Math.pow是求方运算符
            Console.WriteLine(yy);//结果是8
        Hello: Console.WriteLine("Hello,World");//Hello是一个标签,类似Delphi的标签
            goto Hello;//goto语句,和Delphi一样,跳转到标签 但是结合上面的,这就是个死循环了.
        }

    }
    class switch1
    {
        public void test()
        {
            var score = 100;
            switch (score)
            {
                case 1: //必须分开写case,而且语句后面必须写break
                case 2:
                    Console.WriteLine("不及格");
                    break;
                default:
                    break;
            }
            try
            {
                string str;
                str = score == 0 ? "你考了个零蛋" : "pass";
            }
            catch (OverflowException) //try语句的捕捉栈溢出
            {
                throw; //自己不处理的话,throw就是重新丢出去,丢给调用者处理
            }
        }

    }
}

MarkDown对C#语言的支持还真不赖,该高亮的高亮.

原文来自:C#学习笔记一 20210720,尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论