在属性返回数组如果get访问设定值 [英] Returning Array in Property If Setting Value in Get Accessor

查看:167
本文介绍了在属性返回数组如果get访问设定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人只读浮法[] _location;公众播放器()
{
    _location =新的浮动[3];
}公众持股量[]地址
{
    得到
    {
        _location [0] = Worker.Elements [lbXCoordinate]值。
        _location [1] = Worker.Elements [lbYCoordinate]值。
        _location [2] = Worker.Elements [lbZCoordinate]值。        返回_location;
    }
}

两个问题:


  1. 我经常发现自己设置在获得的访问器(如上所示),而不是使用的设置的访问器属性的值。这样,我保证,每当有人问价值,他们得到的更新版本。是通常OK办?


  2. 我认识到,在属性返回一个数组是不是好的做法(<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx\"相对=nofollow>链接),但我担心,如果我返回一个ReadOnlyCollection还的话,我不得不做出这样的和浮动每次有人要求更新值(见下文两者的新实例) 。它会被要求每秒多次在我的程序,所以这似乎是不明智的与性能有关。什么情况下可以忽略我,那么这个规则,因为该值实际设置用户每次需要它(所以我们不会有这样的人恰好覆盖阵列的一个问题)?


使用ReadOnlyCollection还:

 私人ReadOnlyCollection还&LT;浮动&GT; _位置;公众播放器()
{
}公共ReadOnlyCollection还&LT;浮动&GT;位置
{
    得到
    {
        _location =新ReadOnlyCollection还&LT;浮动&GT;(新的浮动[] {Worker.Elements [lbXCoordinate]值,Worker.Elements [lbXCoordinate]值,Worker.Elements [lbXCoordinate]值。});        返回_location;
    }
}


解决方案

您可以返回 IReadOnlyCollection&LT;浮动方式&gt; 而不是,因为数组实现了这个接口

这是有效的code:

 私人_location [] =新的浮动[3];公共IReadOnlyCollection&LT;浮动&GT;位置
{
    得到
    {
        返回_location;
    }
}

对于在吸气值的设定,这似乎确定对我来说,因为开销可能是最小的。

您只需要确保在不同的线程访问 Worker.Elements

的情况下,一些同步

要确保一致的值返回,你可以在 Worker.Elements 同步,只要你尝试读取或写入这个变量:

  GET
{
   锁定(Worker.Elements)
   {
      _location [0] = Worker.Elements [lbXCoordinate]值。
      _location [1] = Worker.Elements [lbYCoordinate]值。
      _location [2] = Worker.Elements [lbZCoordinate]值。      返回_location;
   }
}

private readonly float[] _location;

public Player()
{
    _location = new float[3];
}

public float[] Location
{
    get
    {
        _location[0] = Worker.Elements["lbXCoordinate"].Value;
        _location[1]= Worker.Elements["lbYCoordinate"].Value;
        _location[2] = Worker.Elements["lbZCoordinate"].Value;

        return _location;
    }
}

Two questions:

  1. I often find myself setting the value of a Property in the get Accessor (as seen above), as opposed to using a set Accessor. This way I ensure that whenever someone asks for the value, they get an updated version. Is that generally OK to do?

  2. I realize that returning an Array in a Property is not good practice (link), but I am concerned that if I return a ReadOnlyCollection, then I have to make a new instance of both that and a float every time someone asks for an updated value (see below). It will be asked for several times per second in my program, so that seems to be unwise with regards to performance. Is it okay to just ignore this rule in my case, as the value is actually set every time a user asks for it (so we won't have an issue in the case someone happens to overwrite the array)?

Using ReadOnlyCollection:

private ReadOnlyCollection<float> _location;

public Player()
{
}

public ReadOnlyCollection<float> Location
{
    get
    {
        _location = new ReadOnlyCollection<float>(new float[] {Worker.Elements["lbXCoordinate"].Value, Worker.Elements["lbXCoordinate"].Value, Worker.Elements["lbXCoordinate"].Value});

        return _location;
    }
}

解决方案

You can return a IReadOnlyCollection<float> instead, because the array implements this interface.

This is valid code:

private  _location[] = new float[3];

public IReadOnlyCollection<float> Location
{
    get
    {
        return _location;
    }
}

Regarding the setting of the value in the getter, this seems ok to me, as the overhead is likely minimal.

You just need to ensure some synchronization in case of different threads accessing Worker.Elements.

To ensure consistent values returned, you can sync on Worker.Elements whenever you try to read or write this variable:

get
{
   lock(Worker.Elements)
   {
      _location[0] = Worker.Elements["lbXCoordinate"].Value;
      _location[1]= Worker.Elements["lbYCoordinate"].Value;
      _location[2] = Worker.Elements["lbZCoordinate"].Value;

      return _location;
   }       
}

这篇关于在属性返回数组如果get访问设定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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