how do i make a Property in C# readonly ?
I tried to do the C# Property
public string Title { get; }
public abstract string? Html { get; }
public int RatingCount => UserEmail.Count;
public decimal? AverageRating { get
{
decimal sum = UserEmail.Values.Sum();
return RatingCount > 0 ? (sum / RatingCount) : null;
}}
private List<Comment> _comment { get; } = new();
public IReadOnlyList<Comment> Comments => _comment.AsReadOnly();
private readonly Dictionary<string, int> UserEmail = new();
public Post(User user, string title)
{
uesr = user;
Title = title;
}
this is a descrption for what happend
>Solution :
Look at this example, I hope it solves your question:
public class MyClass
{
private readonly int _myField;
public MyClass(int myField)
{
_myField = myField;
}
public int GetMyField()
{
return _myField;
}
}