Is automatic read-only use possible?

I found a topic on MSDN that says yes, it is possible.

I did a test that seems to violate this statement:

using System; namespace Test { class Program { static void Main(string[] args) { Foo f = new Foo("1"); Console.WriteLine(f.Bar); // prints 1 f.Test("2"); Console.WriteLine(f.Bar);// successfully prints 2 } } class Foo { public Foo(string b) { this.Bar = b; } public string Bar { get; private set; } public void Test(string b) { // this would be impossible for readonly field! // next error would be occur: CS0191 or CS0191 // A readonly field cannot be assigned to (except in a constructor or a variable initializer) this.Bar = b; } } } 

Where am I mistaken?

+53
c # properties readonly msdn
Mar 19 '10 at 20:50
source share
7 answers

The answer below was written in 2010. In C # 6 (released in 2015), you can write automatically updated read-only properties:

 // This can only be assigned to in a constructor public int Foo { get; } 



You're absolutely right. Read-only properties that are automatically executed automatically are currently not possible. Making a private setter is not the same, regardless of what some books and MSDN can say :)

If I ruled the world, this is not so. When I see some language designers at NDC 2010 in June (please come!) I intend to try to persuade, bribe, persuade and generally make trouble until they agree. In the end, this is just one plate function.

Looking at this MSDN article, the text itself does not mean that it creates an automatic read-only property. It creates an immutable type using an automatic property, and rightly so. The only problem bits are comments saying

 // Read-only properties. 

... which are certainly wrong. Frame agrees with us:

 var prop = typeof(Contact).GetProperty("Name"); Console.WriteLine(prop.CanWrite); // Prints True 
+90
Mar 19 '10 at 20:51
source share

The property is read-only outside the Foo class. I think this article is coming.

But this is not the same as labeling a variable with the readonly keyword.

+8
Mar 19 '10 at 20:56
source share

This is confusing. You should distinguish read only to C # readonly (which means keyword).

  • read-only: they mean that no one from the outside can write directly to it, only read.
  • C # readonly : you can only write in the constructor, and then never again.
+6
Mar 19 '10 at 20:59
source share

No, it is not possible to make an automatically implemented readonly property. On the page you indicated:

with automatically implemented properties, both get and set accessor are required

The read-only property has a NO set accessor.

A property without a set accessory is considered read-only

+4
Mar 19 '10 at 21:54
source share

Private dialing does not match readonly .

Like methods or fields, the private keyword makes setter visibility available only to the class itself. Other objects cannot use the installer, but the methods of the class itself can call it free. Therefore, your test code compiles and works fine.

It appears to external objects as a readonly property, but it is not read-only in the true definition.

+1
Mar 19 '10 at 20:55
source share

The ReadOnly keyword, in C # and VB, does the same when applied to a field. They do this so that the field is assigned only during static initialization (if it is marked as a static / shared field) or during the constructor.

C # doesn't use the readonly keyword for anything else.

The ReadOnly keyword in VB has a different meaning when applied to Property. In this case, this simply means that there is no acceptable way to assign it to a public property (of course, the support field can be changed by other internal code, of course).

+1
Oct 14 '12 at 17:45
source share

Unable to create automatic readonly property. If you try to compile a class with an automatically implemented property, you will get this error if it does not have both get and set:

'ProjectName.ClassName.Property.get' must declare the body because it is not marked abstract or external. Automatically implemented properties should determine both get and set accessors.

With a sentence starting with "Automatically", which is part of the error we are associated with.

+1
Jul 03 '13 at 17:28
source share



All Articles