English 中文(简体)
抽象财产是否会造成私人支持?
原标题:Does an abstract property create a private backing field?

简单问题:抽象财产是否会造成私人支持? 例:

public abstract Name { get; set; }

这是否会造成私人支持? 我想强迫产生这种财产的阶层利用自己的支持领域,而不是由汇编者创造的。

最佳回答

无。 我刚刚用以下几类测试:

public abstract class Class1
{
    public abstract string TestStringAbstract { get; set; }

    public string TestString { get; set; }
}

http://www.red-gate.com/products/reflector/“rel=“noreferer”>。 这是产生的法典:

public abstract class Class1
{
    // Fields
    [CompilerGenerated]
    private string <TestString>k__BackingField;

    // Methods
    protected Class1()
    {
    }

    // Properties
    public string TestString
    {
        [CompilerGenerated]
        get
        {
            return this.<TestString>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            this.<TestString>k__BackingField = value;
        }
    }

    public abstract string TestStringAbstract { get; set; }
}

你们只能看到为具体财产创造了一个单一的支持领域。 抽象的定义留待了。

这样做是合乎逻辑的,因为任何儿童阶层都必须凌驾于财产之上,因此,在创造支持领域时,没有任何东西可以进入(因为你可能永远无法获得抽象的财产)。

另一方面,virtual 财产将形成一个支持领域,任何以自动执行的替代方式推翻财产的类别都将在该类别中形成自己的支持领域。

问题回答

自抽象以来,班级执行人必须执行财产。 如果执行者宣布这样做,那么,它拥有一个自动财产,一个隐藏的成员持有实际价值。

:

public abstract string Name { get; set; }

以及

public string Name { get; set; }

第一份财产申报没有创造后盾。 它只是产生一种抽象的财产(如接口方法申报),而这种财产必须由任何非故意的继承类别来执行。

The second declaration is an auto-property, which DOES create a backing field. It s actually compiler syntactic sugar shorth以及 for:

private string _name;
public string Name { get { return _name; } set { _name = value; } }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签