C#_001_各种声明

1.多维数组:

int[, , ,] sz = new int[3, 3, 3, 3];

2.结构体:

struct direnJG  //结构体定义声明
{
    public int direnx;
    public youxi yX;//可写入枚举、函数
}

3.枚举:

enum youxi : int//枚举定义:int 可输入类型 byte、sbyte、short、ushort、int、uint、long 或 ulong	
{
    kaishi = 10,//赋初始标号(,间隔元素)
    jixu
}

4.函数:

   public  int ceshi(params int[] shuzu){}//参数数组params!!!!!!
   //使用: ceshi(a,a1,a2,a3.....);

5.委托:

    Func <int,int, int,int,bool> wt004 = (a,b,c,d)=> a > b;  //  有/无参数,有返回(最后一位)
    Action awt001 = () => { };                              //   有/无参数,无返回
    public delegate int WT001(int a, int b);    //委托定义

6.字段与属性:

class ziduan
{
    private int name;
    public int Name { get { return name; } set { this.name = value; } }//属性封装字段
}

7.虚方法抽象方法

   public abstract void move()//抽象方法
   public virtual void move()// virtual 虚方法
   public override void move()//重写方法!!!
点击打开链接

8.接口:

interface IQueue<T>

抽象方法与接口区别

注:虚方法,抽象方法,接口,隐藏方法(有点迷糊...)

9.List集合:

      List<int>alist=new List<int>(4);
      var blist = new List<int>() { 1, 2, 3 };

10.线程与任务:

 Thread xc = new Thread((a) => 
            {
                Console.WriteLine("开始下载..."+Thread.CurrentThread.ManagedThreadId);//线程id
                Thread.Sleep(500);
                Console.WriteLine("下载完成!!!"+a);         
            });
            xc.IsBackground = true;//设置为后台(主线程结束后强制关闭)
            xc.Start("你好");//传递参数并开始线程
            Console.WriteLine("进程运行...");
           // xc.Abort();//强制终止
           // xc.Join();//等待该线程执行完毕
           ThreadPool.QueueUserWorkItem(方法名);//线程池!!!!!
           ThreadPool.QueueUserWorkItem(方法名);
//任务: 
          Task xc3 = new Task(qd2);//任务!!!!
            xc3.Start();                           //线程2=线程1.contnuewith(Dosecond)//连续任务!!!!
//=========================================================
            TaskFactory tf1 = new TaskFactory();//任务工厂!!!
            Task xc4 = tf1.StartNew(qd2);
 //           lock (对象)//申请加锁
 //          {
 //               对象.方法体
 //           }//释放

11.Socket:

11.1.TCP

//服务端:
 //1.这个类会创建socket对象
            TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 9999);
            //2.开始监听
            listener.Start();
            Console.WriteLine("等待...1:" );
            //3.等待客户连接??集合<>?
            TcpClient client = listener.AcceptTcpClient();//阻塞式
             Console.WriteLine("等待...2:"+client.Connected); 
            //4.获取客户端发送过来的数据
            NetworkStream stream= client.GetStream();//得到了一个网络流,从这个网络流里的到客户端发送过来的数据.Read()
//客户端:
  //当对象建立时就会跟server建立连接
            TcpClient client = new TcpClient("127.0.0.1", 9999);//阻塞式
            Console.WriteLine("等待...1");
            //通过网络流进行数据交换
            NetworkStream stream = client.GetStream();
            //写入数据就是发送数据.Write();
//发送与接收
         string message = Console.ReadLine();//阻塞式
         byte[] data1 = Encoding.UTF8.GetBytes(message);//string转字节数组
         stream.Write(data1, 0, data1.Length);
//=================================================================
         byte[] data2 = new byte[1024];
         int length= stream.Read(data2,0,data2.Length);//返回字节长度
         string message2 =Encoding.UTF8.GetString(data2, 0, length)//字节数组转string
//关闭:
 stream.Close();
 client.Close();

11.2.UDP:

//服务端
           //创建udpclient绑定跟端口号
            UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999));
            //接受数据
            IPEndPoint point = new IPEndPoint(IPAddress.Any,0);
            while (true)
            {
                //通过point确定数据来自哪个ip端口号,返回一个字节数组
                byte[] data = udpClient.Receive(ref point);//阻塞式
                //转换数据string
                string message = Encoding.UTF8.GetString(data);
                Console.WriteLine(message);
            }
            udpClient.Close();//释放资源
//客户端:
            //创建udpclient对象
            UdpClient client = new UdpClient();
            while (true)
            {
                string message = Console.ReadLine();//阻塞式
                byte[] data = Encoding.UTF8.GetBytes(message);
                client.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999));
            }
            client.Close();//释放资源

12.文件:

 FileInfo Fio = new FileInfo(@"D:\1\5\新建文本文档.txt");//文件操作
 DirectoryInfo DTio = new DirectoryInfo(@"D:\1\5");//目录操作
File.WriteAllText(@"D:\1\5\新建文本文档1.txt","你好  233 ");//创建新文件
string[] f001 = File.ReadAllLines(@"D:\1\5\新建文本文档1.txt");
string f002 = File.ReadAllText(@"D:\1\5\新建文本文档1.txt");
byte[] f003 = File.ReadAllBytes(@"D:\1\5\新建文本文档1.txt");    
//========================================================================================   
FileStream stream = new FileStream(@"D:\1\5\新建文本文档2.txt",FileMode.Open);//创建文件流 用来操作文件
stream.Close();//关闭文件流!!!
//========================================================================================
 //创建文本文件读取流
StreamReader reader = new StreamReader(@"D:\1\5\新建文本文档2.txt");
reader.Close();

 //文本文件写入流
StreamWriter writer = new StreamWriter(@"D:\1\5\新建文本文档3.txt");
writer.Close();
13反射:
   Class1 my = new Class1();//一个类中的数据是存储在对象中的,但是type对象只存储类的成员

            Type type = my.GetType();//通过对象获取这个对象所属类的type对象
            Console.WriteLine(type.Name);//获取类的名字
            Console.WriteLine(type.Namespace);//获取类的命名空间
            Console.WriteLine(type.Assembly);//获取类的程序集

            FieldInfo[]array= type.GetFields();//字段
            foreach (var info in array) 
            {
                Console.WriteLine(info.Name+" "); 
            }

            PropertyInfo[] array2 = type.GetProperties();//属性
            foreach (var info in array2)
            {
                Console.WriteLine(info.Name);
            }

            MethodInfo[] array3 = type.GetMethods();//方法
            foreach (var info in array3)
            {
                Console.WriteLine(info.Name);
            }
            //通过type对象可以获取它对应的类的所有成员(public)
 Assembly assem = my.GetType().Assembly;//通过类的type对象获取它所在的程序集Assembly
            Console.WriteLine(assem.FullName);
          
           Type[] types= assem.GetTypes();
           foreach (var type1 in types)
           {
               Console.WriteLine(type1);
           }


猜你喜欢

转载自blog.csdn.net/qq_40346899/article/details/80694711
今日推荐