自动性质与结构不混合? [英] Automatic Properties and Structures Don't Mix?

查看:202
本文介绍了自动性质与结构不混合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

周围的一些小的结构踢在回答这个帖子,我遇到意外以下几点:



下面的结构,使用一个int字段是完全合法的:

 结构MYSTRUCT 
{
公共MYSTRUCT(INT大小)
{
this.Size =大小; //< - 法律分配。
}

公众诠释大小;
}



但是,下面的结构,使用自动属性不会编译:

 结构MYSTRUCT 
{
公共MYSTRUCT(INT大小)
{
本。大小=; //< - 编译时错误!
}

公众诠释大小{搞定;集;}
}



返回的的这个对象不能使用的错误之前所有字段被分配到。我知道,这是一个结构的标准程序:对于任何属性支持字段必须直接从结构的构造内进行分配(而不是通过属性的set访问)。



一个解决方法是使用一个明确的支持领域:

 结构MYSTRUCT 
{
公共MYSTRUCT(INT大小)
{
= _size大小;
}

私人诠释_size;

公众诠释大小
{
{返回_size; }
集合{_size =价值; }
}
}



(请注意,VB.NET不会有这种的问题,因为在VB.NET各个领域都将自动初始化为0 /空/假时,第一个创建的。)



这似乎是使用自动属性时,是一个不幸的限制在C#结构。从概念上思考,我想知道,这不会是一个合理的地方,那里是一个例外,它允许属性set访问到一个结构的构造函数中调用,至少在一个自动的财产?



这是一个小问题,几乎是一个边缘的情况下,但我不知道别人怎么想到这个问题...


解决方案

从C#6起:这不再是一个问题。






Becore C#6,您需要调用默认的构造函数这个工作:

 公共MYSTRUCT(INT大小):这个()
{
尺寸=尺寸;
}



这里的一个更大的问题是,你有一个可变的结构。这就是绝不会一个好主意。我将使它:

 公众诠释大小{搞定;私人集;} 



不是的技术上的不可变的,但足够接近

Kicking around some small structures while answering this post, I came across the following unexpectedly:

The following structure, using an int field is perfectly legal:

struct MyStruct
{ 
    public MyStruct ( int size ) 
    { 
        this.Size = size; // <-- Legal assignment.
    } 

    public int Size; 
}

However, the following structure, using an automatic property does not compile:

struct MyStruct
{ 
    public MyStruct ( int size ) 
    { 
        this.Size = size; // <-- Compile-Time Error!
    } 

    public int Size{get; set;}
}

The error returned is "The 'this' object cannot be used before all of its fields are assigned to". I know that this is standard procedure for a struct: the backing field for any property must be assigned directly (and not via the property's set accessor) from within the struct's constructor.

A solution is to use an explicit backing field:

struct MyStruct
{ 
    public MyStruct(int size)
    {
        _size = size;
    }

    private int _size;

    public int Size
    {
        get { return _size; }
        set { _size = value; }
    }
}

(Note that VB.NET would not have this issue, because in VB.NET all fields are automatically initialized to 0/null/false when first created.)

This would seem to be an unfortunate limitation when using automatic properties with structs in C#. Thinking conceptually, I was wondering if this wouldn't be a reasonable place for there to be an exception that allows the property set accessor to be called within a struct's constructor, at least for an automatic property?

This is a minor issue, almost an edge-case, but I was wondering what others thought about this...

解决方案

From C# 6 onward: this is no longer a problem


Becore C# 6, you need to call the default constructor for this to work:

public MyStruct(int size) : this()
{
    Size = size;
}

A bigger problem here is that you have a mutable struct. This is never a good idea. I would make it:

public int Size {get; private set;}

Not technically immutable, but close enough.

这篇关于自动性质与结构不混合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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