where T : struct
我们 C#开发人员都知道 C#的基础知识。我的意思是声明,条件,循环,运算符等。
我们中的一些人甚至掌握了Generics , 匿名类型 , lambdas , LINQ等......
但是,即使 C#粉丝,瘾君子,专家几乎不知道 C#最隐藏的功能或技巧是什么?
yield
by Michael Stumvar
by Michael Stumusing()
statement by kokosreadonly
by kokosas
by Mike Stoneas
/ is
by Ed Swangrenas
/ is
(improved) by Rocketpantsdefault
by deathofratsglobal::
by pzycomanusing()
blocks by AlexCusevolatile
by Jakub Šturcextern alias
by Jakub ŠturcDefaultValueAttribute
by Michael StumObsoleteAttribute
by DannySmurfDebuggerDisplayAttribute
by StuDebuggerBrowsable
and DebuggerStepThrough
by bdukesThreadStaticAttribute
by marxidadFlagsAttribute
by Martin ClarkeConditionalAttribute
by AndrewBurns??
(coalesce nulls) operator by kokoswhere T:new
by Lars Mæhlumenum
values by lfoustevent
operators by marxidad?:
) by JasonSchecked
and unchecked
operators by Binoj Antonyimplicit and explicit
operators by Flory__makeref __reftype __refvalue
by Judah Himangopartial
methods by Jon EricksonDEBUG
pre-processor directive by Robert Durgin__arglist
by Zac BowlingTransactionScope
by KiwiBastardDependantTransaction
by KiwiBastardNullable<T>
by IainMHMutex
by DiagoSystem.IO.Path
by ageektrappedWeakReference
by Juan ManuelString.IsNullOrEmpty()
method by KiwiBastardList.ForEach()
method by KiwiBastardBeginInvoke()
, EndInvoke()
methods by Will DeanNullable<T>.HasValue
and Nullable<T>.Value
properties by RismoGetValueOrDefault
method by John Sheehan这不是 C#本身,但我没有看到任何人真正使用System.IO.Path.Combine()
到他们应该的程度。事实上,整个 Path 类非常有用,但没有人使用它!
我愿意打赌每个生产应用程序都有以下代码,即使它不应该:
string path = dir + "\\" + fileName;
lambdas 和类型推理被低估了。 Lambdas 可以有多个语句 ,它们会自动兼容为兼容的委托对象 (只需确保签名匹配),如下所示:
Console.CancelKeyPress +=
(sender, e) => {
Console.WriteLine("CTRL+C detected!\n");
e.Cancel = true;
};
请注意,我没有new CancellationEventHandler
也没有指定sender
和e
类型,它们可以从事件中推断出来。这就是为什么编写整个delegate (blah blah)
并不需要你指定参数类型的麻烦。
Lambdas 不需要返回任何东西 ,类型推断在这样的上下文中非常强大。
顺便说一下,你可以随时返回Lambdas,它们在函数式编程意义上使 Lambdas成为可能。例如,这是一个 lambda,它使 lambda 处理 Button.Click 事件:
Func<int, int, EventHandler> makeHandler =
(dx, dy) => (sender, e) => {
var btn = (Button) sender;
btn.Top += dy;
btn.Left += dx;
};
btnUp.Click += makeHandler(0, -1);
btnDown.Click += makeHandler(0, 1);
btnLeft.Click += makeHandler(-1, 0);
btnRight.Click += makeHandler(1, 0);
注意链接: (dx, dy) => (sender, e) =>
这就是为什么我很高兴参加函数式编程课程:-)
除了 C 中的指针,我认为这是你应该学习的另一个基本的东西:-)