我听说Liskov替换原则(LSP)是面向对象设计的基本原则.它是什么以及它的使用例子是什么?
oop liskov-substitution-principle definition design-principles solid-principles
看作C#无法打开一个Type(我收集的并不是作为特殊情况添加的,因为is-a关系意味着可能有多个不同的情况可能适用),是否有更好的方法来模拟切换类型?
void Foo(object o)
{
if (o is A)
{
((A)o).Hop();
}
else if (o is B)
{
((B)o).Skip();
}
else
{
throw new ArgumentException("Unexpected type: " + o.GetType());
}
}
Run Code Online (Sandbox Code Playgroud) 我从未见过以for这种方式初始化的循环,也不明白为什么会以这种方式编写?
我正在研究连接到.NET中的IMAP服务器,并开始查看名为ImapX的库中的代码.我for在一个方法中找到了循环,该方法将数据写入a NetworkStream然后似乎在funky for循环中读取响应.我不想逐字复制和粘贴别人的代码,但这里是要点:
public bool SendData(string data)
{
try
{
this.imapStreamWriter.Write(data);
for (bool flag = true; flag; flag = false)
{
var s = this.imapStreamReader.ReadLine();
}
}
catch (Exception)
{
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
同样,这不是确切的代码,但它是一般的想法.这就是所有方法所做的,它不使用服务器响应,只是true在没有抛出异常的情况下返回.我只是不明白for循环是如何或为什么这样使用的; 任何人都可以解释初始化这个优惠的优势,如果有的话?
可能重复:
C# - if语句中的赋值
我发现自己做了以下多次
if (e.row.FindControl("myLiteral") is Literal)
{
(e.row.FindControl("myLiteral") as Literal).Text = "textData";
}
Run Code Online (Sandbox Code Playgroud)
有没有办法替换"if"部分并简化setter:
(e.row.FindControl("myLiteral") <some operator that combines is and as> .text = "textData";
Run Code Online (Sandbox Code Playgroud)
编辑:我之前应该提到这一点 - 我想完全删除'if'.
"some operator"应该在内部执行此操作,并且仅当e.row.FindControl是文字时才设置".text"