C# readonly vs Get
31
Are there any differences between the readonly modifier and get-only properties?
Example:
public class GetOnly
{
public string MyProp { get; }
}
public class ReadOnly
{
public readonly string MyProp;
}
Bonus: is there a way to make an interface that works with both? (to use with generics)
public interface ISomething
{
public string MyProp { get; }
}
public class GetOnly : ISomething
{
public string MyProp { get; }
}
public class ReadOnly : ISomething // Cannot implement
{
public readonly string MyProp;
}
Many thanks in advance!