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

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

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

      了解 C# 字段初始化要求

      Understanding C# field initialization requirements(了解 C# 字段初始化要求)
        <legend id='fscez'><style id='fscez'><dir id='fscez'><q id='fscez'></q></dir></style></legend>

        <tfoot id='fscez'></tfoot>

              <tbody id='fscez'></tbody>
              • <bdo id='fscez'></bdo><ul id='fscez'></ul>

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

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

                本文介绍了了解 C# 字段初始化要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                考虑以下代码:

                public class Progressor
                {
                    private IProgress<int> progress = new Progress<int>(OnProgress);
                
                    private void OnProgress(int value)
                    {
                        //whatever
                    }
                }
                

                这会在编译时出现以下错误:

                This gives the following error on compilation:

                字段初始值设定项不能引用非静态字段、方法或属性Progressor.OnProgress(int)"

                A field initializer cannot reference the non-static field, method, or property 'Progressor.OnProgress(int)'

                我理解它所抱怨的限制,但我不明白为什么这是一个问题,但是可以在构造函数中初始化该字段,而不是如下:

                I understand the restriction it is complaining about, but I don't understand why it is an issue, but the field can be initialized in the constructor instead as follows:

                public class Progressor
                {
                    private IProgress<int> progress;
                
                    public Progressor()
                    {
                         progress =  new Progress<int>(OnProgress);
                    }
                
                    private void OnProgress(int value)
                    {
                        //whatever
                    }
                }
                

                C# 中需要此限制的字段初始化与构造函数初始化有什么区别?

                What is the difference in C# regarding the field initialization vs constructor initialization that requires this restriction?

                推荐答案

                字段初始化在基类构造函数调用之前,所以它不是一个有效的对象.此时,任何以 this 作为参数的方法调用都会导致无法验证的代码,如果不允许使用无法验证的代码,则会抛出 VerificationException.例如:在安全透明代码中.

                Field initialization come before base class constructor call, so it is not a valid object. Any method call with this as argument at this point leads to unverifiable code and throws a VerificationException if unverifiable code is not allowed. For example: in security transparent code.

                • 10.11.2 实例变量初始化器
                  当实例构造函数没有构造函数初始值设定项,或者它具有 base(...) 形式的构造函数初始值设定项时,该构造函数隐式执行由在其类中声明的实例字段的变量初始值设定项指定的初始化.这对应于在进入构造函数时和直接基类构造函数的隐式调用之前立即执行的一系列赋值.变量初始化器按照它们在类声明中出现的文本顺序执行.
                • 10.11.3 构造函数执行
                  变量初始化器被转换为赋值语句,并且这些赋值语句在基类实例构造函数的调用之前执行.这种排序可确保所有实例字段在执行任何有权访问该实例的语句之前由其变量初始化程序初始化.

                这篇关于了解 C# 字段初始化要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <legend id='c0x46'><style id='c0x46'><dir id='c0x46'><q id='c0x46'></q></dir></style></legend>
                    <tbody id='c0x46'></tbody>

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

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

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