模式匹配“不是”的优先顺序是什么?我意识到我写了一些这样的代码:
if (x is not TypeA or TypeB)
Run Code Online (Sandbox Code Playgroud)
并隐含地假设我正在写:
if (!(x is TypeA) && !(x is TypeB))
Run Code Online (Sandbox Code Playgroud)
但我刚刚意识到它可能被评估为:
if ((!x is TypeA) || (x is TypeB))
Run Code Online (Sandbox Code Playgroud)
换句话说,“不”是否适用于“或分隔”列表,或者仅适用于列表中的下一个参数。我原来的声明是否需要写成这样:
if (x is not TypeA and not TypeB)
Run Code Online (Sandbox Code Playgroud) 我有一组接口和类看起来像这样:
public interface IItem
{
// interface members
}
public class Item<T> : IItem
{
// class members, and IItem implementation
}
public interface IItemCollection : IEnumerable<IItem>
{
// This should be enumerable over all the IItems
}
// We cannot implement both IItemCollection and IEnumerable<TItem> at
// the same time, so we need a go between class to implement the
// IEnumerable<IItem> interface explicitly:
public abstract class ItemCollectionBase : IItemCollection
{
protected abstract IEnumerator<IItem> GetItems();
IEnumerator<IItem> IEnumerable<IItem>.GetEnumerator() { return …Run Code Online (Sandbox Code Playgroud) 在 Visual Studio 2019 中,当您点击分号时,它喜欢尝试猜测应该将分号放在哪里。通常它会跳转到完全不同的行或插入换行符。每当它执行除了将其附加为下一个字符之外的任何操作时,这都是极具破坏性的,我必须返回并修复它所破坏的内容。这类似于破坏性的“自动大括号完成”,它总是将大括号放在我不想要的地方,但可以关闭。我找不到任何地方可以关闭分号行为。有什么办法可以关闭这个功能吗?
大多数时候,分号出现问题是因为我误按了它,但我现在没有按退格键,而是有更大的混乱需要清理。我从来没有遇到过它做了我想要它做的额外事情的情况。
一些示例,其中 * 是光标位置:
// between a ) and ;
Foo()*;
// inserts an extra space
Foo();* ;
// before an existing semicolon:
return null*;
// moves the old semicolon to the next line:
return null;*
;
// pressing semicolon in the middle of a multi-line function call:
string path = Path.Combine(
"C:\\",
"b"*
"filename.txt");
// moves the cursor to the end of the block and inserts a "; " before ";":
string path = …Run Code Online (Sandbox Code Playgroud) 我正在执行一些计算,如果运行时间太长,我需要超时。我可能会设置 5 秒的超时,并在我的代码中定期进行轮询。实际代码要复杂得多,并且具有大量递归,并且分布在许多类中,但这应该给出它如何工作的一般概念。基本上,任何时候调用递归或执行可能需要时间的操作时,它都会调用 AssertTimeout()。
private DateTime endTime;
public void PerpareTimeout(int timeoutMilliseconds)
{
endTime = DateTime.UtcNow.AddMilliseconds(timeoutMilliseconds);
}
public void AssertTimeout()
{
if (DateTime.UtcNow > endTime)
throw new TimeoutException();
}
public void DoWork1()
{
foreach (var item in workItems)
{
AssertTimeout();
DoWork2(item)
}
}
public void DoWork2(WorkItem item)
{
foreach (var item in workItems)
{
AssertTimeout();
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我连接了调试器并暂停执行时,问题就出现了。我想以某种方式禁用暂停时间的超时。因此,如果它运行了 2 秒,我命中断点并等待 5 分钟,然后恢复,恢复执行后它将再运行 3 秒,然后超时。
我可以使用这样的东西:
public void PrepareTimeout(int timeoutMilliseconds)
{
if (System.Diagnostics.Debugger.IsDebuggerAttached)
endTime = DateTime.MaxValue;
else
endTime = DateTime.UtcNow.AddMilliseconds(timeoutMilliseconds); …Run Code Online (Sandbox Code Playgroud) 我正在将一些VB6代码转换为C#.VB6将资源存储在.frx文件中,与C#将其存储在.resx文件中的方式相同.如何将.frx文件中的图像转换为可嵌入.resx文件中的图像?