readonly

Assigning to static readonly field of base class

天涯浪子 提交于 2019-12-01 00:05:36
问题 public class ClassA { public static readonly string processName; } public class ClassB : ClassA { static ClassB() { processName = "MyProcess.exe"; } } I am getting an error while compiling the above C# code. The error says -- "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)" But I am assigning it in a static constructor. The need for such a static variable is that, the base class has methods that uses this variable, but the derived

Read-only array in .NET

半腔热情 提交于 2019-12-01 00:04:02
Arrays are a fast way to iterate through an unordered set of items, and it's often nice for them to be read-only. While exposing arrays with the `readonly' keyword is useless because the contents of the array can still be altered, a ReadOnlyCollection<T> wrapper solves this. The problem is it's 4 times slower than a plain array in tests I've done. (I know, returning a copy of the array would only take a performance hit once, but ideally I wouldn't want to waste CPU time on that either.) I have noticed I can get the benefit of a read-only sanity-check while preserving the performance of a plain

Restricting access to method calls on read-only properties

淺唱寂寞╮ 提交于 2019-11-30 23:54:30
I have a class that defines a read-only property that effectively exposes a private field, something like this: public class Container { private List<int> _myList; public List<int> MyList { get { return _myList;} } public Container() : base () { _myList = new List<int>(); } // some method that need to access _myList public SomeMethod(int x) { _myList.Add(x); } } now it's impossible for the consumer to manage my property directly, so code like aContainer.MyList = new List(); generates a compile-time error. However, the consumer is absolutely free to call all sorts of methods on the reference he

Java File.setWritable() and stopped working correctly after JDK 6u18

ε祈祈猫儿з 提交于 2019-11-30 22:24:22
We have a Java application with a particular module that checks if a temporary directory is 'writable' before executing its function. To test this, we have a JUnit test that creates a new directory, uses the Java File class method setWritable(false) to make the directory "not writable", then passes that directory to the module being tested and expects to get an IllegalArgumentException back. This had all been working fine for a long time under JDK 6u18. Today I have updated the JDK version to JDK 6u24 (the current release from the Sun site as of today). That unit test just started failing with

How does protobuf-net handle readonly fields?

情到浓时终转凉″ 提交于 2019-11-30 20:06:47
I use protobuf-net to serialize/deserialize my data. I have some rather simple classes, so that's no real problem. As far as I know, protobuf-net uses IL generation to create serialization/deserialization code. While I have readonly fields in my model, I wonder how is it possible to write to such a field with IL? I can plainly see it works well, but I don't know why... I've tried to spy it in the code, but it's a bit too complicated. My attempts to generate such code myself always result in IL validator errors. Actually, I can't get it to fail - at least, when generating in memory. Let's start

How to override a getter-only property with a setter in C#?

家住魔仙堡 提交于 2019-11-30 19:45:48
Update : This question has been revised to make it clearer. The answers below seem to reflect that this method works well. Hopefully this question can help people who need to add a get or set to an existing property. Ran into a problem today where I needed to override a base class's get -only property with both a get and set . Current consensus seems to be that this is impossible, but I think that I found a method. The general idea is to make a new property instead of directly override ing the old one, then we create a bridge method that override s the old get method with a call to the new one

Restricting access to method calls on read-only properties

♀尐吖头ヾ 提交于 2019-11-30 18:58:06
问题 I have a class that defines a read-only property that effectively exposes a private field, something like this: public class Container { private List<int> _myList; public List<int> MyList { get { return _myList;} } public Container() : base () { _myList = new List<int>(); } // some method that need to access _myList public SomeMethod(int x) { _myList.Add(x); } } now it's impossible for the consumer to manage my property directly, so code like aContainer.MyList = new List(); generates a

variable that can't be modified

北城以北 提交于 2019-11-30 17:39:45
Does C# allow a variable that can't be modified? It's like a const , but instead of having to assign it a value at declaration, the variable does not have any default value, but can only be assigned a value once at runtime (EDIT: and possibly not from constructor). or is this not possible? You could create your own generic class that provided this functionality, but that might be overkill. public class SetValueOnce<T> { public bool _set; private T _value; public SetValueOnce() { _value = default(T); _set = false; } public SetValueOnce(T value) { _value = value; _set = true; } public T Value {

Setting the Textbox read only property to true using JavaScript

别等时光非礼了梦想. 提交于 2019-11-30 17:28:43
How do you set the Textbox read only property to true or false using JavaScript in ASP.NET? Sridhar You can try document.getElementById("textboxid").readOnly = true; document.getElementById('textbox-id').readOnly=true should work Try This :- set Read Only False ( Editable TextBox) document.getElementById("txtID").readOnly=false; set Read Only true(Not Editable ) var v1=document.getElementById("txtID"); v1.setAttribute("readOnly","true"); This can work on IE and Firefox also. I find that document.getElementById('textbox-id').readOnly=true sometimes doesn't work reliably. Instead, try: document

Is a readonly field in C# thread safe?

拜拜、爱过 提交于 2019-11-30 16:26:31
问题 Is a readonly field in C# thread safe? public class Foo { private readonly int _someField; public Foo() { _someField = 0; } public Foo(int someField) { _someField = someField; } public void SomeMethod() { doSomething(_someField); } } Have gone through some posts: - What are the benefits to marking a field as readonly in C#? - JaredPar suggests that readonly fields once constructed are immutable and hence safe. - Readonly Fields and Thread Safety, suggests that there is some risk if