<legend id='OrWeE'><style id='OrWeE'><dir id='OrWeE'><q id='OrWeE'></q></dir></style></legend><tfoot id='OrWeE'></tfoot>
  1. <small id='OrWeE'></small><noframes id='OrWeE'>

    <i id='OrWeE'><tr id='OrWeE'><dt id='OrWeE'><q id='OrWeE'><span id='OrWeE'><b id='OrWeE'><form id='OrWeE'><ins id='OrWeE'></ins><ul id='OrWeE'></ul><sub id='OrWeE'></sub></form><legend id='OrWeE'></legend><bdo id='OrWeE'><pre id='OrWeE'><center id='OrWeE'></center></pre></bdo></b><th id='OrWeE'></th></span></q></dt></tr></i><div id='OrWeE'><tfoot id='OrWeE'></tfoot><dl id='OrWeE'><fieldset id='OrWeE'></fieldset></dl></div>

      • <bdo id='OrWeE'></bdo><ul id='OrWeE'></ul>

      内联属性初始化和尾随逗号

      Inline property initialisation and trailing comma(内联属性初始化和尾随逗号)

          <bdo id='Uvd97'></bdo><ul id='Uvd97'></ul>

          1. <i id='Uvd97'><tr id='Uvd97'><dt id='Uvd97'><q id='Uvd97'><span id='Uvd97'><b id='Uvd97'><form id='Uvd97'><ins id='Uvd97'></ins><ul id='Uvd97'></ul><sub id='Uvd97'></sub></form><legend id='Uvd97'></legend><bdo id='Uvd97'><pre id='Uvd97'><center id='Uvd97'></center></pre></bdo></b><th id='Uvd97'></th></span></q></dt></tr></i><div id='Uvd97'><tfoot id='Uvd97'></tfoot><dl id='Uvd97'><fieldset id='Uvd97'></fieldset></dl></div>

            <small id='Uvd97'></small><noframes id='Uvd97'>

              <tbody id='Uvd97'></tbody>
            • <tfoot id='Uvd97'></tfoot>

              1. <legend id='Uvd97'><style id='Uvd97'><dir id='Uvd97'><q id='Uvd97'></q></dir></style></legend>
                本文介绍了内联属性初始化和尾随逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                void Main(){测试 t = 新测试{A = "一",B = "b",//<-- 错误的尾随逗号};}公开课测试{公共字符串 A { 获取;放;}公共字符串 B { 获取;放;}}

                我在我的代码中发现了很多上面的错字.我总是对编译器似乎并不关心这一点感到惊讶.为什么上面不是语法错误?它有什么实际有效的用途吗?

                解决方案

                我在我的代码中发现了很多上面的错字.我总是对编译器似乎并不关心这一点感到惊讶.为什么上面不是语法错误?

                因为设计 C# 语法语法的人足够聪明,可以从其他编程语言中吸取教训,不允许使用悬空的逗号,从而使那些语言的程序员不断受到刺激.p>

                例如,ECMAScript (JavaScript) 最初对这个问题保持沉默,因此一些实现(Firefox 中的 SpiderMonkey、Opera 的 JavaScript 等)自然而然地允许它们,而其他实现(微软的 JScript)则不允许.因此,这导致了这里和其他地方的一连串为什么这在 IE 中不起作用"的问题.(幸运的是,ECMAScript 5 明确允许它们,并且 IE8 最终在对象初始化器中支持它们——IE8 仍然以非标准方式处理数组初始化器,但公平地说,那些悬空的逗号也仅在 ECMAScript 5 中得到澄清.)

                您在 C# 语法的许多其他地方也发现了这一点,例如枚举和数组初始值设定项.

                void Main()
                {
                    Test t = new Test
                    {
                        A = "a",
                        B = "b", // <-- erroneous trailing comma
                    };
                }
                
                public class Test
                {
                    public string A { get; set; }
                    public string B { get; set; }
                }
                

                I find the above typo in my code quite a lot. I'm always suprised that the compiler doesn't seem to care about this. Why is the above not a syntax errror? Is there any actually valid use for it?

                解决方案

                I find the above typo in my code quite a lot. I'm always suprised that the compiler doesn't seem to care about this. Why is the above not a syntax errror?

                Because the people designing the C# syntax grammar were smart enough to learn the lessons from other programming languages which didn't allow the dangling comma, to the constant irritation of programmers in those languages.

                For example, ECMAScript (JavaScript) was silent on the issue initially, and so naturally some implementations (SpiderMonkey in Firefox, Opera's JavaScript, etc.) allowed them while others (Microsoft's JScript) didn't. And so this led to a spate of "why doesn't this work in IE" questions here and elsewhere. (Fortunately, ECMAScript 5 explicitly allows them, and IE8 finally supports them in object initializers -- IE8 still treats array initializers in a non-standard way, though to be fair the dangling comma for those was only clarified in ECMAScript 5, too.)

                You find this in lots of other places in the C# grammar too, like enums and array initializers.

                这篇关于内联属性初始化和尾随逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

              2. <i id='sCuwm'><tr id='sCuwm'><dt id='sCuwm'><q id='sCuwm'><span id='sCuwm'><b id='sCuwm'><form id='sCuwm'><ins id='sCuwm'></ins><ul id='sCuwm'></ul><sub id='sCuwm'></sub></form><legend id='sCuwm'></legend><bdo id='sCuwm'><pre id='sCuwm'><center id='sCuwm'></center></pre></bdo></b><th id='sCuwm'></th></span></q></dt></tr></i><div id='sCuwm'><tfoot id='sCuwm'></tfoot><dl id='sCuwm'><fieldset id='sCuwm'></fieldset></dl></div>
                  <tbody id='sCuwm'></tbody>

                1. <legend id='sCuwm'><style id='sCuwm'><dir id='sCuwm'><q id='sCuwm'></q></dir></style></legend>
                    <bdo id='sCuwm'></bdo><ul id='sCuwm'></ul>

                    <small id='sCuwm'></small><noframes id='sCuwm'>

                        <tfoot id='sCuwm'></tfoot>