C#与C不同的一些基础命令

using System;
using System.Collections.Generic;       // 包含List对象
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections;   // ArrayList使用需要添加
using System.Threading;     // 多线程

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            t_thread();
        }

        
        /* 结构是一种值类型,通常用来封装一组相关的变量
         * 结构的构造函数必须带参数
         * 结构可以实现接口,不能从另一个结构或类继承
         * 示例代码:
         *  t_struct rect;
         *  rect.width = 2;
         *  rect.height = 3;
         *  Console.WriteLine(rect.Aread());
         */
        struct t_struct
        {
            public double width;
            public double height;
            public void Rect(double x, double y)
            {
                width = x;
                height = y;
            }
            public double Aread()
            {
                return width * height;
            }
        }

        // foreach用于枚举一个集合的元素,不应用于更改该集合内容,已避免产生不可避免的错误
        static void t_foreach()
        {
            ArrayList alt = new ArrayList();    // 可以动态添加和删除元素,元素需要更改推荐使用这种
            alt.Add("hello");
            alt.Add("wawu");
            foreach(string name in alt)
            {
                Console.WriteLine(name);
            }

            int[] arr = new int[2] { 1, 2 };
            int[] arrAdd = new int[arr.Length + 1]; // 当前没有使用,添加数组元素
            // 结合数组
            foreach(int number in arr)
            {
                Console.WriteLine(number);
            }
        }

        // switch中如果有语句的都必须加break,需要执行多个只能添加goto跳转执行
        static void t_switch()
        {
            int i = 0;

            switch (i)
            {
                case 0:
                    // 如果这里有语句就必须有break命令结束,没有则调到下一步继续执行
                case 1:
                    Console.WriteLine("value is 1");
                    goto case2;
                case 2:
                    case2:          // 上面的goto跳转到这里
                    Console.WriteLine("value is 2");
                    break;
                default:
                    break;
            }
        }

        // 链表使用
        static void t_list()
        {
            List list = new List();
            list.Add(1);
            list.Add(3);
            Console.WriteLine("list总的个数:" + list.Count);
            foreach(int i in list)
            {
                Console.WriteLine(i);
            }

            list.Clear();
            Console.WriteLine("list总的个数:" + list.Count);
        }

        /* 多线程
         * Thread.Sleep(time) 线程延时时间
         * Start启动线程;Suspend挂起;Resume取消挂起;Abort终止运行;Join运行线程结束前阻塞其他线程;Sleep暂停
         * Priority线程优先级;互锁(比如互锁打印任务)三种方法(lock、Monitor、Mutex)
         */
        static void t_thread()
        {
            Thread thread1 = new Thread(new ThreadStart(t_list));
            Thread thread2 = new Thread(new ThreadStart(t_switch));
            thread1.Priority = ThreadPriority.Lowest;   // 设定为低优先级运行
            thread2.Priority = ThreadPriority.Highest;  // 设定为高优先级运行
            Thread.Sleep(100);     // 线程停止运行
            
            thread1.Start();

            // 判断线程是否正在运行
            if(thread1.ThreadState == ThreadState.Running)
            {
                //当前这两个指令被废弃不建议使用了,并且不要把其他语句放到这个语句之前,以避免判断还在运行,但调用挂起时已经停止运行
                thread1.Suspend();      // 挂起线程
                thread1.Resume();       // 继续执行已挂起的线程

                Console.WriteLine("thread1调用的t_list正在运行,下面挂起后继续执行,但可能被Abort中止只执行一部分");
            }
            
            thread1.Abort("终止此线程");
            
            thread1.Join();      // 阻塞调用线程,直到运行中的线程终止为止

            thread2.Start();

            Thread.Sleep(100);
        }
    }

    /* get读取当前变量,set设置当前变量
     * Add add = new Add(1,2);
     * Console.WriteLine(add.result);
     */ 
    class Add
    {
        int a = 0, b = 0;
        public int A       // 使用时,给A赋值并将变量保存到a中
        {
            set
            {
                a = value;  
            }
        }
        public int B
        {
            set
            {
                b = value;
            }
        }

        public int result
        {
            get
            {
                return a + b;
            }
        }

        public Add(int a, int b)
        {
            this.a = a;
            this.b = b;
        }
    }
}

 

你可能感兴趣的:(C#,C#,基础,switch,foreach,struct)