1

I have a class with an internal struct that contains data specific to that class.

However i want other classes to be able to read the data as a read-only setup.

So i tried:

public class MyClass{
    private Data _data;
    public Data Data
    {
        get { return _data; } //expose the internal data as read only
    }

    internal struct Data
    {
        public int SomeData;
    }
}

This gave me the error:

 Inconsistent accessibility: property type

It doesn't make much sense for the Data struct to be outside of MyClass since it's only related to that class. Everything else just reads it.

What would be the correct way to do this?

WDUK
  • 1,412
  • 1
  • 12
  • 29
  • 3
    no that `get { return _data; }` does not guarantee readonly, imo.. it just make sure the pointer on `Data` field is **readonly**. also, the reason why you get `inconsistent accessibility` is due to the fact that the `struct Data` is `internal` (assembly-wide) while the `Data` field is public. there is no way for the other assembly to understand what is `Data` struct is. – Bagus Tesa May 17 '18 at 02:31
  • What would be the cleanest way to have Data restricted to `MyClass` but the data of it in `MyClass` readable to any class that has reference to `MyClass` then ? – WDUK May 17 '18 at 02:35
  • In your actual scenario, does `Data` really have just one `int` field, or does it have lots of fields? Why is `Data` a struct and not a class? – Michael Liu May 17 '18 at 02:46
  • It has about 15 i just simplified it for the question.I moved them all into a Data struct to make it cleaner but then needed to be able to read the values outside of the class so `internal` wasn't working. Didn't want to have Data exposed to other classes, since it gets messy and cluttered with things that i shouldn't be able to create. – WDUK May 17 '18 at 02:49
  • actually @WDUK, [immutability is pretty hard to achieve](https://stackoverflow.com/a/2724848/4648586). on most cases you could simply use a class that only allow read for external assemblies e.g. `public int SomeData{ get { return _someData; } internal set { _someData = value; }; }`. however that approach wont save you if you expose classes (e.g. `List`). – Bagus Tesa May 17 '18 at 03:01
  • Does this answer your question? [How can I make a read only version of a class?](https://stackoverflow.com/questions/2724731/how-can-i-make-a-read-only-version-of-a-class) – StayOnTarget Aug 12 '22 at 12:18

1 Answers1

3

Make your struct immutable and give that to the outside. Like this:

public class MyClass
{
    private Data _data;
    public Data DataToExpose
    {
        get { return _data; } //expose the internal data as read only
    }

    public struct Data
    {
        public Data(int someData)
        {
            this.SomeData = someData;
        }
        public int SomeData { get; }
    }
}

Now people can use the Data from outside but it will be a read-only. Both the reference and the fields are read-only. It is good practice to make structs immutable anyways.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64