C#拓展学习

1.委托

//委托的定义
delegate int Weituo(int x); 
//声明委托事件
public event Weituo weituoEvent;
//参数和返回值规定了实现函数的参数和返回值
//使用委托
Weituo = (arg1){return arg1;}
//
int zhixing(int x)
{
    return x;
}
weituoEvent += x;
//触发事件
weituoEvent(2);

2.Lambda表达式

(arg1,arg2..)=>{}

3.C#初始化对象

public class text1
{
    int X{get;set};
    int Y{get;set};
}
public class text2
{
    int Y{get;set};
    int Z{get;set};
}
int main()
{
    var textList1 = new List<text1>()
    {
           new text(){X=100;Y=200},
           new text(){X=200;Y=400} 
    }
    var textList2 = new List<text2>()
    {
           new text(){Z=100;Y=200},
           new text(){Z=200;Y=400} 
    }
}

4.LINQ(using system.Linq)

  • 普通查询
var res = from m in textList1 where X>200 select m;
  • 联合查询
var res = from m1 in textList1 from m2 in textList2 where m1.Y==m2.Y orderby m1.x select new{text1 = m1,text2 = m2};
  • 分组
var res = from m in textList1 where X>200 group m by x into g select g.count();
  • 量词操作符any all
bool res = textList1.Any(m=>m.X=="");
bool res = textList1.All(m=>m.X=="");

5.反射与特性

  • 反射:一个运行的程序查看本身的元数据或者其他程序集的元数据的行为叫做反射
  • type对象
MyClass my = new MyClass();
Type type = my.GetType(); //获得type对象
type.Name; //获取类的名字
type.Namespace; //获取所在的命名空间
Assembly assem = type.Assembly;
//获取所在的程序集
assem.FullName; 获得程序集名字
Type[] types = assem.GetTypes();
//获得程序集下的所有类的type
FieldInfo[] array = type.GetFields();//获得类中有哪些字段(只能获取公有字段)
arrary.Name; // 字段名字
PropertyInfo[] array2 = type.GetProperties();//获得类中的属性
array2.Name; //属性名字
MethodInfo[] array3 = type.GetMethods();//获得类中的属性
array3.Name; //属性名字
  • Obsolete特性
[Obsolete("这个方法已过时,使用NewMethod代替"[,true])] //表示该方法被弃用
static void oldMethod(){}
  • Conditional特性
#define anything 
[Conditional("anything")] //如果没有宏定义,将调用该函数的语句全部注释掉
static void oldMethod(){}
  • DebuggerStepThrough特性
[DebuggerStepThrough("anything")] //可以跳过debug的调试,不让单步调试
static void oldMethod(){}

6.线程

//执行函数
int Test(int x,string y)
{
    Console.WriteLine(y);
    return x+10;
}
//回调函数
static void CallBack(IAsyncResult ar)
{
    Func<int,string,int> a = ar.AsyncState as Func<int,string,int>;
    int res = a.EndInvoke(ar); //res为执行函数的返回值
    Console.WriteLine("线程结束");
}
//线程池函数
void Test(object state)
{
    Console.WriteLine(y);
}
//自定义thread类
class MyThread
{
    int x;
    string y;
    void Text()
    {
           Console.WriteLine(y);
    return x+10; 
    }
}
static void Main(string[] args)
{
    //1.异步委托开启线程
    Func<int,int> a = Test;
    //第三个参数是回调函数,第四个函数是回调函数中使用的参数
    IAsyncResult ar= a.BeginInvoke(100,"hellow",CallBack,a);
    //直接使用lambda表达式获得回调
    a.BeginInvoke(100,"hellow",ar=>{
        int res = a.EndInvoke(ar); //res为执行函数的返回值
        Console.WriteLine("线程结束");
    },null);
    ar.IsCompleted; //判断线程是否执行完毕
    //检测线程线程结束
    bool isEnd = as.AsyncWaitHandle.WaitOne(1000); //1000ms表示超时时间
    if(isEnd)
    {
        a.EndInvoke(ar);
    }
    
    //2.通过Thread类开启线程
    //1.一般函数
    Thread t =new Thread(Test);
    t.start(100,"hello");
    //2.lambda表达式
    Thread t =new Thread(()=> {});
    t.start();
    t.Join();
    //线程Id
    Thread.CurrentThread.ManagedThreadId;
    
    
    //3.自定义Thread类来开启线程
    Mythread my = new MyThread(100,"hello");
    Thread t = new Thread(my.Test);
    t.Join();
    
    //4.通过线程池开启线程
    ThreadPool.QueueUserWorkItem(test);
    
    //5.通过任务开启线程
    Task t = new Task(Test);
    t.Start();
}
  • 前台线程和后台线程:在Main函数执行完后,前台线程会继续运行,后台线程会被直接干掉
Thread t = new Thread(test);
t.IsBackGround = true; //设置线程为后台线程
  • 线程之争用和死锁
private object o = new object();
lock(o)
{
    
}

7.socket编程

  • IP和端口号
    IP识别计算机,端口号识别软件
  • 服务器连接步骤
//1.创建socket(传输网络,传输方式,传输协议)
Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//2.绑定ip和端口号
IPAddress ipadress = new IPAddress(new byte[]{192,168,0,112});
EndPoint point = new IPEndPoint(ipadress,7788)
tcpServer.Bind(point);
//3.开始监听(等待客户端连接)
tcpServer.Listen(100);//参数是最大连接数
//4.同步连接(暂停该线程,直到有一个客户端连接过来,再继续下面的代码)
Socket clientSocket = tcpServer.Accept();
//接收客户端的消息
byte[] data = new byte[1024];
int length = tcpServer.Receive(data);
string message = Encoding.UT8.GetString(data,0,length);
//发送消息给客户端
string message = "hello";
//对字符串编码得到utf-8编码的字节流
data = Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
  • 客户端连接步骤
//1.创建socket(传输网络,传输方式,传输协议)
Socket tcpClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//2.发起建立连接的请求
IPAddress ipadresss = new IPAddress(new byte[]{192,168,0,112});
tcpClient.Connect(ipaddress,7788);
//接收服务器的消息
byte[] data = new byte[1024];
int length = tcpClient.Receive(data);
string message = Encoding.UT8.GetString(data,0,length);
//像服务器端发送消息
string message = Console.ReadLine();
//对字符串编码得到utf-8编码的字节流
data = Encoding.UTF8.GetBytes(message);
clientClient.Send(data);
发布了33 篇原创文章 · 获赞 3 · 访问量 629

猜你喜欢

转载自blog.csdn.net/qq_43647628/article/details/104345836