C# 语法快捷键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1435849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Syntax Shortcuts
提问by Graham Conzett
I was wondering if there exists somewhere a collection or list of C# syntax shortcuts. Things as simple omitting the curly braces on if
statements all the way up to things like the ??
coalesce operator.
我想知道某处是否存在 C# 语法快捷方式的集合或列表。就像省略if
语句上的花括号一样简单,直到诸如??
合并运算符之类的事情。
采纳答案by codymanix
a = b ? c : d ;
is short for
是简称
if (b) a = c; else a = d;
And
和
int MyProp{get;set;}
is short for
是简称
int myVar;
int MyProp{get {return myVar; } set{myVar=value;}}
Also see the code templates in visual studio which allows you to speed coding up.
另请参阅 Visual Studio 中的代码模板,它可让您加快编码速度。
But note that short code doesn't mean necessarily good code.
但请注意,短代码并不一定意味着好的代码。
回答by Joren
I don't know of a precompiled list, but the C# Reference(especially the C# Keywords section) concisely contains the information you're looking for if you're willing to read a bit.
我不知道预编译列表,但C# 参考(尤其是 C# 关键字部分)简洁地包含了您正在寻找的信息,如果您愿意阅读一下的话。
回答by Vadim
How does this C# basic reference pdf documentlooks to you?
你觉得这个 C# 基本参考pdf 文档怎么样?
Here's another pdf.
这是另一个 pdf。
回答by mr_dunski
My all time favorite is
我最喜欢的是
a = b ?? c;
which translates to
这意味着
if (b != null) then a = b; else a = c;
回答by HeathenWorld
They are not syntax shortcuts, but snippets are great coding shortcuts. Typing prop (tab)(tab), for example, stubs out the code you need for a property.
它们不是语法快捷方式,但片段是很棒的编码快捷方式。例如,键入 prop (tab)(tab) 会删除您需要的属性代码。
http://msdn.microsoft.com/en-us/library/ms165392(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ms165392(VS.80).aspx
回答by Yatrix
c# 6.0 has some fun ones. ?.
and ?
(null conditional operator) is my favorite.
c# 6.0 有一些有趣的。?.
和?
(空条件运算符)是我的最爱。
var value = obj != null ? obj.property : null;
turns into
var value = obj != null ? obj.property : null;
变成
var value = obj?.property
and
和
var value = list != null ? list[0] : null;
var value = list != null ? list[0] : null;
turns into
变成
var value = list?[0]