Search code examples
c#visual-studio-2008.net-3.5collectionview

C# NET 3.5 : Property or indexer cannot be assigned to “--” it is read only


I have a readonly property:

public CollectionView View { get; }

and I am trying to initialize it from the class constructor:

public MyClass()
{
   this.View = ......
}

But the error described in the title is appearing. I am using NET 3.5 on Visual Studio 2008. I remember that in later versions of .NET and Visual Studio a readonly property could be initialized/assigned only from the constructor. Is it not possible in .NET 3.5? If not, how can I do this in .NET 3.5? I mean, I want a readonly property and only be assigned once in the constructor.


Solution

  • In C# 3.0 they didn't add special handling for read-only autoproperties so you must do it the old way:

    public class MyClass {
        private readonly CollectionView _View;
        public CollectionView View { get { return _View; } }
    
        public MyClass() {
            this._View = ...;
        }
    }