对象初始化程序中允许使用额外的逗号 [英] Extra comma allowed in object initializer

查看:47
本文介绍了对象初始化程序中允许使用额外的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个古老而众所周知的事情,但我刚刚得知我可以在对象初始化程序中编写一个额外的逗号,并且它已被编译器接受。



考虑以下示例

 命名空间 ConsoleApplication1 {
计划{
静态 void Main( string [] args){

// 创建初始化程序中带有额外逗号的新对象
CommaComa theThing = new CommaComa(){A = 1 ,}; // 无错误
}
}

/// < 摘要 >
/// 某些类
/// < / summary >
public class CommaComa {
/// < 摘要 >
/// 属性
/// < / summary >

public int A { get ; set ; }
/// < 摘要 >
/// 另一个属性
/// < / summary >
public string B { get ; set ; }

/// < 摘要 >
/// 某些方法
/// < / summary >
public void Foo( int a, int b = 5 ){}
}
}



正如您所看到的,在分配 A = 1 之后还有一个额外的逗号,但是代码编译得很好。



现在问题是(基本上是古玩sity),在什么样的用例中,在它之后添加额外的逗号是没有意义的?



据我所知,在初始化器之外不允许这样做。如果这是系统性的,我希望下面这样的东西也可以工作

 theThing.Foo(a: 1 ,);  //  提供参数缺失错误 

解决方案

也许它是这样做的,允许开发人员重新排序他们的分配,而不必费心在除了最后一行之外的每一行都添加一个逗号。



类似于:

 KarmaComa theThing =  new  KarmaComa(){
A = 1
B = Foo
};



可以很容易地重构为:

 KarmaComa theThing =  new  KarmaComa(){
B = Foo
A = 1
};



只需拖放线即可。



但这只是猜测。

编译器不仅允许它,而是在 C#规范 [ ^ ](参见第7.6.10节)

Quote:

7.6.10.6匿名对象创建表达式

anonymous-object-creation-expression用于创建匿名类型的对象。

anonymous-object-creation-expression:

new anonymous-object-initializer

anonymous-object-initializer:

{member-declarator-listopt}

{member-declarator-list,}



@ phil.o对分配的重新排序有一个很好的观点,但我更倾向于代码生成使用...即不必删除最后的逗号从列表中。



即使一切正常也能正常工作已分配关系(根据规范:-))所以它也可能已包含在可扩展性中


这只会有意义,因为属性赋值只不过是键值对,以及任何kvp集合的基础(不仅仅是C#,而是任何使用kvp或map的更高语言)都是忽略任何空值的条目。


This may be an old and well-known thing but I just learned that I can write an extra comma in an object initializer and it's accepted by the compiler.

Consider the following example

namespace ConsoleApplication1 {
   class Program {
      static void Main(string[] args) {

         // Create a new object with an extra comma in the initializer
         CommaComa theThing = new CommaComa() { A = 1, }; // no error
      }
   }

   /// <summary>
   /// Some class
   /// </summary>
   public class CommaComa {
      /// <summary>
      /// A property
      /// </summary>

      public int A { get; set; }
      /// <summary>
      /// Another property
      /// </summary>
      public string B { get; set; }

      /// <summary>
      /// Some method
      /// </summary>
      public void Foo(int a, int b = 5) { }
   }
}


As you can see there's an extra comma after the assignment A = 1 but the code compiles just fine.

Now the question is (basically out of curiosity), in what kind of use-cases it would make sense to add the extra comma with nothing after it?

As far as I can see the same isn't allowed outside initializers. If this would be systematic I'd expect something like the following would also work

theThing.Foo( a: 1, ); // Gives Argument missing error

解决方案

Maybe it is done this way to allow developpers to reorder their assignations without having to bother to put a comma on each end of line except the last one.

Something like:

KarmaComa theThing = new KarmaComa() {
   A = 1,
   B = "Foo",
};


that could be easily refactored to:

KarmaComa theThing = new KarmaComa() {
   B = "Foo",
   A = 1,
};


by simply drag-and-dropping the line.

But this is just a guess.


It's not just allowed by the compiler, it's in the C# specification[^] (See Section 7.6.10)

Quote:

7.6.10.6 Anonymous object creation expressions
An anonymous-object-creation-expression is used to create an object of an anonymous type.
anonymous-object-creation-expression:
new anonymous-object-initializer
anonymous-object-initializer:
{ member-declarator-listopt }
{ member-declarator-list , }


@phil.o has a good point with the re-ordering of the assignments, but I tend more towards the code-generation use ... i.e. not having to remove the final comma from the list.

It works even if all possible properties have been assigned (as per the spec :-)) so it may also have been included for extensibility


It would only make sense, because the property assignments are nothing more than key value pairs, and the basis of any kvp collection (not just C#, but any higher language that uses kvp or maps) is to ignore any entries that are null value.


这篇关于对象初始化程序中允许使用额外的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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