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

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

    1. <legend id='W5y3h'><style id='W5y3h'><dir id='W5y3h'><q id='W5y3h'></q></dir></style></legend>

      1. <tfoot id='W5y3h'></tfoot>

        为什么 C# 编译器不阻止属性引用自己?

        Why doesn#39;t the C# compiler stop properties from referring to themselves?(为什么 C# 编译器不阻止属性引用自己?)
          <bdo id='HKJDn'></bdo><ul id='HKJDn'></ul>
            <tbody id='HKJDn'></tbody>
          <i id='HKJDn'><tr id='HKJDn'><dt id='HKJDn'><q id='HKJDn'><span id='HKJDn'><b id='HKJDn'><form id='HKJDn'><ins id='HKJDn'></ins><ul id='HKJDn'></ul><sub id='HKJDn'></sub></form><legend id='HKJDn'></legend><bdo id='HKJDn'><pre id='HKJDn'><center id='HKJDn'></center></pre></bdo></b><th id='HKJDn'></th></span></q></dt></tr></i><div id='HKJDn'><tfoot id='HKJDn'></tfoot><dl id='HKJDn'><fieldset id='HKJDn'></fieldset></dl></div>
        • <tfoot id='HKJDn'></tfoot>
            <legend id='HKJDn'><style id='HKJDn'><dir id='HKJDn'><q id='HKJDn'></q></dir></style></legend>

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

                  本文介绍了为什么 C# 编译器不阻止属性引用自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我这样做,我会得到一个 System.StackOverflowException:

                  If I do this I get a System.StackOverflowException:

                  private string abc = "";
                  public string Abc
                  {
                      get
                      { 
                          return Abc; // Note the mistaken capitalization
                      }
                  }
                  

                  我明白为什么 - 属性引用自身,导致无限循环.(查看之前的问题这里和这里).

                  I understand why -- the property is referencing itself, leading to an infinite loop. (See previous questions here and here).

                  我想知道(以及我在之前的问题中没有看到答案)是为什么 C# 编译器没有发现这个错误?它会检查一些其他类型的循环引用(类从自身继承等),对吗?只是这个错误不够普遍,不值得检查吗?或者是否有一些我没有想到的情况,当您希望属性以这种方式实际引用自身时?

                  What I'm wondering (and what I didn't see answered in those previous questions) is why doesn't the C# compiler catch this mistake? It checks for some other kinds of circular reference (classes inheriting from themselves, etc.), right? Is it just that this mistake wasn't common enough to be worth checking for? Or is there some situation I'm not thinking of, when you'd want a property to actually reference itself in this way?

                  推荐答案

                  可以看官方"最后评论中的原因 这里.

                  You can see the "official" reason in the last comment here.

                  Microsoft 于 2008 年 11 月 14 日发布于19:52

                  Posted by Microsoft on 14/11/2008 at 19:52

                  感谢您的建议视觉工作室!

                  Thanks for the suggestion for Visual Studio!

                  你说得对,我们可以很容易地检测属性递归,但我们不能保证什么都没有有用的正在完成递归.财产的主体可以在您的对象上设置其他字段这会改变下一个的行为递归,可以改变它的行为基于来自控制台的用户输入,甚至可以基于不同的行为关于随机值.在这些情况下,一个自递归属性确实可以终止递归,但我们有无法确定是否是这种情况在编译时(不解决停止问题!).

                  You are right that we could easily detect property recursion, but we can't guarantee that there is nothing useful being accomplished by the recursion. The body of the property could set other fields on your object which change the behavior of the next recursion, could change its behavior based on user input from the console, or could even behave differently based on random values. In these cases, a self-recursive property could indeed terminate the recursion, but we have no way to determine if that's the case at compile-time (without solving the halting problem!).

                  由于上述原因(以及需要做出重大改变不允许这样做),我们将无法禁止自递归属性.

                  For the reasons above (and the breaking change it would take to disallow this), we wouldn't be able to prohibit self-recursive properties.

                  亚历克斯·特纳

                  项目经理

                  Visual C# 编译器

                  Visual C# Compiler

                  这篇关于为什么 C# 编译器不阻止属性引用自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 列表框上的取消选择)

                    <legend id='7oxmJ'><style id='7oxmJ'><dir id='7oxmJ'><q id='7oxmJ'></q></dir></style></legend>

                          • <bdo id='7oxmJ'></bdo><ul id='7oxmJ'></ul>
                          • <tfoot id='7oxmJ'></tfoot>

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

                            <small id='7oxmJ'></small><noframes id='7oxmJ'>