Question

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?

Was it helpful?

Solution

No, but this idea is being tracked on Connect.

OTHER TIPS

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 in traditional immutable functional style of programming -- execution is deferred, queries are represented by immutable monads, expression trees are immutable, and so on.

And yet at the same time object initializers, collection initializers and auto props all encourage a traditional mutable-component-based style of programming. It seems like we are pushing in both directions -- which is indicative of the nature of C#; it's a pragmatic programming language that supports many different styles of programming.

However, since we are all big fans of the immutable style of programming, and since we believe that this style will pay dividends in making it easier to ensure the correctness of massively multithreaded applications in future ubiquitous multi-core architectures, we're definitely interested in figuring out some way to tame the mutability we've introduced. Readonly autoprops are one obvious way to do that; a small step, but a good one.

That all said, we have not even shipped C# 4 yet, and have not announced that there will be any new language features after that. You should treat all my musings about hypothetical features of unannounced products as "for entertainment purposes only" speculations, not as promises or announcements.

No, unfortunately not. I would very much like the feature, which could look like this:

public readonly string Name { get; }

or (slightly oddly)

public readonly string Name { get; readonly set; }

This would be converted into something like:

private readonly string <>_Name;

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

The twist is that setter calls would be allowed - but only within the constructor. Such calls would be converted directly into assignments to the backing field.

I would dearly, dearly love such a feature. Maybe for C# 5...

readonly can only be applied to fields, so I believe not.

Could you not just use:

public readonly MyType MyProperty;

as it is then only assignable from the constructor anyway?

No, there's no way you can do it. In fact, I don't see why would you want to get a value of a property which has not been set previously. For obvious reasons, you cannot set the value if there's neither set accessor, nor backing field.

That's really convoluted mate.

Just make it a public readonly field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top