文章目录
-
- 1.委托
- 2.Lambda表达式
- 3.C#初始化对象
- 4.LINQ(using system.Linq)
- 5.反射与特性
- 6.线程
- 7.socket编程
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();
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.Name;
type.Namespace;
Assembly assem = type.Assembly;
assem.FullName; 获得程序集名字
Type[] types = assem.GetTypes();
FieldInfo[] array = type.GetFields();
arrary.Name;
PropertyInfo[] array2 = type.GetProperties();
array2.Name;
MethodInfo[] array3 = type.GetMethods();
array3.Name;
[Obsolete("这个方法已过时,使用NewMethod代替"[,true])]
static void oldMethod(){}
#define anything
[Conditional("anything")]
static void oldMethod(){}
[DebuggerStepThrough("anything")]
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);
Console.WriteLine("线程结束");
}
void Test(object state)
{
Console.WriteLine(y);
}
class MyThread
{
int x;
string y;
void Text()
{
Console.WriteLine(y);
return x+10;
}
}
static void Main(string[] args)
{
Func<int,int> a = Test;
IAsyncResult ar= a.BeginInvoke(100,"hellow",CallBack,a);
a.BeginInvoke(100,"hellow",ar=>{
int res = a.EndInvoke(ar);
Console.WriteLine("线程结束");
},null);
ar.IsCompleted;
bool isEnd = as.AsyncWaitHandle.WaitOne(1000);
if(isEnd)
{
a.EndInvoke(ar);
}
Thread t =new Thread(Test);
t.start(100,"hello");
Thread t =new Thread(()=> {});
t.start();
t.Join();
Thread.CurrentThread.ManagedThreadId;
Mythread my = new MyThread(100,"hello");
Thread t = new Thread(my.Test);
t.Join();
ThreadPool.QueueUserWorkItem(test);
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识别计算机,端口号识别软件
- 服务器连接步骤
Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress ipadress = new IPAddress(new byte[]{192,168,0,112});
EndPoint point = new IPEndPoint(ipadress,7788)
tcpServer.Bind(point);
tcpServer.Listen(100);
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";
data = Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
Socket tcpClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
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();
data = Encoding.UTF8.GetBytes(message);
clientClient.Send(data);