.Net6.0系列-5.Net 6LinQ(一)揭秘及优势

25 篇文章 0 订阅
8 篇文章 0 订阅

一.LinQ让数据处理变的简单,但是执行效率不一定最高, 本质就是委托
1.委托是可以指向方法的类型
2.Net中定义了泛型委托Action(无返回值)和Func(有返回值),所以一般不用自定义委托类型
3.c#中的var是强类型的

//委托举例
static void main(string[] args)
{
 D1 d1=FSum; 
 Console.WriteLine(d1(1,2));
 //Action使用举例:无参数
 Action A1=F1;
 A1();
 //Action使用举例:传入两个参数,但是无返回值
 Action A2=ADelegate;
 A2();
 //Func使用举例:2个参数,一个返回值参数
 Func<int,int,int> f=FSum;
 Console.WriteLine(f(5,2));
 //Func使用举例:2个int参数,但是返回string类型
 Func<int,int,string> fStr=FStr;
 Console.WriteLine(fstr(1,2));
}
public static void F1()
{
	Console.WriteLine("Hello World!");
}
public static int FSum(int a,int b)
{	
	return a-b;
}
public static string FStr(int a,int b)
{
	int c=a+b;
	return "返回字符串";
}
public static void ADelegate(int a,int b)
{
	int c=a+b;
}
delegate int D1(int a,int b);

二:匿名委托的使用,这里主要是一步一步的从委托类型的写法,转换为Lamada表达式的写法

static void main(string[] args)
{
	//匿名无参委托
	Action a1=delegate(){
		Console.WriteLine("我是aaa");
	};
	a1();
	//有参匿名委托
	Action<string,int> a2=delegate(string str,int i)
	{
		Console.WriteLine($"str={str},i={i}");
	};
	a2("Hello,Word!",0);
	//有参数的匿名委托
	Func<int,int,int> FReturn=delegate(int i,int j)
	{
		return i+j;
	};
	Console.WriteLine(FReturn(1,0));
	//省略delegate的写法,变成了lamada表达式的写法
	Func<int,int,int> Flamada=(int i,int j)=>{
		return i+j;
	};
	Console.WriteLine(Flamada(0,1));
	//去掉参数类型的写法
	Func<int,int,int> FlamadaNoParameterType=(i,j)=>{
		return i+j;	
   	};
   	Console.writeLine(FlamadaNoParameterType(0,1));
   	//去掉大括号的,及return的写法,这种只适用于一行代码的写法
   	Action a1=()=>Console.WriteLine("我是aaa");  	
	Console.WriteLine(a1);
   	Func<int,int,int> FlamadaNoParameterNoReturn=(i,j)=>i+j;
   	Console.WriteLine(FlamadaNoParameterNoReturn(0,1));

	//终极演化版(只有一行代码且只有一个参数)
	Action<int> ALast=t=>Console.WriteLine(t);
	Func<int,bool> FLast=t=>t>0;
}

三 .解密Linq

static void Main(string[] args)
{
	//使用系统where扩展方法获取数组中的大于3的集合
 	int[] nums=new int[] {1,2,3,4,5,6,7};
 	IEnumerable<int> Rel=nums.where(t=>t>3);
 	//使用自己写的demo获取集合
 	IEnumerable<int> MyWhereRel=WhereDemo(nums,t=>t>3);
 	//Yield方法,无需等待全部遍历完成,判断完成一个数据就输出一个数据
 	IEnumerable<int> MyWhereRel=WhereDemoYield(nums,t=>t>3);
 	foreach(int i in MyWhereRel)
 	{
		Console.WriteLine(i);
    }
}

static IEnumerable<int> WhereDemo(IEnumberable<int> items,Func<int,bool> f)
{
	List<int> res=new List<int>();
	foreach(int i in items)
	{
		if(f(i)==true)
		{
			res.Add(i);
		}
	}
	return res;
}
static IEnumerable<int> WhereDemoYield(IEnumberable<int> items,Func<int,bool> f)
{
	foreach(int i in items)
	{
		if(f(i)==true)
		{
			yield return i;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要集成海康相机SDK .NET 6.0,您可以按照以下步骤进行操作: 1. 下载并安装海康相机SDK .NET 6.0。 2. 打开 Visual Studio,创建一个新的 C# 项目。 3. 在项目中添加对海康相机SDK .NET 6.0的引用。 4. 在代码中实例化海康相机SDK,并进行初始化。 5. 配置相机参数,如分辨率、曝光时间、帧率等。 6. 开始捕获图像并处理图像数据。 这里提供一份简单的示例代码,供您参考: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using HCNetSDK; namespace HkCameraDemo { class Program { static void Main(string[] args) { // 初始化SDK bool initResult = HCNetSDK.NET_DVR_Init(); if (!initResult) { Console.WriteLine("SDK初始化失败!"); return; } // 登录设备 NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30(); int userId = HCNetSDK.NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "password", ref deviceInfo); if (userId == -1) { Console.WriteLine("登录设备失败!"); HCNetSDK.NET_DVR_Cleanup(); return; } // 设置预览参数 NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO(); previewInfo.lChannel = 1; previewInfo.dwStreamType = 0; previewInfo.dwLinkMode = 0; previewInfo.hPlayWnd = IntPtr.Zero; // 开始预览 int realHandle = HCNetSDK.NET_DVR_RealPlay_V30(userId, ref previewInfo, null, IntPtr.Zero, true); if (realHandle == -1) { Console.WriteLine("预览失败!"); HCNetSDK.NET_DVR_Logout(userId); HCNetSDK.NET_DVR_Cleanup(); return; } // 循环获取图像数据 while (true) { // 获取图像数据 IntPtr buffer = IntPtr.Zero; uint size = 0; HCNetSDK.NET_DVR_GetRealPlayerIndex(realHandle, ref buffer, ref size); // 处理图像数据 // TODO: 在此处添加图像处理代码 // 释放内存 HCNetSDK.NET_DVR_FreeGMemory(buffer); } // 停止预览 HCNetSDK.NET_DVR_StopRealPlay(realHandle); // 注销登录 HCNetSDK.NET_DVR_Logout(userId); // 释放SDK资源 HCNetSDK.NET_DVR_Cleanup(); } } } ``` 注意:以上示例代码仅供参考,请根据实际情况进行修改和优化。另外,为了保证代码的可读性和可维护性,建议在代码中添加必要的注释和异常处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值