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

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


当前回答

下面的c#代码抛出NullReferenceException而不是打印1:

    static void SomeMethod(string format, params object[] args)
    {
        Console.WriteLine(args.Length);
    }

    static void Main(string[] args)
    {
        SomeMethod("blabla", null, "Ok here"); // print 2
        SomeMethod("blabla", null); // exception
    }

其他回答

Python的everything-is-really-a-reference有一个有趣的副作用:

>>> a = [[1]] * 7
>>> a
[[1], [1], [1], [1], [1], [1], [1]]
>>> a[0][0] = 2
>>> a
[[2], [2], [2], [2], [2], [2], [2]]

在Python中:

abs((10+5j)-(25+-5j))

返回~18.03,根据毕达哥拉斯定理,这是点(10,5)和(25,5)之间的距离。之所以会出现这种情况,是因为Python的原生语言支持2+2j形式的复数。由于复数的绝对值形式为a+bj =根号下(a^2+b^2),我们得到距离,同时从一个复数减去另一个复数,然后应用abs(绝对)函数在它上面。

在x++ (Microsoft Dynamics AX):

1)需要在单独的行上使用分号(;)将变量声明与语句分开(至少在4.0版本之前)

    int i;
    int myArray[5];
    ;
    i = 1;

2)数组索引是基于1的,所以你不允许像in那样使用索引0(零)从数组中读取

    int myArray[5];
    ;
    print myArray[0];    // runtime error

这并不奇怪,但是你可以在赋值的左边使用0索引,比如in

    int myArray[5];
    ;
    myArray[2] = 102;
    myArray[0] = 100;    // this is strange
    print myArray[2];    // expcting 102?

会发生什么呢?数组被初始化为它的默认值,无论赋值中使用了什么值。上面的代码输出0(零)!

早期的FORTRAN,空格不重要。(anti-Python !)

DO 20 I = 1, 10

含义:从这里循环到第20行,I从1到10。

DO 20 I = 1. 10

含义:将1.10分配给名为DO20I的变量。

有传言说这个漏洞毁了一个太空探测器。

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.