在C#中,我试图通过随机索引从列表中获取项目.检索完后,我希望将其删除,以便不再选择它.好像我需要很多操作才能做到这一点,是不是我可以从列表中简单地提取项目?RemoveAt(index)函数为void.我想要一个具有返回值的人.
我在做什么:
List<int> numLst = new List<int>();
numLst.Add(1);
numLst.Add(2);
do
{
int index = rand.Next(numLst.Count);
int extracted = numLst[index];
// do something with extracted value...
numLst.removeAt(index);
}
while(numLst.Count > 0);
Run Code Online (Sandbox Code Playgroud)
我想做什么:
List<int> numLst = new List<int>();
numLst.Add(1);
numLst.Add(2);
do
{
int extracted = numLst.removeAndGetItem(rand.Next(numLst.Count));
// do something with this value...
}
while(numLst.Count > 0);
Run Code Online (Sandbox Code Playgroud)
这样的"removeAndGetItem"函数是否存在?
Roa*_*ich 16
不,因为它违反了纯函数礼仪,其中一个方法要么具有副作用,要么返回一个有用的值(即不只是指示错误状态) - 从不两者兼而有之.
如果希望函数显示为原子,则可以获取列表上的锁定,这将阻止其他线程在修改时访问列表:
public static class Extensions
{
public static T RemoveAndGet<T>(this IList<T> list, int index)
{
lock(list)
{
T value = list[index];
list.RemoveAt(index);
return value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
public static class ListExtensions
{
public static T RemoveAndGetItem<T>(this IList<T> list, int iIndexToRemove}
{
var item = list[iIndexToRemove];
list.RemoveAt(iIndexToRemove);
return item;
}
}
Run Code Online (Sandbox Code Playgroud)
这些被称为扩展方法,称为new List<T>().RemoveAndGetItem(0).
扩展方法中要考虑的事项
使用您传递的索引进行异常处理,在执行此操作之前检查索引是否为0以及列表的计数.
| 归档时间: |
|
| 查看次数: |
16421 次 |
| 最近记录: |