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

夙愿已清 提交于 2019-11-29 13:07:15

问题


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?


回答1:


No, but this idea is being tracked on Connect.




回答2:


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.




回答3:


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...




回答4:


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?




回答5:


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.




回答6:


That's really convoluted mate.

Just make it a public readonly field.



来源:https://stackoverflow.com/questions/1536691/is-there-a-way-to-make-readonly-not-just-private-automatic-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!