邪恶还是不邪恶?
public static void Raise(this EventHandler handler, object sender, EventArgs args)
{
if (handler != null)
{
handler(sender, args);
}
}
// Usage:
MyButtonClicked.Raise(this, EventArgs.Empty);
// This works too! Evil?
EventHandler handler = null;
handler.Raise(this, EVentArgs.Empty);
Run Code Online (Sandbox Code Playgroud)
请注意,由于扩展方法的性质,如果MyButtonClicked为null,MyButtonClicked.Raise将不会抛出NullReferenceException.(例如,没有MyButtonClicked事件的监听器).
邪恶与否?
毫无疑问,我会选择将STL用于大多数C++编程项目.最近我提出了这个问题,"有没有你不会使用STL的情况?"......
我越是想到它,我就越意识到也许应该是我选择不使用STL的情况......例如,一个非常大的长期项目,其代码库预计将持续数年......也许是真正符合项目需求的定制容器解决方案值得最初的开销吗?你怎么想,有没有你选择不STL的情况?
鉴于Reactive Extensions(Rx)框架提供的可组合事件的好处,我想知道我的类是否应该停止推送.NET事件,而是暴露Rx可观察量.
例如,使用标准.NET事件获取以下类:
public class Foo
{
private int progress;
public event EventHandler ProgressChanged;
public int Progress
{
get { return this.progress; }
set
{
if (this.progress != value)
{
this.progress = value;
// Raise the event while checking for no subscribers and preventing unsubscription race condition.
var progressChanged = this.ProgressChanged;
if (progressChanged != null)
{
progressChanged(this, new EventArgs());
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
很多单调的管道.
这个类可以使用某种observable来替换这个功能:
public class Foo
{
public Foo()
{
this.Progress = some new observable; …
Run Code Online (Sandbox Code Playgroud) 编辑2015这个问题及其答案已不再适用.它在C#6出现之前被问到,它具有空传播的opertor(?.),它避免了在这个问题和随后的答案中讨论的hacky-workarounds.截至2015年,在C#中,您现在应该使用Form.ActiveForm?.ActiveControl?.Name.
我一直在考虑.NET中的空传播问题,这常常导致丑陋的重复代码,如下所示:
尝试#1常用代码:
string activeControlName = null;
var activeForm = Form.ActiveForm;
if (activeForm != null)
{
var activeControl = activeForm.ActiveControl;
if(activeControl != null)
{
activeControlname = activeControl.Name;
}
}
Run Code Online (Sandbox Code Playgroud)
StackOverflow上有一些关于Maybe <T> monad的讨论,或者使用某种"if not null"扩展方法:
尝试#2,扩展方法:
// Usage:
var activeControlName = Form.ActiveForm
.IfNotNull(form => form.ActiveControl)
.IfNotNull(control => control.Name);
// Definition:
public static TReturn IfNotNull<TReturn, T>(T instance, Func<T, TReturn> getter)
where T : class
{
if (instance != null ) return getter(instance);
return null;
}
Run Code Online (Sandbox Code Playgroud)
我认为这更好,然而,重复的"IfNotNull"和lambdas会有一些语法混乱.我现在正在考虑这个设计:
尝试使用扩展方法#3,可能<T>
// Usage:
var activeControlName …
Run Code Online (Sandbox Code Playgroud) 给定以下结构:
public struct Foo<T>
{
public Foo(T obj) { }
public static implicit operator Foo<T>(T input)
{
return new Foo<T>(input);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码编译:
private Foo<ICloneable> MakeFoo()
{
string c = "hello";
return c; // Success: string is ICloneable, ICloneable implicitly converted to Foo<ICloneable>
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有编译 - 为什么?
private Foo<ICloneable> MakeFoo()
{
ICloneable c = "hello";
return c; // Error: ICloneable can't be converted to Foo<ICloneable>. WTH?
}
Run Code Online (Sandbox Code Playgroud) 我只是在挖掘一些JavaScript代码(Raphaël.js)并遇到以下行(稍微翻译):
Math.min.apply(0, x)
Run Code Online (Sandbox Code Playgroud)
在哪里x
是一个数组.为什么你会这样做?行为似乎是"从数组中取出最小值x
".
我的任务是构建一个.NET客户端应用程序来检测WAV文件中的静音.
内置Windows API可以实现这一点吗?或者,那里有任何好的图书馆来帮助解决这个问题?
我有点惊讶地发现以下代码的结果,我只想从一系列int中删除所有3:
var sequence = new [] { 1, 1, 2, 3 };
var result = sequence.SkipWhile(i => i == 3); // Oh noes! Returns { 1, 1, 2, 3 }
Run Code Online (Sandbox Code Playgroud)
为什么不跳过3?
我的下一个想法是,好吧,Except运算符可以解决这个问题:
var sequence = new [] { 1, 1, 2, 3 };
var result = sequence.Except(i => i == 3); // Oh noes! Returns { 1, 2 }
Run Code Online (Sandbox Code Playgroud)
综上所述,
有人可以解释为什么SkipWhile不会跳过最后一个元素吗?任何人都可以建议我可以用什么LINQ运算符从上面的序列中删除'3'?
给定类型a和类型b,我如何在运行时确定是否存在从a到b的隐式转换?
如果这没有意义,请考虑以下方法:
public PropertyInfo GetCompatibleProperty<T>(object instance, string propertyName)
{
var property = instance.GetType().GetProperty(propertyName);
bool isCompatibleProperty = !property.PropertyType.IsAssignableFrom(typeof(T));
if (!isCompatibleProperty) throw new Exception("OH NOES!!!");
return property;
}
Run Code Online (Sandbox Code Playgroud)
这是我想要工作的调用代码:
// Since string.Length is an int property, and ints are convertible
// to double, this should work, but it doesn't. :-(
var property = GetCompatibleProperty<double>("someStringHere", "Length");
Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×2
events ×2
audio ×1
c++ ×1
containers ×1
javascript ×1
linq ×1
lucene ×1
monads ×1
reflection ×1
search ×1
stl ×1