现在.NET支持尾随逗号阵列像Python做 [英] .NET now support trailing comma in array like python does

查看:157
本文介绍了现在.NET支持尾随逗号阵列像Python做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好今天我只是指出,C#3.5有一个有趣的功能,它是有用的,在Python可以作为很好,可能是其他语言。

Hi Everyone today i just noted that C# 3.5 have a interesting feature which is useful and available in python as well and may be other languages.

这是尾随逗号(,)一个数组中为止。通常,编译器会给出它的错误,但它不需要。而且看起来,微软添加这一功能,现在编译行的事投诉在阵列中的尾随逗号。这是这是现在没有错误的任何更多的处理不必要的错误。

It is the trailing comma(,) in a array. Normally a compiler will give error on it but it need not to. And it seems that Microsoft add this feature and now compiler doest complaint for a trailing comma in array. It was unnecessary error which is now not treated as error any more.

看阵列的声明i和s,看到结尾(逗号),它编译并运行正常

Look at declaration of array i and s and see a trailing (comma), which compile and run correctly.

    static void Main(string[] args) {
        var intarray = new[] { 1, 2, 3, 4, 5, };
        var strarray = new[] { "Hello", ",", " ", "World", };

        Console.WriteLine(string.Join("", strarray));
        foreach (var i in intarray)
            Console.WriteLine(i);

    }

这为信息,而不是一个问题,但意见是值得欢迎的。我使用VS2008

This for informative and not a question but comments are welcome. I am using VS2008.

推荐答案

它不仅在阵,它是在普通对象初始值:

It's not only in array, it's in an object initializer in general:

var foo = new Foo
{
    Prop1 = "Value 1",
    Prop2 = "Value 2",
};

在枚举另外:

enum Foo
{
    Val1, Val2,
}

这篇关于现在.NET支持尾随逗号阵列像Python做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆