[英]How to make Take() and Skip() modular?
I have a struct like this: 我有这样的结构:
public struct Response
{
public string Cmd;
}
And also in main I'm having this byte array: 而且主要是我有这个字节数组:
Decode(StringToByteArray("0100002402433131000000000000000311"));
And then I have a decode function inside this function I want to do Take(5) and Skip (3) like this: 然后我在这个函数里面有一个解码函数我想做Take(5)和Skip(3)这样:
byte[] cmd = resp.Skip(5).Take(3).ToArray();
x.Cmd = Encoding.UTF8.GetString(cmd);
How can i make this Modular as i need to the same to many function that might be the position is different is there anyway that instead of using Take(3) or Skip (5) i can assign Variable to calculate this automatically? 我如何制作这个模块,因为我需要相同的许多功能,可能是位置不同,无论如何,而不是使用Take(3)或Skip(5)我可以分配变量来自动计算?
Here's your other function: 这是你的另一个功能:
public T[] SkipTake<T>(IEnumerable<T> items, int skipCount, int takeCount) {
return items.Skip(skipCount).Take(takeCount).ToArray();
}
It uses generics, so I'd read up on those (http://msdn.microsoft.com/en-us/library/512aeb7t(v=vs.90).aspx). 它使用泛型,所以我会阅读这些(http://msdn.microsoft.com/en-us/library/512aeb7t(v=vs.90).aspx)。
Personally ild make it an extention method, (same idea as @siride but a bit more fluent) 个人ild使它成为一种扩展方法,(与@siride相同,但更流畅一点)
eg 例如
public static class SkipTakeExtentions{
public static T[] SkipTake<T>(this IEnumerable<T> items, int skipCount, int takeCount) {
return items.Skip(skipCount).Take(takeCount).ToArray();
}
}
usage: 用法:
thing.SkipTake(1,2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.