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

Automatic properties allow me to replace this code:

private MyType myProperty; public MyType MyProperty { get { return myPropertyField; } } 

using this code:

 public MyType MyProperty { get; private set; } 

with some changes here and there - but is there any way to replace this code:

 private readonly MyType myProperty; public MyType MyProperty { get { return myPropertyField; } } 

with something like that?

+9
c # readonly
Oct 08 '09 at 9:35
source share
6 answers

No, but this idea is tracked in Connect .

+7
Oct 08 '09 at 9:40
source share

In fact, there is currently no way to do this.

We understand that in C # 3 we created some philosophical oxymoron. The LINQ design is heavily imbued with the traditional, unchanging functional programming style β€” execution is delayed, queries are represented by immutable monads, expression trees are immutable, and so on.

Yet at the same time, object initializers, collection initializers, and autoresists encourage a traditional programming style based on mutable components. It seems that we are moving in both directions, which indicates the nature of C #; it is a pragmatic programming language that supports many different programming styles.

However, since we are all big fans of the immutable programming style, and since we believe that this style will pay dividends to simplify the correctness of multi-threaded applications in future ubiquitous multi-core architectures, it is definitely interested in somehow taming the variability that we introduced . Only repetitive auto programs are one obvious way to do this; small step, but good.

That is all said, we have not yet sent C # 4, and have not announced that after this new language functions will appear. You should consider all my reflections on the hypothetical features of unannounced products as β€œfor entertainment purposes only” and not as promises or announcements.

+10
Oct 08 '09 at 14:52
source share

No, unfortunately, no. I really liked a feature that might look like this:

 public readonly string Name { get; } 

or (a little strange)

 public readonly string Name { get; readonly set; } 

This will be converted to something like:

 private readonly string <>_Name; public string Name { get { return <>_Name; } } 

The twist is that setter calls will be allowed, but only inside the constructor. Such calls will be converted directly to destinations in the support field.

I would be expensive, I really like this feature. Maybe for C # 5 ...

+5
Oct 08 '09 at 9:45
source share

readonly can only be applied to fields, so I don't believe it.

Could you just use:

 public readonly MyType MyProperty; 

since then it is only assigned to the constructor anyway?

+1
Oct 08 '09 at 9:39
source share

No, you cannot do this. Actually, I don’t understand why you want to get the value of a property that was not previously set. For obvious reasons, you cannot set the value if there is no set accessor, or support fields.

+1
Oct 08 '09 at 9:40
source share

This is a really confusing assistant.

Just make it a readonly public field.

-one
Oct 08 '09 at 9:42
source share



All Articles