我在某个地方读了一本电子书(我很想再次找到),通过使用委托,可以编写具有如下语法的代码:
()(); // where delegate precedes this.
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供任何细节如何可能/在什么情况下会发生这种情况?
Jon*_*eet 127
你可以比目前给出的例子略胜一筹,实际上......你可以任意扩展它:
class Test
{
delegate Hofstadter Hofstadter();
static void Main()
{
// Unfortunately I'm clearly not as smart as the real thing
Hofstadter douglas = () => null;
douglas()()()()()()();
}
}
Run Code Online (Sandbox Code Playgroud)
对于额外的ASCII艺术,这是另一个可怕的选择:
class Test
{
delegate __ ___();
delegate ___ __(___ _);
static void Main()
{
___ _ = () => null;
_ ()((_))();
}
}
Run Code Online (Sandbox Code Playgroud)
请永远不要这样做.
编辑:最后一个 - 虽然它只是用下划线替换其他东西,并尽可能重用名称:
class Test
{
delegate void _();
delegate __<_> ___<_>();
delegate ___<_> __<_>(___<_> ____);
static ___<_> ____<_>(___<_> ____) { return ____; }
static __<_> ____<_>() { return ____<_>; }
static void Main()
{
((__<_>)____)(____<_>)();
}
}
Run Code Online (Sandbox Code Playgroud)
Ree*_*sey 35
这是一个演示此示例的示例程序:
using System;
class Program
{
static Action GetMethod()
{
return () => Console.WriteLine("Executing");
}
static void Main()
{
GetMethod()();
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
话虽这么说,我不会在生产代码中这样做.这是非常意外的.
编辑:以防万一你想要看到更丑陋的东西... [尤其是" ()()[()=>{}]()"]:
using System;
class Program
{
static void Main()
{
(new Program()).Confusion();
Console.ReadKey();
}
public Action this[Action index]
{
get {
return () => Console.WriteLine("Executing");
}
}
Func<Program> GetInstance()
{
return () => this;
}
void Confusion()
{
// This is particularly ugly...
GetInstance()()[()=>{}]();
}
}
Run Code Online (Sandbox Code Playgroud)
Ric*_*ard 12
你只需要一点点自我引用,你可以随意调用它:
delegate Foo Foo();
class Program {
static void Main(string[] args) {
Foo f = null;
f = () => f;
// Add more "()" as you feel like...
f()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()();
}
}
Run Code Online (Sandbox Code Playgroud)
static void Foo()
{
Console.WriteLine("Hello World!");
}
static Action Bar()
{
return new Action(Foo);
}
static void Main()
{
Func<Action> func = new Func<Action>(Bar);
func()();
Bar()();
}
Run Code Online (Sandbox Code Playgroud)
版画
Hello World! Hello World!
这是有效的,因为func()并Bar()返回一个Action可以使用常规方法调用语法调用的委托.
| 归档时间: |
|
| 查看次数: |
3850 次 |
| 最近记录: |