简体   繁体   English

C#中的自定义自动属性

[英]Custom auto properties in C#

I have the following class with auto properties: 我有以下类与自动属性:

class Coordinates
{
    public Coordinates(int x, int y)
    {
        X = x * 10;
        Y = y * 10;
    }

    public int X { get; set; }

    public int Y { get; set; }
}

As you can see from the constructor I need the value to be multiplied by 10. Is there anyway to do it without removing autoproperties? 从构造函数中可以看出,我需要将值乘以10.无论如何都要在不删除autoproperties的情况下执行此操作吗?

I tried the following not thinking that it causes recursion and then everything goes fubar 我尝试了以下不认为它导致递归,然后一切都变得富有

public int X { get {return X;} set{ X *= 10;} }

I would like to assign values to X and Y multiplied by 10. 我想将值分配给X和Y乘以10。

Coordinates coords = new Coordinates(5, 6); // coords.X = 50 coords.Y = 60
coords.X = 7; // this gives 7 to X but I would like it to be 70.

In order to make setter working like that, you'll need to use backing field: 为了使setter像这样工作,你需要使用支持字段:

class Coordinates
{
    public Coordinates(int x, int y)
    {
        X = x;
        Y = y;
    }

    private int _x;
    public int X
    {
        get { return _x; }
        set { _x = value * 10; }
    }

    private int _y;
    public int Y
    {
        get { return _y; }
        set { _y = value * 10; }
    }
}

Given your example: 举个例子:

Coordinates coords = new Coordinates(5, 6); // coords.X = 50 coords.Y = 60
coords.X = 7; // this gives 70

However, I don't recommend you having such setter because it could lead to confusion. 但是,我不建议你有这样的二传手,因为它可能导致混乱。 It's better to have a dedicated method which will do such multiplication. 最好有一个专用的方法来进行这样的乘法运算。 In the end, your code will be more descriptive and intuitive. 最后,您的代码将更具描述性和直观性。

You get a recursion, because you again call the same property, which in turn calls the same property, which in turn calls the same property... you get the point. 你得到一个递归,因为你再次调用相同的属性,而后者又调用相同的属性,而后者又调用相同的属性......你明白了。

public int X { get {return X ;} set{ X *= 10;} } public int X {get {return X ;} set { X * = 10;}}

How does auto properties works ? 汽车物业如何运作?
Behind the scenes Properties are actually methods, which means they don't actually store data. 幕后属性实际上是方法,这意味着它们实际上并不存储数据。 So who saves the data ? 那么谁保存数据? AutoProperties generate private backend field to save the data. AutoProperties生成私有后端字段以保存数据。

So in the simple declaration of auto property 所以在简单的auto属性声明中

int X { get; set; }

The compiler translate it into something like that 编译器将其转换为类似的东西

private int <X>k__BackingField;

public int X
{
    [CompilerGenerated]
    get
    {
        return <X>k__BackingField;
    }
    [CompilerGenerated]
    set
    {
        <X>k__BackingField = value;
    }
}

So no matter if you use it as Auto Properties or simple property, they are the same. 因此,无论您将其用作自动属性还是简单属性,它们都是相同的。

Now, to answer you question, with paraphrasing, "How do i return the value multiply with 10" 现在,回答你的问题,用释义,“我如何将价值乘以10乘以”

You can solve it with using 2 ways: 1. By saving the data multiply by 10 (setter implementation) 2. By returning the data multiply by 10 (getter implementation) 您可以使用以下两种方式解决它:1。将数据乘以10(setter实现)2。将数据乘以10(getter实现)

I won't elavorate, which one you should use, because for this kind of simple scenario, both will be perfectly valid. 我不会详细说明你应该使用哪一个,因为对于这种简单的场景,两者都是完全有效的。 I would just say that some of the factors for the choice will be micro(micro micro micro) performence, true state storage. 我只想说,选择的一些因素将是微观(微观微观)性能,真正的状态存储。

Here is the setter implementation 这是setter实现

private int _x;
public int X
{
    get
    {
        return _x;
    }

    set
    {
        return _x*10;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM