where T : struct
Run Code Online (Sandbox Code Playgroud)
我们C#开发人员都知道C#的基础知识.我的意思是声明,条件,循环,运算符等.
我们中的一些人甚至掌握了Generics,匿名类型,lambdas,LINQ等......
但是C#粉丝,瘾君子,专家几乎都不知道C#最隐藏的功能或技巧是什么?
yield由迈克尔·葡萄汁var由迈克尔·葡萄汁using()kokos的声明readonly由kokosas由迈克·斯通as/ is由埃德Swangrenas/ is(改进)由Rocketpantsdefault由deathofratsglobal::通过pzycomanusing()由块AlexCusevolatile作者:JakubŠturcextern alias作者:JakubŠturcJava要求如果在构造函数中调用this()或super(),它必须是第一个语句.为什么?
例如:
public class MyClass {
public MyClass(int x) {}
}
public class MySubClass extends MyClass {
public MySubClass(int a, int b) {
int c = a + b;
super(c); // COMPILE ERROR
}
}
Run Code Online (Sandbox Code Playgroud)
Sun编译器说"调用super必须是构造函数中的第一个语句".Eclipse编译器说"构造函数调用必须是构造函数中的第一个语句".
但是,您可以通过重新安排代码来解决这个问题:
public class MySubClass extends MyClass {
public MySubClass(int a, int b) {
super(a + b); // OK
}
}
Run Code Online (Sandbox Code Playgroud)
这是另一个例子:
public class MyClass {
public MyClass(List list) {}
}
public class MySubClassA extends MyClass {
public MySubClassA(Object item) {
// Create a list …Run Code Online (Sandbox Code Playgroud) 我最近一直在阅读很多Javascript,并且我注意到要导入的.js文件中的整个文件包含如下所示.
(function() {
...
code
...
})();
Run Code Online (Sandbox Code Playgroud)
这样做的原因是什么,而不是一组简单的构造函数?
Eric Lippert对这个问题的评论让我彻底糊涂了.C#中的转换和转换有什么区别?
Haskell被称为"纯函数式语言".
在这种情况下,"纯粹"意味着什么?这对程序员有什么影响?
使用表达式成员允许您将方法或属性的主体定义为没有return关键字的单个表达式(如果它返回了某些内容).
例如,它变成了这些
int Method1()
{
return 5;
}
void Method2()
{
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)
进入这些
int Method1() => 5;
void Method2() => Console.WriteLine();
Run Code Online (Sandbox Code Playgroud)
当您从正文中抛出异常时,会产生差异:
void Method3()
{
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
但是,以下内容将无法编译:
void Method3() => throw new Exception();
Run Code Online (Sandbox Code Playgroud)
以下消息:
Warning The member 'Program.Exception()' does not hide an inherited member. The new keyword is not required.
Error 'Program.Exception()' must declare a body because it is not marked abstract, extern, or partial
Error ; expected
Error Invalid token 'throw' in class, struct, or …Run Code Online (Sandbox Code Playgroud) 我已经读过,通常c ++中的语句以分号结尾; 这可能有助于解释表达式语句的含义.但那么你举一个例子,你会称之为表达式?
在这种情况下,只是语句或表达式语句或表达式?
int x;
x = 0;
Run Code Online (Sandbox Code Playgroud) 在浏览LinCAN驱动程序的源代码时,我发现了一些困扰我的宏.
#else /*CONFIG_PREEMPT*/
#define can_preempt_disable() do { } while (0)
#define can_preempt_enable() do { } while (0)
#endif /*CONFIG_PREEMPT*/
Run Code Online (Sandbox Code Playgroud)
我理解的有用性
do {
...;
if(condition) break;
...
} while (0);
Run Code Online (Sandbox Code Playgroud)
使用break作为一种throw.我半理解包装一系列函数,如
#define FOO() do { foo(); bar(); } while (0)
Run Code Online (Sandbox Code Playgroud)
避免使用无支撑的警告if.我理解有时#define需要"无操作语句".但为什么这种特殊的呢?特别是,空括号,虚假条件,做......而?一些语法警告我无法理解?
c# ×3
c ×1
c#-6.0 ×1
c++ ×1
casting ×1
coding-style ×1
constructor ×1
expression ×1
haskell ×1
iife ×1
java ×1
javascript ×1
loops ×1
paradigms ×1
python ×1
roslyn ×1
scope ×1
terminology ×1