Questions tagged [readonly]

Read-only is a generic concept that means "not writeable" - Please DO NOT USE THIS TAG. This tag is ambiguous and needs to be broken up.

This tag is ambiguous.

Please consider using (or creating) alternate tags for the specific use-cases of this term.

  • Read-only is a generic concept that means "not writeable"

  • readonly is a language keyword in C# that makes a member immutable.

  • readonly is an HTML attribute that makes input elements non-editable.

1505 questions
1480
votes
31 answers

What is the difference between const and readonly in C#?

What is the difference between const and readonly in C#? When would you use one over the other?
readonly
  • 306,152
  • 101
  • 198
  • 201
515
votes
15 answers

Declare a const array

Is it possible to write something similar to the following? public const string[] Titles = { "German", "Spanish", "Corrects", "Wrongs" };
Jaime Oro
  • 8,369
  • 8
  • 29
  • 38
470
votes
26 answers

In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

In a Django form, how do I make a field read-only (or disabled)? When the form is being used to create a new entry, all fields should be enabled - but when the record is in update mode some fields need to be read-only. For example, when creating a…
X10
  • 14,635
  • 7
  • 27
  • 26
332
votes
14 answers

What are the benefits to marking a field as `readonly` in C#?

What are the benefits of having a member variable declared as read only? Is it just protecting against someone changing its value during the lifecycle of the class or does using this keyword result in any speed or efficiency improvements?
leora
  • 163,579
  • 332
  • 834
  • 1,328
232
votes
13 answers

Why can't radio buttons be "readonly"?

I would like to show a radio button, have its value submitted, but depending on the circumstances, have it not editable. Disabled doesn't work, because it doesn't submit the value (or does it?), and it grays out the radio button. Read-only is really…
mcv
  • 3,825
  • 4
  • 31
  • 39
196
votes
4 answers

Why isn't String.Empty a constant?

In .Net why is String.Empty read only instead of a constant? I'm just wondering if anyone knows what the reasoning was behind that decision.
travis
  • 33,392
  • 20
  • 68
  • 93
187
votes
15 answers

Is there a read-only generic dictionary available in .NET?

I'm returning a reference to a dictionary in my read only property. How do I prevent consumers from changing my data? If this were an IList I could simply return it AsReadOnly. Is there something similar I can do with a dictionary? Private…
Rob Sobers
  • 19,173
  • 22
  • 78
  • 107
143
votes
4 answers

Github: readonly access to a private repo

I am developing some private projects on Github, and I would like to add nightly cronjobs to my deployments servers to pull the latest version from github. I am currently doing this by generating keypairs on every deployment server and adding the…
Jeroen
  • 28,942
  • 33
  • 116
  • 190
130
votes
6 answers

Pushing read-only GUI properties back into ViewModel

I want to write a ViewModel that always knows the current state of some read-only dependency properties from the View. Specifically, my GUI contains a FlowDocumentPageViewer, which displays one page at a time from a FlowDocument.…
Joe White
  • 87,312
  • 52
  • 206
  • 320
127
votes
2 answers

Docker, mount volumes as readonly

I am working with Docker, and I want to mount a dynamic folder that changes a lot (so I would not have to make a Docker image for each execution, which would be too costly), but I want that folder to be read-only. Changing the folder owner to…
Mustafa
  • 9,075
  • 7
  • 62
  • 109
123
votes
13 answers

Why does C# disallow readonly local variables?

Having a friendly debate with a co-worker about this. We have some thoughts about this, but wondering what the SO crowd thinks about this?
Brian Genisio
  • 46,643
  • 16
  • 121
  • 163
122
votes
2 answers

How to make Entity Framework Data Context Readonly

I need to expose an Entity Framework Data Context to 3rd party plugins. The purpose is to allow these plugins to fetch data only and not to let them issue inserts, updates or deletes or any other database modification commands. Hence how can I make…
Harindaka
  • 4,060
  • 6
  • 35
  • 56
121
votes
8 answers

Can I change a private readonly field in C# using reflection?

I am wondering, since a lot of things can be done using reflection, can I change a private readonly field after the constructor completed its execution? (note: just curiosity) public class Foo { private readonly int bar; public Foo(int num) { …
Ron Klein
  • 8,650
  • 9
  • 50
  • 83
114
votes
10 answers

Python read-only property

I don't know when attribute should be private and if I should use property. I read recently that setters and getters are not pythonic and I should use property decorator. It's ok. But what if I have attribute, that mustn't be set from outside of…
Rafał Łużyński
  • 5,889
  • 5
  • 23
  • 35
96
votes
7 answers

Readonly Properties in Objective-C?

I have declared a readonly property in my interface as such: @property (readonly, nonatomic, copy) NSString* eventDomain; Maybe I'm misunderstanding properties, but I thought that when you declare it as readonly, you can use the generated setter…
Alex
  • 58,815
  • 45
  • 146
  • 176
1
2 3
99 100