Issue when using only a getter and issue with List in c#
-
28-04-2021 - |
Frage
I'm working on an application that uses a list to handle previous guesses from the user. Below is the (private) list and the property for accessing the list.
To prevent privacy leak i'm using a ReadOnlyCollection, which is also the reason for me only having a getter in the property (elements are added inside the class directly to the list, not through the property).
Now to the problem. The code below generates to error message:
- TheNameOfTheClass.PreviousGuesses.get must declare a body because it is not marked abstract or extern. Automatically implemented properties must define both get and set accessors.
- Cannot implicity convert type 'System.Collections.Generic.List' to 'System.Collections.ObjectModel.ReadOnlyCollection.
How can this be solved? Thanks in advance!
private List<int> _previousGuesses;
public ReadOnlyCollection<int> PreviousGuesses {
get {
return _previousGuesses;
}
}
EDIT: Ok, so problem nr 2 is solved (thanks Zortkun!). What about the first problem, that I can't use only a getter, any ideas?
Lösung
I guess the favor is not to post the first thing that pops up on google but how about this?
return _previousGuesses.AsReadOnly();
From here
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow