readonly

const vs. readonly

为君一笑 提交于 2019-11-30 13:34:19
Today I found an article where a const field is called compile-time constant while a readonly field is called runtime constant . The two phrases come from 《Effective C#》. I searched in MSDN and the language spec, find nothing about runtime constant . No offensive but I don't think runtime constant is a proper phrase. private readonly string foo = "bar"; creates a variable named "foo", whose value is "bar", and the value is readonly, here it is a variable, no business on constant . A readonly variable is still a variable, it can't be a constant. Variable and constant are mutually exclusive.

SQLAlchemy - how to map against a read-only (or calculated) property

让人想犯罪 __ 提交于 2019-11-30 09:49:14
I'm trying to figure out how to map against a simple read-only property and have that property fire when I save to the database. A contrived example should make this more clear. First, a simple table: meta = MetaData() foo_table = Table('foo', meta, Column('id', String(3), primary_key=True), Column('description', String(64), nullable=False), Column('calculated_value', Integer, nullable=False), ) What I want to do is set up a class with a read-only property that will insert into the calculated_value column for me when I call session.commit()... import datetime def Foo(object): def __init__(self

Is there a way to make readonly (not just private) automatic properties?

心已入冬 提交于 2019-11-30 09:15:13
Automatic properties let me replace this code: private MyType myProperty; public MyType MyProperty { get { return myPropertyField; } } with this code: public MyType MyProperty { get; private set; } with a few changes here and there - but is there a way to replace this code: private readonly MyType myProperty; public MyType MyProperty { get { return myPropertyField; } } with something similar? No, but this idea is being tracked on Connect . Indeed, there is no way to do this at present. We realize that in C# 3 we produced a bit of a philosophical oxymoron. The design of LINQ is heavily steeped

WPF UserControl expose ActualWidth

浪子不回头ぞ 提交于 2019-11-30 08:49:15
问题 How do I expose the ActualWidth property of one of the components of my user control to users? I have found plenty of examples of how to expose a normal property by creating a new dependency property and binding, but none on how to expose a read-only property like ActualWidth . 回答1: What you need is a ReadOnly dependency property. The first thing you need to do is to tap into the change notification of the ActualWidthProperty dependency on the control that you need to expose. You can do that

How can a readonly static field be null?

爷,独闯天下 提交于 2019-11-30 08:11:08
So here's an excerpt from one of my classes: [ThreadStatic] readonly static private AccountManager _instance = new AccountManager(); private AccountManager() { } static public AccountManager Instance { get { return _instance; } } As you can see, it's a singleton-per-thread - i.e. the instance is marked with the ThreadStatic attribute. The instance is also instantiated as part of static construction. So that being the case, how is it possible that I'm getting a NullReferenceException in my ASP.NET MVC application when I try to use the Instance property? Quoting MSDN ThreadStaticAttribute : Do

Html.TextBox conditional attribute with ASP.NET MVC Preview 5

旧城冷巷雨未停 提交于 2019-11-30 07:56:48
I have a strongly-typed MVC View Control which is responsible for the UI where users can create and edit Client items. I'd like them to be able to define the ClientId on creation, but not edit, and this to be reflected in the UI. To this end, I have the following line: <%= Html.TextBox("Client.ClientId", ViewData.Model.ClientId, new { @readonly = (ViewData.Model.ClientId != null && ViewData.Model.ClientId.Length > 0 ? "readonly" : "false") } ) %> It seems that no matter what value I give the readonly attribute (even "false" and ""), Firefox and IE7 make the input read-only, which is annoyingly

How does protobuf-net handle readonly fields?

回眸只為那壹抹淺笑 提交于 2019-11-30 04:45:38
问题 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

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

Deadly 提交于 2019-11-30 04:29:12
问题 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

How to make a reference type property “readonly”

大城市里の小女人 提交于 2019-11-30 03:53:56
I have a class Bar with a private field containing the reference type Foo . I would like to expose Foo in a public property, but I do not want the consumers of the property to be able to alter Foo ... It should however be alterable internally by Bar , i.e. I can't make the field readonly . So what I would like is: private _Foo; public Foo { get { return readonly _Foo; } } ...which is of course not valid. I could just return a clone of Foo (assumming that it is IClonable ), but this is not obvious to the consumer. Should I change the name of the property to FooCopy ?? Should it be a

variable that can't be modified

末鹿安然 提交于 2019-11-30 01:25:40
问题 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? 回答1: 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 =