于梦想齐行
于梦想齐行

C#基础(一)

C#基础(一)
using System;

namespace ConsoleApp1
{
    struct Books
    {
        public string title;
        public string author;
        public string subject;
        public int book_id;
    };
    struct BooksE
    {
        public string title;
        public string author;
        public string subject;
        public int book_id;
        public void setValues(string t, string a, string s, int i)
        {
            title = t;
            author = a;
            subject = s;
            book_id = i;
        }
        public void display()
        {
            Console.WriteLine("title: {0}", title);
            Console.WriteLine("author: {0}", author);
            Console.WriteLine("subject: {0}", subject);
            Console.WriteLine("book_id: {0}", book_id);
        }
    };
    enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
    class Program
    {
        static void Main(string[] args)
        {
            //读取用户输入的数据并转为指定类型
            int num;
            Console.WriteLine("请输入一个整数:");
            num = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("num: {0}", num);

            //常量
            //整数常量
            /*
             * 0x/0X表示十六进制,0表示八进制,没有前缀则表示十进制。
             * 整数常量也可以有后缀,U和L分别表示unsigned和long。
             */
            int a1 = 0xff;
            int a2 = 022;
            uint a3 = 999U;
            long a4 = 999L;
            Console.WriteLine("a1: {0}, a2: {1}, a3: {2}, a4: {3}", a1, a2, a3, a4);
            //字符常量
            /*
             * 字符常量是括在单引号里,例如'x'。一个字符常量可以是一个普通字符(例如'x')、一个转移序列(例如'\t')或者一个通用字符(例如'\u02C0')。
             * 
             */
            //字符串常量
            /*
             * 字符串常量是在双引号里或者在@""内。
             */
            String a5 = "ok";
            String a6 = @"ok";
            Console.WriteLine("a5:{0}, a6: {1}", a5, a6);
            //定义常量
            //常量使用const关键字来定义的,
            const int a7 = 99;
            Console.WriteLine(a7);
            Coordinate co = new Coordinate(5, 17);
            Console.WriteLine("x={0},y={1}", co.x, co.y);
            Console.WriteLine("c1={0},c2={1}", Coordinate.c1, Coordinate.c2);

            //位操作
            int a = 60;
            int b = 13;
            Console.WriteLine("a&b: {0}", a&b);
            Console.WriteLine("a|b: {0}", a|b);
            Console.WriteLine("a^b: {0}", a^b);
            Console.WriteLine("~a: {0}", ~a);
            Console.WriteLine("a<<2: {0}", a<<2);
            Console.WriteLine("a>>2: {0}", a>>2);

            //其他运算符
            //sizeof: 返回数据类型的大小
            //typeof: 返回class的类型
            //&: 返回变量地址
            //*: 变量的指针 *a 指向一个变量
            //?: :条件表达式  b = (a == 1)?20:30;
            //is: 判断对象是否为某一类型 Ford is Car
            //as: 强制类型转换,即使转换失败也不会抛出异常。
            /*
             * Object obj = new StringReader("hello");
             * StringReader r = obj as StringReader;
             */
            /*
             * 运算符优先级(从高到低)
             * 类别          运算符                         结合性

             * 后缀          ()[]->.++--                     从左到右
             * 一元          +-!~++--(type)* & sizeof        从右到左
             * 乘除          * / %                           从左到右
             * 加减          +-                              从左到右
             * 移位          << >>                           从左到右
             * 关系          < <= > >=                       从左到右
             * 相等          == !=                           从左到右
             * 位与AND       &                               从左到右
             * 位异或XOR      ^                              从左到右
             * 位或          |                               从左到右
             * 逻辑与AND      &&                             从左到右
             * 逻辑或OR       ||                             从左到右
             * 条件           ?:                             从右到左
             * 赋值        = += -= *= /= %= >>= <<= &= ^= |= 从右到左
             * 逗号          ,                               从左到右
             */


            //判断
            bool j1 = true;
            if(j1)
            {
                Console.WriteLine("满足条件");
            }else
            {
                Console.WriteLine("不满足条件");
            }
            int j2 = 3;
            switch(j2)
            {
                case 0:
                    Console.WriteLine("j2=0");
                    break;
                case 1:
                    Console.WriteLine("j2=1");
                    break;
                case 2:
                    Console.WriteLine("j2=2");
                    break;
                case 3:
                    Console.WriteLine("j2=3");
                    break;
                default:
                    Console.WriteLine("its value is not equal to either.");
                    break;
            }
            bool j3 = j2 == 3 ? false : true;
            Console.WriteLine("j3: {0}", j3);


            //循环
            //while
            int l1 = 0;
            while(l1 < 9)
            {
                l1++;
                Console.WriteLine("l1: {0}", l1);
            }
            //for
            for(int l2 = 0; l2<9;l2++)
            {
                Console.WriteLine("l2: {0}", l2);
            }
            //foreach
            int[] l3 = new int[] { 1,2,3,4,5,6,7,8,9};
            foreach(int i in l3)
            {
                Console.WriteLine("l3: {0}", i);
            }
            //do...while
            int l4 = 0;
            do
            {
                l4++;
                Console.WriteLine("l4: {0}", l4);
            } while (l4 < 9);

            //break语句
            int l5 = 0;
            while(l5 < 20)
            {
                if (l5 > 15) break;
                l5++;
                Console.WriteLine("l5: {0}", l5);
            }
            //continue语句
            int l6 = 0;
            while(l6 < 9)
            {
                l6++;
                if (l6 == 6) continue;
                Console.WriteLine("l6: {0}", l6);
            }


            //封装
            //访问修饰符
            //public: 所有对象都可以访问
            //private: 对象本身在对象内部可以访问
            //protected: 只有该类对象及其子类对象可以访问
            //internal: 同一个程序集的对象可以访问
            //protected internal: 访问限于当前程序集或派生自包含类的类型。相当于protected + internal


            int m1 = 100;
            int m2 = 200;
            int mret1;
            NumberManipulator nm = new NumberManipulator();
            mret1 = nm.FindMax(m1, m2);
            Console.WriteLine("最大值是: {0}", mret1);
            Console.WriteLine("6的阶乘: {0}", nm.factorial(6));
            Console.WriteLine("7的阶乘: {0}", nm.factorial(7));
            Console.WriteLine("8的阶乘: {0}", nm.factorial(8));
            Console.WriteLine("9的阶乘: {0}", nm.factorial(9));


            //参数传递
            //值传递:复制参数的实际值给函数的形式参数,实参和形参使用的是两个不同内存中的值。在这种情况下,当形参的值发生改变时,不会影响实参的值,从而保证了实参数据的安全。
            Console.WriteLine("值传递");
            int s1 = 0;
            int s2 = 9;
            Console.WriteLine("交换前 -> s1: {0}, s2: {1}", s1, s2);
            swap(s1, s2);
            Console.WriteLine("交换后 -> s1: {0}, s2: {1}", s1, s2);
            //引用参数:复制参数的内存位置的引用给形式参数。这意味着,当形参的值发生改变时,同时也改变实参的值。
            Console.WriteLine("引用参数");
            int s3 = 0;
            int s4 = 9;
            Console.WriteLine("交换前 -> s3: {0}, s4: {1}", s3, s4);
            swap(ref s3, ref s4);
            Console.WriteLine("交换后 -> s3: {0}, s4: {1}", s3, s4);
            //输出参数:这种方式可以返回多个值。
            int s5 = 157;
            Console.WriteLine("输出参数");
            Console.WriteLine("方法调用前,s5: {0}", s5);
            getValue(out s5);
            Console.WriteLine("方法调用后,s5: {0}", s5);

            //输出参数的使用示例
            int s6, s7;
            getValues(out s6, out s7);
            Console.WriteLine("用户输入后s6的值: {0}", s6);
            Console.WriteLine("用户输入后s7的值: {0}", s7);


            //可空类型Nullable(其基础值范围内再加上一个null值)
            //?单问号用于对int、double、bool等无法直接赋值为null的数据类型进行赋值,意思是这个数据类型是Nullable类型的。
            int? i1 = 3;
            Console.WriteLine("i1: {0}", i1);
            //等同于
            Nullable<int> i2 = new Nullable<int>(3);
            Console.WriteLine("i2: {0}", i2);
            //int i;//默认值0
            //int? i;//默认值null
            //??双问号用于判断一个变量在为null的时候返回一个指定的值。
            //在处理数据库和其他包含可能未赋值的元素的数据类型时,将null赋值给数据类型或布尔类型的功能特别有用。例如,在数据库中布尔字段的取值包括:true、false、未定义。

            int? num1 = null;
            int? num2 = 45;
            double? num3 = new double?();
            double? num4 = 3.1415;
            bool? boolval = new bool?();
            Console.WriteLine("显示可空类型的值:{0},{1},{2},{3}", num1, num2, num3, num4);
            Console.WriteLine("一个可空的布尔值:{0}", boolval);

            //Null合并运算符
            //用于定义可空类型和引用类型的默认值。null合并运算符为类型转换定义了一个预设值,以防可空类型的值为Null。Null合并运算符把操作数类型隐式转换为另一个可空(或不可空)的值类型的操作数类型。
            //如果第一个操作数的值为null,则运算符返回第二个操作数的值,否则返回第一个操作数的值。
            double? num5 = null;
            double? num6 = 3.1415;
            double num7;
            num7 = num5 ?? 5.34;//如果num5的值为null,则返回5.34。
            Console.WriteLine("num7的值:{0}", num7);
            num7 = num6 ?? 5.34;
            Console.WriteLine("num7的值:{0}", num7);

            //数组:存储相同类型元素的固定大小的顺序集合。
            //声明
            //double[] d1;
            //初始化
            //double[] d1 = new double[10];
            //赋值1
            double[] b1 = new double[10];
            b1[0] = 4500.0;
            //赋值2
            double[] b2 = { 234.0, 222.1, 232.9 };
            //赋值e3
            int[] b3 = new int[3] { 1,2,3 };
            //=
            int[] b4 = new int[] { 1,2,3 };
            //赋值4
            int[] marks = new int[] { 1,2,3,4,5,6,7,8,9};
            int[] score = marks;
            //赋值一个数组变量到另一个目标数组变量中,在这种情况下,目标和源会指向相同的内存位置。
            //
            //当创建一个数组时,C#编译器会根据数组类型隐式的初始化每个数组元素为一个默认值。例如, int数组的所有元素都会被初始化为0。

            //Array类的部分方法
            int[] list = { 34,72,13,44,25,30,10 };
            Console.WriteLine("原始数组:");
            foreach(int i in list)
            {
                Console.Write(i+" ");
            }
            Console.WriteLine();
            //逆转数组
            Array.Reverse(list);
            Console.WriteLine("逆转数组:");
            foreach(int i in list)
            {
                Console.Write(i+" ");
            }
            Console.WriteLine();
            //排序数组
            Array.Sort(list);
            Console.Write("排序数组:");
            foreach (int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            /*
             * 原始数组: 34 72 13 44 25 30 10
             * 逆转数组: 10 30 25 44 13 72 34
             * 排序数组: 10 13 25 30 34 44 72
             */


            //字符串
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";
            string fullname = fname + lname;
            Console.WriteLine("Full Name: {0}", fullname);
            //通过使用string构造函数
            char[] letters = { 'H', 'e', 'l', 'l', 'o'};
            string greetings = new string(letters);
            Console.WriteLine("Grettings: {0}", greetings);
            //方法返回字符串
            string[] sarray = {"Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message: {0}", message);
            //用于转化值的格式化方法
            DateTime waiting = new DateTime(2012,10,10,17,58,1);
            string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
            Console.WriteLine("Message: {0}", chat);

            //String的一些方法
            //比较
            string str1 = "This is text";
            string str2 = "This is text";
            if (String.Compare(str1, str2) == 0)
            {
                Console.WriteLine("str1 and str2 are equal.");
            }
            else
            {
                Console.WriteLine("str1 and str2 are not equal.");
            }
            //包含
            string str3 = "This is text";
            if(str3.Contains("text"))
            {
                Console.WriteLine("The sequence 'text' was found.");
            }
            //获取子字符串
            string str4 = "last night i dream of san perdro";
            Console.WriteLine(str4);
            string str4sub = str4.Substring(23);
            Console.WriteLine(str4sub);
            //连接字符串
            string[] str5 = new string[] { "ok1", "ok2", "ok3"};
            string str5all = String.Join("\n", str5);
            Console.WriteLine(str5all);



            //结构体
            //定义结构体
            Books book1;
            Books book2;
            book1.title = "C Programming";
            book1.author = "Nuha Ali";
            book1.subject = "C Programming Tutorial";
            book1.book_id = 6495407;

            book2.title = "Telecom Billing";
            book2.author = "Zera Ali";
            book2.subject = "Telecom Billing Tutorial";
            book2.book_id = 6495700;

            Console.WriteLine("book1 title : {0}", book1.title);
            Console.WriteLine("book1 author : {0}", book1.author);
            Console.WriteLine("book1 subject : {0}", book1.subject);
            Console.WriteLine("book1 book_id :{0}", book1.book_id);

            Console.WriteLine(book2);

            BooksE book3 = new BooksE();
            book3.setValues("C Programming", "Nuha Ali", "C Programming Tutorial", 6495407);
            book3.display();

            //结构体和类的不同
            /*
             * 1.结构体中声明的字段无法赋予初值,类可以:
             * struct test001 {
             *     private int aa = 1;
             * };
             * class test002 {
             *     private int aa = 1;
             * }
             * 2.结构体的构造函数中,必须为结构体所有字段赋值,类的构造函数无此限制。
             * 
             * 类的对象是存储于堆空间中,结构存储在栈中。堆空间大,但访问速度慢,栈空间小,访问速度快。
             */


            //枚举enum
            int x = (int)Day.Sun;
            int y = (int)Day.Fri;
            Console.WriteLine("Sun = {0}", x);//0
            Console.WriteLine("Fri = {0}", y);//5
            

            Console.ReadKey();
        }

        static void swap(int x, int y)
        {
            int temp;
            temp = x;
            x = y;
            y = temp;
        }

        static void swap(ref int x, ref int y)
        {
            int temp;
            temp = x;
            x = y;
            y = temp;
        }

        static void getValue(out int x)
        {
            int temp = 9;
            x = temp;
        }

        static void getValues(out int x, out int y)
        {
            Console.WriteLine("请输入第一个值:");
            x = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第二个值:");
            y = Convert.ToInt32(Console.ReadLine());
        }
    }
    class NumberManipulator
    {
        //方法
        //<Access Specifier> <Return Type> <Method Name> (Parameter List) {
        //Method Body
        //}
        public int FindMax(int n1, int n2)
        {
            int result;
            if (n1 > n2)
            {
                result = n1;
            }
            else
            {
                result = n2;
            }
            return result;
        }
        //递归调用(阶乘)
        public int factorial(int num)
        {
            int result;
            if (num == 1)
            {
                return 1;
            } else
            {
                result = factorial(num - 1) * num;
                return result;
            }
        }
    }
    class Coordinate
    {
        public int x;
        public int y;
        public const int c1 = 5;
        public const int c2 = c1 + 5;
        public Coordinate(int p1, int p2)
        {
            x = p1;
            y = p2;
        }
    }
}
没有标签
首页      CSharp      C#基础(一)

于梦想齐行

C#基础(一)
using System; namespace ConsoleApp1 { struct Books { public string title; public string author; public string subject; publi…
扫描二维码继续阅读
2022-06-25
近期文章
近期评论