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.

1624 questions
1687
votes
30 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
  • 343,444
  • 107
  • 203
  • 205
657
votes
16 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
  • 9,899
  • 8
  • 31
  • 39
523
votes
30 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
  • 17,155
  • 7
  • 30
  • 28
379
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
  • 188,729
  • 360
  • 878
  • 1,366
263
votes
17 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
  • 4,217
  • 6
  • 34
  • 40
210
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
  • 35,751
  • 21
  • 71
  • 94
189
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
  • 20,737
  • 24
  • 82
  • 111
186
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
  • 10,013
  • 10
  • 70
  • 116
157
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 Ooms
  • 31,998
  • 35
  • 134
  • 207
140
votes
8 answers

mysql - How to grant read only permissions to a user?

I have a mysql user, whom I want to grant all the READ permission on a db schema. One way is this : GRANT SELECT, SHOW_VIEW ON test.* TO 'readuser'@'%'; Is there a way to group all read operations in grant ?
Ajeet Ganga
  • 8,353
  • 10
  • 56
  • 79
140
votes
7 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,658
  • 8
  • 43
  • 62
136
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
  • 47,787
  • 16
  • 124
  • 167
134
votes
7 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
  • 94,807
  • 60
  • 220
  • 330
128
votes
9 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
  • 9,178
  • 9
  • 55
  • 88
126
votes
10 answers

When should an attribute be private and made a read-only property?

I don't know when an attribute should be private and whether I should use property. I read recently that setters and getters are not pythonic but using the property decorator is OK. But what if I have attribute, that mustn't be set from outside of…
Rafał Łużyński
  • 7,083
  • 5
  • 26
  • 38
1
2 3
99 100