C
C#Sandy Stone

❔ Difference between using readonly or { get; }?

The only difference I can think of is that if we use { get; } then we are converting from a field to a property; but I'm not so sure about this...
LPeter1997
LPeter1997308d ago
Essentially yes. readonly is a field modifier, while { get; } is a get-only property. The major difference usually comes in API. An interface can't declare a readonly field, but it can declare a get-only prop for example that the derived classes can implement however they want. They can be properties with a backing field or computed properties, ... So your only major difference essentially is API
Sandy Stone
Sandy Stone308d ago
Alright, thanks!
sibber
sibber308d ago
{ grt; } is a readonly property which generates a readonly backing field it's convention to use properties target than fields for public things
basically, i am little cat
basically, i am little cat308d ago
You can also have:
get {
return Random.Shared.Next(0, 64);
}
get {
return Random.Shared.Next(0, 64);
}
Which won't be quite read-only
sibber
sibber308d ago
thats is also a readonly property
basically, i am little cat
basically, i am little cat308d ago
yeah, but its compiled to just get_Whatever method
sibber
sibber308d ago
yeah but its a property one that you cant set so, readonly property
basically, i am little cat
basically, i am little cat308d ago
well its not same as readonly field
sibber
sibber308d ago
yes because its not
basically, i am little cat
basically, i am little cat308d ago
yeah that was the point
Accord
Accord307d ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Avoid infinite recursion in STJ convertersThere are so-so many cases where I only want to customize reading up a type in System.Text.Json, but❔ how to export project to .exeHey I have a project I'm trying to export to .exe. I see a lot of videos but their layout looks diff❔ How to access a button from a different class?Hello. Recently, I was developing a project and decided to start splitting my code into separate fil✅ Trouble importing LoggerService namespace with ILoggerService in .NET Core APICould someone help me with this error? I can't import the LoggerService namespace no matter what I t✅ What's the easiest type of information storage?I'm new to C# and when I was starting out with Python, the first type of data storage I learned was Unable to create an object of type 'DBCSDBContext'. For the different patterns supported at design tHey I've developed an application using Entity Framework Core and PostgreSQL, and I've encountered a✅ Listview colorHow do i put a different color for a specific item of a listview?❔ Blazor TypeConverter Exception```csharp [TypeConverter(typeof(IListStringConverter))] public List<string> Accepted✅ Docker + Blazor + EF Core 7 + SQLite = __EFMigrationsHistory doesnt existHello everyone i am new to docker and i am attempting to containerize an existing project. I've got ✅ What is the best way to setup app configuration using IHostBuilder?I want to make a good building of my host configuration and make it convenient to use. My goal is to❔ ef migration merges 2 tables of same type?<:kekw:710973294886649956> so my dbcontext that has two seperate tables of "Char" classes results ❔ Problem with converting WAV byte[] to MP3 byte[] (and vice versa)I have this code (using NAudio) for converting a wav byte array to an mp3 byte array: ```csharp ❔ Contact us validation for MVC projectHi Mates, I have prebuilt Contact Us page in Bootstrap, But I want to use this page for user contact❔ Get only values when deserialize jsonHi, it's possible to deserialize something like this with only values? ```csharp "genres": { "A✅ Crazy C# BeahviourThis is a very basic problem that it's getting me mad. Since it doesn't work in this case but on an Error idk how to fix```csharp public class AppData : ObservableObject { private string accountToken; How to create different examples of the code that behave the same way?I want to create different examples of codes from my code, but in different styles and structure. I ✅ Serializing a dictonary without path in root objectHello, I'm trying to create a DTO for an API, where the request takes a dictornary of values, but th✅ deltek maconomy package c#hello, I am doing a c# package for deltek maconomy so i need and access user name and password for t❔ How to get a random enum value?I have a function here where I would like to get a random value from an enum type. The goal is to gi