19

我正在用 C# 编写一些代码,我发现自己在写:

return new MyClass(...

当我注意到 thereturn和 thenew都是 C# 关键字时。所以我想知道 C# 中最长的合法关键字序列是什么。我能想到的只有:

internal static override void MyFunc(...

internal static override void所有关键字在哪里。你能想到更长的关键字序列吗?

注意:这个问题真的没有意义。我只是希望在火上浇更多的乐趣:-)

4

8 回答 8

47

对于 6:

new protected internal unsafe virtual decimal Foo() {...}

编辑 7:

new protected internal unsafe virtual extern decimal Foo();

如果我们允许括号和大括号...

编辑了“lock”、“new object()”、“as”和“string”由其他人贡献;见评论)

decimal Bar() {
    lock (new object() as string) {
        if (true) {
            checked {
                unsafe {
                    try {
                        do {
                            return default(decimal);
                            unchecked {break;}
                            continue;
                        } while (false);
                    }
                    catch { throw; }
                    finally { }
                }
            }
        }
    }
}
于 2009-03-23T11:21:35.603 回答
36

我猜它是无限的:

return null as string as string as string as string as string....
于 2009-03-23T11:29:03.183 回答
3

这是另一种情况,只要您愿意:

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 // ...
于 2015-03-27T19:36:29.200 回答
1
internal protected static volatile string foo = "bar";

那是5。

于 2009-03-23T11:18:54.173 回答
0

方法定义的另一种变体(由我的同事发现):

protected internal override sealed unsafe async void await() { ... }

连续生成 8 个关键字。使用作为上下文关键字的事实await,因此可以将其重用于方法名称。

于 2015-11-12T14:27:08.313 回答
0
public abstract class Base {
    protected internal abstract ref readonly int MyMethod();
}

public class Sub : Base {
    extern sealed protected internal override unsafe ref readonly int MyMethod();
}

设法让 9 进入方法声明。不确定是否可以在方法声明中获得更多信息

于 2020-05-07T19:52:28.763 回答
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();
}
于 2020-11-26T08:48:55.550 回答
-2

我可以作弊吗?

internal protected static volatile StringBuilder @string = 
  new StringBuilder(int.Parse("12"));

使用我可以使用关键字或其他保留术语作为变量名的事实,如果我在它前面加上一个 @ - 如果你允许 StringBuilder 的重复,则在 9 处。

于 2009-03-23T11:24:59.713 回答