知道为什么这段代码:
extern "C" __declspec(dllexport) void Transform(double x[], double y[], int iterations, bool forward)
{
long n, i, i1, j, k, i2, l, l1, l2;
double c1, c2, tx, ty, t1, t2, u1, u2, z;
/* Calculate the number of points */
n = (long)pow((double)2, (double)iterations);
/* Do the bit reversal */
i2 = n >> 1;
j = 0;
for (i = 0; i < n - 1; ++i)
{
if (i < j)
{
tx = x[i];
ty = …Run Code Online (Sandbox Code Playgroud) 出于某种原因,我注意到我最终在工作中使用了很多有限状态机.特别是,当我实现自定义TCP /串行协议时,它们非常有用并且产生非常强大的输出(在我看来).
我在CS课上的日子远远落后于我.因此,我对那里学到的东西的回忆是模糊的.我很好奇是否有其他人在利用我已经忘记的概念.
没有正确的答案.投票包含您最常使用的概念的答案.我们最终会得到最常用的概念.对我来说,这将是一个要研究的东西清单.
- 罗伯特·
我觉得我很难找到一个相当简单的方法.
我想我们都用过这个:
select someThing from someTable where someColumn in('item1', 'item2')
Run Code Online (Sandbox Code Playgroud)
在C#中,我必须写这样的东西:
if (someEnum == someEnum.Enum1 || someEnum == someEnum.Enum2 ||
someEnum == someEnum.Enum3)
{
this.DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
这有效,但它只是罗嗦.
出于沮丧,我写了一个扩展方法来完成我想要做的事情.
namespace System
{
public static class SystemExtensions
{
public static bool In<T>(this T needle, params T[] haystack)
{
return haystack.Contains(needle);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我可以编写更短的代码:
if (someEnum.In(someEnum.Enum1, someEnum.Enum2, someEnum.Enum3))
this.DoSomething();
if (someInt.In(CONSTANT1, CONSTANT2))
this.DoSomethingElse();
Run Code Online (Sandbox Code Playgroud)
但是,为我在框架中找不到的东西编写自己的方法感觉很脏.
你们大家可以提供的任何帮助都会很棒,谢谢
编辑:感谢大家的深入分析.我想我会继续使用我的In()方法.
我很难描述这个问题.也许这就是为什么我很难找到一个好的解决方案(这些话只是不合作).让我通过代码解释:
// original code
enum Fruit
{
Apple,
Orange,
Banana,
}
...
Fruit fruit = acquireFruit();
if (fruit != Fruit.Orange && fruit != Fruit.Banana)
coreFruit();
else
pealFruit();
eatFruit();
Run Code Online (Sandbox Code Playgroud)
现在假装多年的发展与这三种类型.上述逻辑的不同风格在存储过程,SSIS包,Windows应用程序,Web应用程序,Java应用程序,perl脚本等中传播....
最后:
// new code
enum Fruit
{
Apple,
Orange,
Banana,
Grape,
}
Run Code Online (Sandbox Code Playgroud)
大多数时候,"系统"运行正常,直到使用Grapes.然后,当不需要或不需要时,系统的某些部分会不恰当地起作用,剥离和/或取芯葡萄.
你坚持什么样的指导方针,以避免这些混乱?我的偏好是旧代码抛出异常,如果它没有被重构以考虑新的枚举.
我在黑暗中想出了一个镜头:
#1避免像这样"不在逻辑中"
// select fruit that needs to be cored
select Fruit from FruitBasket where FruitType not in(Orange, Banana)
Run Code Online (Sandbox Code Playgroud)
#2需要时使用精心构造的NotIn()方法
internal static class EnumSafetyExtensions
{
/* By adding enums to these methods, you certify that 1.) ALL …Run Code Online (Sandbox Code Playgroud) 我正在写一个原型来证明我正在研究的东西的可行性.基本上,它需要将不在磁盘上的程序集加载到应用程序域中.从表面上看,听起来很容易.事实上,它是WinForms世界中孩子的游戏,其中一个过程就是一个过程.
对于ASP.Net Web应用程序,它有点松鼠.我有99.99%的工作.目前的方法有点像这样工作:
public class AppDomainManager : System.AppDomainManager
{
PhantomAssemblyLoader m_phantomAssemblyLoader;
public AppDomainManager()
{
m_phantomAssemblyLoader = new PhantomAssemblyLoader();
}
public override void InitializeNewDomain(AppDomainSetup appDomainInfo)
{
m_phantomAssemblyLoader.Attach(AppDomain.CurrentDomain);
}
}
public class PhantomAssemblyLoader
{
public PhantomAssemblyLoader()
{
}
public void Attach(AppDomain appDomain)
{
appDomain.AssemblyResolve += new ResolveEventHandler(AssemblyResolve);
appDomain.DomainUnload += new EventHandler(DomainUnload);
}
public void Detach(AppDomain appDomain)
{
appDomain.AssemblyResolve -= AssemblyResolve;
appDomain.DomainUnload -= DomainUnload;
}
void DomainUnload(object sender, EventArgs e)
{
this.Detach(sender as AppDomain);
}
private Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly …Run Code Online (Sandbox Code Playgroud) 如果我不需要,我宁愿不使用Linq(当我开始一个新项目时,我删除对它的引用).不过,Lambdas看起来很有用.我不会养成使用lambdas的习惯,但是,如果Linq必须标记为骑行.
提前致谢
我正在研究一种仅适用于引用类型的扩展方法。然而,我认为目前正在对值进行装箱和拆箱。我怎样才能避免这种情况?
namespace System
{
public static class SystemExtensions
{
public static TResult GetOrDefaultIfNull<T, TResult>(this T obj, Func<T, TResult> getValue, TResult defaultValue)
{
if (obj == null)
return defaultValue;
return getValue(obj);
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
public class Foo
{
public int Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在某种方法中:
Foo aFooObject = new Foo { Bar = 1 };
Foo nullReference = null;
Console.WriteLine(aFooObject.GetOrDefaultIfNull((o) => o.Bar, 0)); // results: 1
Console.WriteLine(nullReference.GetOrDefaultIfNull((o) => o.Bar, 0)); // results: 0
Run Code Online (Sandbox Code Playgroud) 可能重复:
C#成员变量初始化; 最佳实践?
这有什么好处:
public class RemotingEngine
{
uint m_currentValueId;
object m_lock;
public RemotingEngine()
{
m_currentValueId = 0;
m_lock = new object();
}
Run Code Online (Sandbox Code Playgroud)
与此:
public class RemotingEngine
{
uint m_currentValueId = 0;
object m_lock = new object();
Run Code Online (Sandbox Code Playgroud)
我一直在避开第二个因为它感觉"脏".这显然不那么打字,所以这对我很有吸引力.