我知道PHP没有本机枚举.但我已经从Java世界习惯了它们.我希望使用枚举作为一种方式来提供IDE的自动完成功能可以理解的预定义值.
常量可以解决问题,但是存在名称空间冲突问题,并且(或实际上因为)它们是全局的.数组没有命名空间问题,但是它们太模糊了,它们可以在运行时覆盖,IDE很少(从不?)知道如何自动填充其键.
您是否经常使用任何解决方案/解决方法?有谁回忆一下PHP家伙是否对枚举有任何想法或决定?
在C#中使用switch语句与使用语句的好处/缺点是什么?if/else我无法想象除了你的代码外观之外还有其他重大差异.
是否有任何理由导致生成的IL或相关的运行时性能完全不同?
使用switch语句与使用if30个unsigned枚举的语句的最佳实践是什么,其中大约10个具有预期的操作(目前是相同的操作).需要考虑性能和空间,但并不重要.我已经抽象了代码片段,所以不要因为命名惯例而讨厌我.
switch 声明:
// numError is an error enumeration type, with 0 being the non-error case
// fire_special_event() is a stub method for the shared processing
switch (numError)
{
case ERROR_01 : // intentional fall-through
case ERROR_07 : // intentional fall-through
case ERROR_0A : // intentional fall-through
case ERROR_10 : // intentional fall-through
case ERROR_15 : // intentional fall-through
case ERROR_16 : // intentional fall-through
case ERROR_20 :
{
fire_special_event();
}
break;
default:
{ …Run Code Online (Sandbox Code Playgroud) 如果我有一个具有大量条件的功能,那么组织它的最佳方法是什么?
我担心的是其他人进入代码并了解正在发生的事情.即使示例很简单,也可以想象条件非常复杂.
举个例子:
public void function(string value, string value2)
{
if (value == null)
return;
if (value2 == value)
DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
要么
public void function(string value, string value2)
{
if (value != null)
{
if (value2 == value)
DoSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
要么
public void function(string value, string value2)
{
if (value != null && value2 == value)
DoSomething();
}
Run Code Online (Sandbox Code Playgroud)