我有一个非常大的char数组,我需要将其转换为字符串才能在其上使用Regex.
但是OutOfMemoryException当我将它传递给字符串构造函数时,它是如此之大.
我知道字符串是不可变的,因此不应该指定它的底层字符集,但我需要一种方法来使用正则表达式而不复制整个事物.
我如何获得该阵列?
StreamReader.我知道要读取的内容的起始位置和长度,Read并且ReadBlock方法需要我提供char[]缓冲区.所以这是我想知道的事情:
我发现[...] Async和Begin [...] .net异步API之间存在差异,但这个答案让我有点困惑.
谈到这些模式,斯蒂芬说:
大多数*异步方法(具有相应的*已完成事件)正在使用基于事件的异步模式.较旧(但仍然完全有效)的Begin*和End*是一种称为异步编程模型的模式.
Socket类是此规则的一个例外; 它的*异步方法没有任何相应的事件; 它本质上只是APM以避免过多内存分配的方式完成的.
我得到它使用*Async方法更有效,至少在套接字方面.但后来他提到任务并行库:
但是,APM和EBAP都被基于任务并行库的更灵活的方法所取代.由于TPL可以轻松地包装APM,因此较旧的类可能不会直接更新; 扩展方法用于为旧的APM方法提供Task等价物.
我在MSDN上发现了TPL和传统.NET异步编程,我知道TPL的基础知识,创建任务,取消,延续等等,但我仍然无法理解这些:
异步编程模型(APM)和基于事件的异步模式(EAP)相互比较的优势是什么?TPL如何轻松地包装 APM意味着APM和EAP都被 TPL 取代?
最重要的是:我应该在套接字编程中使用哪个;
c# sockets design-patterns asynchronous task-parallel-library
我可以为非泛型接口设置嵌套合同类型:
[ContractClass(typeof(Foo.FooContracts))]
public interface IFoo
{
string Bar(object obj);
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用通用接口做同样的事情时,它会抱怨:
[ContractClass(typeof(Foo.FooContracts<>))]
public interface IFoo<T>
{
string Bar(T obj);
}
Run Code Online (Sandbox Code Playgroud)
警告是:
合同类
Foo+FooContracts`1和类型IFoo`1必须具有相同的声明类型(如果有).
如果我FooContracts离开Foo课堂,它会在没有警告的情况下编译.
我有一个并发的集合.
var q = new ConcurrentQueue<int>(); // Or a stack/bag/etc...
Run Code Online (Sandbox Code Playgroud)
我可以买到这样的物品.
int i;
if (q.TryDequeue(out i))
DoStuff(i);
Run Code Online (Sandbox Code Playgroud)
但是我需要它像"在有人添加新项目之前等待10秒再返回假":
int i;
if (q.TryDequeue(TimeSpan.FromSeconds(10), out i))
DoStuff(i);
Run Code Online (Sandbox Code Playgroud)
我可以写一个像这样的扩展方法:
public static bool TryDequeue<T>(
this ConcurrentQueue<T> queue,
TimeSpan timeout,
out T result)
{
// Validations...
for (int i = 0; i < 2; i++)
{
if (queue.TryDequeue(out result))
return true;
else if (i == 1)
return false;
Thread.Sleep(timeout);
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果有人在5秒钟内将一个项目添加到队列中,我不想再等5个.
是否有任何线程安全的集合在.NET中支持此功能?
public readonly struct Foo<T>
{
// ...
public T Value { get; }
}
public static Foo<string?> Bar(Foo<string?> foo)
{
if (foo.Value is null) { /* Something */ }
else { /* Some other thing */ }
return foo;
}
public static void Main()
{
var foo1 = new Foo<string>("s");
var ret1 = Bar(foo1); // Warns because foo1's type parameter is not nullable
var foo2 = new Foo<string?>("s");
var ret2 = Bar(foo2); // Ok because foo2's type parameter is nullable …Run Code Online (Sandbox Code Playgroud) 我有一个可变类,里面有一个私有List<T>字段.在Reset()我的类的方法中,我应该使用它的Clear()方法清除列表还是只为其字段分配一个新列表?请注意,该列表不是公共的,仅供类本身使用.因此,分配新列表应该使旧的列表无法访问.由于该Clear()方法是O(n)操作,我想知道在它上面分配新列表的缺点是什么.
我需要使用C#替换字符串的某些部分.我只能找到一个关于如何在这里实现这个问题的类似问题,但它是PHP.
我的情况涉及一个Dictionary [string,string],它包含要替换的对,如:
我有一个值为的字符串:
"My dog ate a cat which once ate a mouse got eaten by a raptor"
Run Code Online (Sandbox Code Playgroud)
我需要一个函数来得到这个:
"My cat ate a mouse which once ate a raptor got eaten by a raptor"
Run Code Online (Sandbox Code Playgroud)
如果我枚举字典并按顺序调用string.Replace,我得到这个:
"My raptor ate a raptor which once ate a raptor got eaten by a raptor"
Run Code Online (Sandbox Code Playgroud)
如果以前没有问到这一点很奇怪,(这是常识吗?)但我找不到任何东西.所以我很抱歉,如果有,我错过了.
我有两位小数:
var first = 1.567m;
var second = 1.568m;
var areEqual = first == second; // false
Run Code Online (Sandbox Code Playgroud)
我有一个可接受的误差范围; 是的0.010.
所以我需要的是一个Check方法:
Check(first: 1.567m, second: 1.577m, margin: 0.010m); // true
Check(first: 1.567m, second: 1.578m, margin: 0.010m); // false
Check(first: 1.567m, second: 1.578m, margin: 0.011m); // true
Run Code Online (Sandbox Code Playgroud)
我该怎么写呢?
我在刷新相关的实体集合时遇到了一些麻烦.
基本上问题如下:
public class Student
{
public virtual ICollection<Lecture> Lectures { get; set; }
public void AddLecture(Lecture lecture)
{
Lectures.Add(lecture);
}
public void CancelChanges()
{
_context.Refresh(RefreshMode.StoreWins, this);
_context.LoadProperty(this, (o) => o.Lectures,
MergeOption.OverwriteChanges);
}
}
public class Grade
{
public virtual Student { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我有一些用于添加讲座的GUI,如果我们想要,我们可以取消编辑过程:
public void ExampleEdit()
{
Student student = _context.Students.SingleOrDefault(/* blah */);
student.AddLecture(_context.Lectures.SingleOrDefault(/* e.g. math */));
student.CancelChanges();
// At this point student SHOULD have no lectures anymore since the
// property was loaded with overwrite …Run Code Online (Sandbox Code Playgroud) 我的类有一个Task<int>使用a 生成对象的方法TaskCompletionSource<int>.然后它执行一些异步操作并设置结果.我需要知道我是否可以信任任务的ContinueWith方法:
public Task<int> CalculateAsync()
{
var source = new TaskCompletionSource();
DoSomeAsyncStuffAndSetResultOf(source);
source.Task.ContinueWith(t => Console.WriteLine("Is this reliable?"));
return source.Task;
}
Run Code Online (Sandbox Code Playgroud)
CalculateAsync方法的调用者可以对生成的任务执行某些操作并阻止其继续运行吗?