在下面的代码中,对Method2的调用接收Value参数为False,即使基类根本没有声明参数的默认值,派生类声明True为默认值.
可以认为(在这里的类似示例中已经完成:在重写方法上使用C#可选参数)编译器首先使用基类的方法声明(这是正确的,因为可以通过对Method1调用前缀来更改此行为this.),但是在这种情况下,基数根本不会声明默认值.
对此有合理的解释吗?
using System;
class Base
{
public virtual bool Method1(bool Value) { return true; }
public virtual bool Method2(bool Value) { return true; }
}
class Derived : Base
{
public override bool Method1(bool Value = true)
{
return Value;
}
public override bool Method2(bool Value = true)
{
return Method1();
}
}
class Program
{
static void Main(string[] args)
{
Derived a = new Derived();
Console.WriteLine("Call to Method1, expected: True, got: …Run Code Online (Sandbox Code Playgroud) 我想到的场景是:Service Bus用于实例到实例的通信,因此Subscription对每个服务实例都是唯一的.最终结果是,如果实例未正常关闭,则其订阅不会被删除.
当服务实例"死"并重新启动时,订阅的先前内容是不相关的并且可以被丢弃.
那么,有没有办法为Service Bus订阅设置"生存时间"或模拟类似的东西,而不必诉诸一些自定义的孤立检测机制?
为什么 C# 编译器会为尝试对不可变属性调用索引器集访问器的代码生成错误 CS1612?
using System;
using System.Collections.Generic;
using System.Text;
namespace StructIndexerSetter
{
struct IndexerImpl {
private TestClass parent;
public IndexerImpl(TestClass parent)
{
this.parent = parent;
}
public int this[int index]
{
get {
return parent.GetValue(index);
}
set {
parent.SetValue(index, value);
}
}
}
class TestClass
{
public IndexerImpl Item
{
get
{
return new IndexerImpl(this);
}
}
internal int GetValue(int index)
{
Console.WriteLine("GetValue({0})", index);
return index;
}
internal void SetValue(int index, int value)
{
Console.WriteLine("SetValue({0}, {1})", index, value);
} …Run Code Online (Sandbox Code Playgroud)