简体   繁体   English

如何在类中设置没有设置的属性(只读属性)? 什么机制?

[英]How is a property without a set allowed (read-only property) is being set inside the class? What mechanism?

First of all, I'm NOT really familiar with C#, but I am with C++.首先,我不太熟悉 C#,但我熟悉 C++。 I didn't find any info about this.我没有找到任何有关此的信息。 Through what mechanism is a C# property is being set when it only have a get implemented and the set is hidden.通过什么机制设置 C# 属性,当它只实现了一个 get 并且该设置被隐藏时。 It's a read-only property that is being set continuously, inside a loop.它是一个只读属性,在循环内连续设置。

My hunch is that the get returns a reference which C# uses to set the value in this case.我的预感是 get 返回一个引用,在这种情况下,C# 使用该引用设置值。 Is that correct?那是对的吗?

(I've come across a code which I'm trying to understand) (我遇到了一个我试图理解的代码)

For example:例如:

public Series<double> Avg
{
    get { return Values[1]; }
}

public Series<double> Default
{
    get { return Values[0]; }
}

(Series is just an array of doubles, but accessing them from the end of the array, by index) (系列只是一个双精度数组,但从数组的末尾访问它们,通过索引)

The code in RSI does write the items of the array: RSI 中的代码确实写入了数组的项目:

Default[0]  = value0;
Avg[0]      = constant1 * value0 + constant2 * Avg[1];

So what is happening up there?那么上面发生了什么? Just to understand, it is something like the below C++ code?只是为了理解,它类似于下面的 C++ 代码?

double& Avg(int index)
{
    return Values[index+1];
}

Certainly, in C++ you would use Avg(1) instead of Avg[1] since it is a function, I'm just curious what the logic actually does in @RSI.cs.当然,在 C++ 中,你会使用Avg(1)而不是Avg[1]因为它是一个函数,我只是好奇@RSI.cs 中的逻辑实际上做了什么。 Is the get in this case is like a reference so it can also write values via the get or is the set is auto-implemented so writing and reading the above properties is accessing a completely different index or even variable?在这种情况下,get 是否类似于引用,因此它也可以通过 get 写入值,还是 set 是自动实现的,因此写入和读取上述属性是访问完全不同的索引甚至变量?

Are the above C# samples even a valid C# code?上面的 C# 示例甚至是有效的 C# 代码吗?

That means you can only read the value of the property but not write it from the outside.这意味着您只能读取属性的值,而不能从外部写入它。

Take a file class for example.以文件类为例。 It would have a property FileSize that you could read from the outside.它会有一个属性 FileSize ,你可以从外面读取它。 It would make no sense to have a Setter since setting the file size should not be possible (you have to modify the content of the file in order for the file size to change).拥有 Setter 是没有意义的,因为设置文件大小应该是不可能的(您必须修改文件的内容才能更改文件大小)。

You would then implement a read only property in the form of the following:然后,您将采用以下形式实现只读属性:

public long FileSize { get { return Content.Length; } }

You can compare a C# property to a set of C++ methods.您可以将 C# 属性与一组 C++ 方法进行比较。 A get would be a getProperty() method in your C++ class and a set would be a setProperty(value). get 将是 C++ 类中的 getProperty() 方法,而 set 将是 setProperty(value)。 AC# property is actually not very different from having a field and two methods. AC# 属性实际上与具有一个字段和两个方法没有太大区别。 The whole get and set is just a nice way to let the compiler generate this for you.整个 get 和 set 只是让编译器为您生成它的好方法。

This means that this is a property that you want it to be read from outside the object without modifying it, for example you can do this:这意味着这是一个您希望从对象外部读取它而不修改它的属性,例如您可以这样做:

public class MyObject
{
    private string _currentState;//This will be changed only from inside class
    public string MyCurrentState
    {
       get
       {
         return _currentState;
       }
    }
}

Since you are more familiar with c++, let me try to translate it somehow.既然你对c++比较熟悉,那么让我试着以某种方式翻译它。 Forgive me any detail errors because it's been some time since I used C++ on a regular basis.请原谅我的任何细节错误,因为我已经有一段时间没有定期使用 C++了。

template<class T> class Series
{
    // some template class definition

    // the index operator in c++
    T& operator [](int idx) 
    {
        return somePrivateTArray[idx];
    }
}

class UserOfSeries
{
    Series<double> Values[]; // = ... somewhere

public:
    Series<double>& Avg()
    {
        return Values[1];
    }

    Series<double>& Default()
    {
        return Values[0];
    }
}

When you use it, you will probably write当你使用它时,你可能会写

UserOfSeries* aUserOfSeries = getUserOfSeriesFromTheSystem();
Series<double>& theAvg = aUserOfSeries->Avg();
theAvg[4] = 12000.000;

You can set a property without a set.您可以设置没有设置的属性。 It only means you cannot access it from outside the class (visibility).这仅意味着您无法从课程外部访问它(可见性)。 Inside the class, the get acts as a reference as I write the values, so setting happens throughout the get if you will.在类内部,get 在我写入值时充当参考,因此如果您愿意,可以在整个 get 过程中进行设置。

It means that my original C++ example is correct but a more accurate one is (by Rotem) std::vector& GetAvg() { return Values[0];这意味着我原来的 C++ 示例是正确的,但更准确的是(由 Rotem) std::vector& GetAvg() { return Values[0]; } so writing happens like this: GetAvg()[0] = ...;所以写作是这样发生的:GetAvg()[0] = ...;

Thank you Oliver and Rotem for your effort in trying understanding my answer.感谢 Oliver 和 Rotem 努力理解我的答案。 I feel like this is a good and supporting community.我觉得这是一个很好的支持社区。 The ones who misunderstood my question as a visibility question were downvoting this though :-)那些将我的问题误解为可见性问题的人对此不以为然:-)

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

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