在Java中,我可以使用http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html#remove()Iterator.remove()方法迭代集合并从中删除一些对象.
这非常自然而且很自然.就像你正在寻找冰箱里的食物,扔掉过期日期的食物.如何在c#中做同样的事情?
我希望iterate和clean-up收集在同一个循环中.
有几个相关的问题,例如如何迭代和更新列表但是我仍然无法在c#中找到此操作的完全重复
我有这个代码:
if (typeof(Enum).IsAssignableFrom(typeof(T)))
{
try
{
return (T)Enum.Parse(typeof(T), text);
}
catch (ArgumentException e)
{
return default(T);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了以下例外:
mscorlib.dll中出现'System.ArgumentException'类型的第一次机会异常附加信息:未找到请求值'ABC'
怎么可能?为什么catch块不工作?
我知道我不应该优化我的程序的每一个点,让我们假设我必须优化数组初始化.
所以我写的程序,比较for loop与Array.Clear
using System;
using System.Diagnostics;
namespace TestArraysClear
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[100000];
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 10; i++)
{
sw.Reset();
sw.Start();
for (int j = 0; j < a.Length; j++)
{
a[j] = 0;
}
sw.Stop();
Console.WriteLine("for " + sw.ElapsedTicks);
sw.Reset();
sw.Start();
Array.Clear(a, 0, a.Length);
sw.Stop();
Console.WriteLine("Array.Clear " + sw.ElapsedTicks);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我机器上的输出:
for 1166
Array.Clear 80 …Run Code Online (Sandbox Code Playgroud) 我有一个项目和一个类库.
我需要类库来更新存储项.在我的项目中,我需要访问这些存储项目.我可以lock在不同项目的同一个实例上使用吗?
BlockingCollection仅包含添加单个项目的方法.如果我想添加一个集合怎么办?我应该只使用"foreach"循环吗?
为什么BlockingCollection不包含添加集合的方法?我认为这种方法非常有用.
我需要将格式为"HHmmss"的字符串快速转换为DateTime或整数.我测试了这样的代码:
Console.WriteLine("decoding " + text);
long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("start time " + microseconds);
field = DateTime.ParseExact(text, "HHmmss", null);
microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("finish time " + microseconds);
Run Code Online (Sandbox Code Playgroud)
而输出是
decoding 172400 start time 121 finish time 244 decoding 172400 start time 236 finish time 383 decoding 172400 start time 116 finish time 416 decoding 172400 start time 235 finish time 421 decoding 172359 start time 149 finish time 323 …
foreach循环Queue从最旧的项开始迭代,最后以最新项结束.如果我需要从最新开始并以最旧的结尾(可能在某些时候中断,因为在大多数情况下我只需要迭代几个最新的项目),该怎么办?
我正在寻找直接有效的解决方案.无需重新创建新对象.
在审查一个应用程序的代码时,我发现它假定顺序与Dictionary.Values添加到集合中的元素相同.
我写过应用程序来测试这是否正确:
using System;
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> values = new Dictionary<string, int>();
values.Add("apple2", 2);
values.Add("apple3", 3);
values.Add("apple4", 4);
values.Add("apple5", 5);
values.Add("apple6", 6);
values.Add("apple1", 1);
var list = new List<int>(values.Values);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
2
3
4
5
6
1
Run Code Online (Sandbox Code Playgroud)
首先,我想知道这是怎么可能的.是不是字典应该使用无序树或类似的东西?
此外,MSDN声明:
Dictionary<TKey, TValue>.ValueCollection未指定值的顺序,但它Dictionary<TKey, TValue>.KeyCollection与Keys属性返回的关联键的顺序相同.
那么为什么MSDN告诉"订单未指定"但实施恰好保持秩序?我是否正确,我最好不要依赖这个事实?
我正在使用boost::thread join方法等待线程完成.但是如果我join在线程已经完成时尝试该线程,我会收到异常.那么我怎么能:
加入线程如果激活,如果不活动则不执行任何操作
让我们考虑一下这样的应用:
void foo (char* const constPointerToChar) {
// compile-time error: you cannot assign to a variable that is const
constPointerToChar = "foo";
}
int _tmain(int argc, _TCHAR* argv[])
{
char* str = "hello";
foo(str);
printf(str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们删除const关键字:
void foo (char* pointerToChar) {
pointerToChar = "foo";
}
int _tmain(int argc, _TCHAR* argv[])
{
char* str = "hello";
foo(str);
printf(str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是hello.因此,即使允许函数更改指针,它也会更改指针的副本,并且不会更改原始指针.
这是预期的,因为指针是按值传递的.
我明白为什么事情会这样,但我不明白为什么有人需要声明参数为X* const.
当我们声明函数参数时,X* const我们说"好吧,我在我的函数中保证,我不会修改我自己的指针副本." 但是为什么调用者应该关心他从未见过和使用的变量会发生什么?
我是否正确声明函数参数X* …