我需要一个线程安全的集合来保存没有重复的项目.ConcurrentBag<T>允许非唯一项,HashSet<T>并且不是线程安全的.在.NET Framework 4.5中是否有这样的集合?
我正在寻找一个可以从多个线程访问的只读字典.虽然ConcurrentDictionary暴露了这样的功能,但我不希望有开销和奇怪的API.
.Net 4.5虽然提供了这样的类,但文档指出只有静态调用才是安全的.
我想知道为什么?
我有.txt文件(包含超过百万行),大约1GB,我有一个字符串列表,我试图删除字符串列表中存在的文件中的所有行并创建新文件,但它正在采取很长时间.
using (StreamReader reader = new StreamReader(_inputFileName))
{
using (StreamWriter writer = new StreamWriter(_outputFileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (!_lstLineToRemove.Contains(line))
writer.WriteLine(line);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何提高代码的性能?
c# ×2
concurrency ×2
.net ×1
.net-4.5 ×1
collections ×1
dictionary ×1
file ×1
immutability ×1
performance ×1