0

Code:- (Note:- here I'm using read-only word means that property has only get accessor.)

Class Test
{
    public List<string> list {get;}
    public string name{get;}

    public Test ()
    {
       list =new List<string>();
    }
}

Main()
{
    Test test =new Test();
    test.list.add("c#"); //no error 
    test.name="Jhon"; //here I get compilation because property name is read-only
}

If you see above snippet. Test class contain two property which is name and list. In main method I'm creating object of test class to access these properties. So if you see if I try to set value to name property then I'll get compilation error because name property is read only. Similarly if you see another property 'list' that is also read-only if I use add property of List class then without error I'm able to add in list. So I'm not getting how this happen.

Tigran
  • 61,654
  • 8
  • 86
  • 123
Beginner
  • 69
  • 2
  • 9

4 Answers4

0

That's because the set would refer to setting the List object, the actual instance of the collection. The List itself, when returned, is not readonly. If you want it to be readonly, you can do something like:

private List<string> list;

public ReadOnlyCollection<string> List {get => list.AsReadOnly()}
V0ldek
  • 9,623
  • 1
  • 26
  • 57
0

You have a misunderstanding of how a "read only" property would work.

If your code looked like this:

Test test = new Test();
test.list.Add("c#"); //no error because you are not 'setting' the object
test.list = new List<string>(); //Error here because you ARE setting the object

Add() is just a method of a List<T>, you are modifying the object not setting the property to something else.

If you want your collection to be "read only" you can use the ReadOnlyCollection interface. You can manage a private list internally and only expose through the public ReadOnlyCollection. Your desired functionality was never made clear so I wouldn't know what to suggest beyond what I have.

maccettura
  • 10,514
  • 3
  • 28
  • 35
0

This is because in case of a string you return a copy of the instance - you can not assign to it.

Why .NET String is immutable?

In case of a List<T> you return a reference to an instance, which is not constant in your case - it is possible to change it.

To prove that yourself, you can do something like :

class Test 
{
    private string val; 
    public ref string Val {get {return ref val;}}
}


void Main()
{
    Test t = new Test();
    t.Val = "a";

    Console.WriteLine("t.Val is - " + t.Val);
}

Observe special ref keyword I used in string property, to denote that string reference has to be returned and not a copy of it.

C# Concepts: Value vs Reference Types (Joseph Albahari)

Tigran
  • 61,654
  • 8
  • 86
  • 123
0
public List<string> list {get;}

That means, it causes the error if you do the same action with name.

test.list = new List<string>();

test.list to get the list object and you call the method Add of the list object. So it's normal.

Antoine V
  • 6,998
  • 2
  • 11
  • 34