在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.

其他回答

我喜欢在C中插入八进制值:

int values[8] = { 123, 154, 103, 310, 046, 806, 002, 970 };

在C中,a[b][C]与C [b[a]]完全相同。

在C:

warning C4013: 'myfunc' undefined; assuming extern returning int

我记得由于某些原因没有看到警告(在一些遗留代码中有太多警告?),并且困惑于为什么从int转换会在使用非int返回函数时导致编译器错误。

编译器假设这样的东西是相当出乎意料的。

Java有一整本关于它们的书。

书http://www.javapuzzlers.com/lg-puzzlers-cropped.jpg

爪哇益智游戏

闲聊:

在类Test中有一个类方法,返回一个常量字符串:

method1
    ^ 'niko'

无论发生什么,这个方法都会不断返回字符串'niko'。但事实并非如此。

s := Test method1 

(设置为“niko”。)

s at: 4 put: $i.

(设置为“niki”。)

s := Test method1

(再次设置为“niki”。)

因此,第二行代码永久地将method1更改为返回'niki'而不是'niko',即使方法的源代码没有更新。