我在C#中编写了一些代码,我发现自己在写:
return new MyClass(...
当我注意到return
和new
都是C#关键字时。所以我想知道C#中最长的关键字合法序列是什么。我能想到的只有:
internal static override void MyFunc(...
其中internal static override void
是所有关键字。你能想到更长的关键词序列吗?
注意:这个问题确实没有意义。我只是希望在火上浇一些乐趣: - )
答案 0 :(得分:43)
对于6:
new protected internal unsafe virtual decimal Foo() {...}
编辑7:
new protected internal unsafe virtual extern decimal Foo();
如果我们允许括号和括号......
(已编辑“锁定”,“新对象()”,“as”和“字符串”由其他人提供;请参阅评论)
decimal Bar() {
lock (new object() as string) {
if (true) {
checked {
unsafe {
try {
do {
return default(decimal);
unchecked {break;}
continue;
} while (false);
}
catch { throw; }
finally { }
}
}
}
}
}
答案 1 :(得分:33)
我想这是无限的:
return null as string as string as string as string as string....
答案 2 :(得分:2)
这是另一种情况,只要你愿意:
do do do do do do do do do do do do do do do // ...
while(x) while(x) while(x) while(x) while(x) // ...
使用上下文关键字,您也可以
await await await await await await await // ...
答案 3 :(得分:1)
internal protected static volatile string foo = "bar";
那是5。
答案 4 :(得分:0)
另一个带有方法定义的变体(由我的同事发现):
protected internal override sealed unsafe async void await() { ... }
连续制作8个关键字。使用await
是上下文关键字的事实,因此可以将其重用于方法名称。
答案 5 :(得分:0)
10个外部部分被黑客入侵
abstract partial class A
{
private protected virtual extern unsafe ref readonly delegate*<int> Test();
}
partial class B : A
{
private protected sealed override unsafe partial ref readonly delegate*<int> Test();
// 1 2 3 4 5 6 7 8 9 10
private protected sealed override extern unsafe partial ref readonly delegate*<int> Test();
}
答案 6 :(得分:-2)
我可以作弊吗?
internal protected static volatile StringBuilder @string =
new StringBuilder(int.Parse("12"));
使用关键字或其他保留术语作为变量名称的事实,如果我在前面添加@,如果允许复制StringBuilder,则在9处。