在 C# 中重写此 If 的最巧妙/最惯用的方法
我有这个 if-else
语句,它可以实现我想要的功能。正如您应该能够看出的,它所做的事情非常简单。
if (width != null && height != null)
{
if (top != null && left != null)
{
ret.type = VMLDimensionType.full;
}
else
{
ret.type = VMLDimensionType.size;
}
}
else
{
if (top != null && left != null)
{
ret.type = VMLDimensionType.positon;
}
else
{
ret.type = VMLDimensionType.unset;
}
}
所提到的 enum
是:
private enum VMLDimensionType
{
unset = 0,
full = 1,
size = 2,
position = 3
}
它是如此简单,我确信有一种更简洁、更易读的方式来表达这一点。
注意:如果不是 VS 默认强加的可笑的“每行一个大括号”规则,我可能不会这么烦恼。例如,在 VB 中,我可能会从该代码块中丢失大约 10 行! (顺便说一句对此有什么想法吗?)
I have this if-else
statement which does what I want. What it's doing is pretty straightforward as you should be able to tell.
if (width != null && height != null)
{
if (top != null && left != null)
{
ret.type = VMLDimensionType.full;
}
else
{
ret.type = VMLDimensionType.size;
}
}
else
{
if (top != null && left != null)
{
ret.type = VMLDimensionType.positon;
}
else
{
ret.type = VMLDimensionType.unset;
}
}
The enum
being referred to is:
private enum VMLDimensionType
{
unset = 0,
full = 1,
size = 2,
position = 3
}
It's so straightforward I'm sure there's a much more terse and more readable way to express this.
NB If it wasn't for the ridiculous 'one-brace per line' rule that VS imposes by default I probably wouldn't be so bothered. Eg in VB I could lose about 10 lines from this code block! (any thoughts on that as an aside?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种选择是将
VMLDimensionType
设为Flags
枚举:然后:
One option would be to make
VMLDimensionType
aFlags
enumeration:And then:
这个怎么样:
How about this:
我想提取 GetDimensionType() 方法。
并使其不那么小,但更具可读性和自我描述性。
用法:
I would like to extract GetDimensionType() method.
And make it not so small, but more readable and self-descriptive.
Usage:
这个怎么样:
What about this: