C
C#6d ago
Faker

✅ readonly keyword vs const keyword and use-case

Hello guys, I just read a bit about the readonly keywords. From what I've read, using the readonly keyword, we can only assign a particular value to a variable either at declaration level or when using the constructor. In constrast with the const keyword, I think we can only assign a value to it in the declaration part. I read that using const means the value is determined at compile-time itself while using readonly means value is determined at run-time. My first question concerning the readonly keyword: When we declare a property, consider the following:
C#
public struct Customer
{

public string Name { get; }
public int Price { get; }

// Readonly member
public readonly string Product { get; }

public Customer(string name, string product, int price)
{

this.Name = name;
this.Product = product;
this.Price = price;
}
}
C#
public struct Customer
{

public string Name { get; }
public int Price { get; }

// Readonly member
public readonly string Product { get; }

public Customer(string name, string product, int price)
{

this.Name = name;
this.Product = product;
this.Price = price;
}
}
My question is, if we omit the readonly keyword and just use { get; } without the set keyword, does the property behave as readonly? Second question is, what are the use cases of readonly vs const keyword please
20 Replies
𓀠𓀒𓀡𓀬
Yes caps
ero
ero6d ago
readonly is not valid for properties
Faker
FakerOP6d ago
yeah just read that we use readonly for fields while {get ; } for properties which are "read-only" ?
𓀠𓀒𓀡𓀬
{ get; } is readonly
ero
ero6d ago
basically
Faker
FakerOP6d ago
yep I see what about the use cases of readonly fields vs const fields please
𓀠𓀒𓀡𓀬
readonly is runtime and not static const is compile time and static you have to use static readonly to make it static
Pobiega
Pobiega6d ago
const behaves like static readonly, but as said, its a compile time constant if a value never changes and is known at compile time, use const if you can you can't have const arrays etc, so there are cases where static readonly is needed non-static readonly is almost only used for stuff passed in via the constructor
ero
ero6d ago
well, unless it's a null array x3
MODiX
MODiX6d ago
ero
REPL Result: Success
const int[] arr = null;
const int[] arr = null;
Compile: 194.036ms | Execution: 16.977ms | React with ❌ to remove this embed.
Pobiega
Pobiega6d ago
I have never needed one of those :p
Faker
FakerOP6d ago
yep I see Noted
ero
ero6d ago
(but this is useless and you shouldn't really use it...)
Faker
FakerOP6d ago
So to summarise: If we know a value is going to be constant at compile itme, use const else use readonly. The difference is that const is for the entire class itself (static readonly) while readonly can differs for each instances. Now, const can only be use for value type (well I guess there are exceptions like the null array) so if we want to make a reference type "constant"; we need to use readonly keyword in this case?
Pobiega
Pobiega6d ago
yeah, and the value of the reference type can still be mutated readonly only applies to the variable itself, not the thing it contains
Faker
FakerOP6d ago
yep I see
ero
ero6d ago
for example you could make collections readonly a lot of the time
class C
{
private readonly List<int> _someList = [];
}
class C
{
private readonly List<int> _someList = [];
}
each instance of C would have a new _someList but the list will never need to be re-assigned
Faker
FakerOP6d ago
yep, but we can add items to it though
ero
ero6d ago
you can just call .Clear() to empty the values of the list or add and remove, exactly
Faker
FakerOP6d ago
yep 💯 It clearer now, thanks guys, really appreciate

Did you find this page helpful?