Is it a breaking change that modifying the access modifier of a public property?

UPDATE : This question was the topic of my blog in January 2012 Thanks for the great question!

UPDATE: This question was the topic of my blog in January 2012. Thanks for the great question! I assume that by "breaking change" you mean "when I recompile code that depended on this assembly, does code that used to compile still compile?"

By that definition, strictly speaking, yes, making a property setter public that used to be private is a breaking change. Suppose you have this code: // Assembly Alpha public class Charlie { public int P { get; private set; } } public class Delta { public int P { get; set; } } And then in another assembly that references Alpha: // Assembly Echo class Foxtrot { static void M(Action f) {} static void M(Action f) {} static void Golf() { M(y=>{y. P = 123;}); } } You compile assembly Echo.

Class Foxtrot has a method Golf which does overload resolution on M. M has two overloads; one that takes a action on Charlie and one that takes an action on Delta. If lambda parameter y is of type Charlie then the lambda body produces an accessibility error, and therefore that overload of M is not an applicable candidate.

Overload resolution chooses the second overload and compilation succeeds. Now you change assembly Alpha so that Charlie. P's setter is public.

You recompile Echo. Now you get an overload resolution error because both overloads of M are equally valid and neither is better than the other. Compilation of Echo fails due to the change in Alpha.

Your change to Alpha was a breaking change. The question is not "is this a breaking change? " It clearly is; almost every change is some kind of breaking change.

The question should be whether the breaking change will actually in practice break anyone, and if so, what is the cost of fixing the break compared to the benefit of the new feature?

" and she said "We do not say that word at this airline!" I tell you, getting a person on the phone at an airline is hard enough; getting a smart and funny one who knows the international radio alphabet to boot is a rarity indeed. – Eric Lippert Dec 20 '11 at 22:14 2 Would be funnier still if it was actually Delta you were calling.

– Jon Hanna Dec 21 '11 at 0:00 @EricLippert thanks for your detailed explanation. – mkus Dec 21 '11 at 15:20 @EricLippert I am very appreciated for your new blog post. Thank you Eric.

– mkus yesterday.

It is a breaking change, in that it may cause existing code to no longer compile. Some languages do not allow you to override a property without overriding all visible accessors. VB is such a language.

Say you have the following C# class: namespace TestLibrary public class A { public virtual int X { get { return 0; } private set { Console. WriteLine("A. Set_X called."); } } } } In a VB project that references your library, you have a class definition: Class B : Inherits TestLibrary.

A Public Overrides ReadOnly Property X As Integer Get Return MyBase. X End Get End Property End Class Notice the ReadOnly modifier on the property. This is required in VB, because you are only defining the getter.

If you leave it out, you get a compile-time error: Property without a 'ReadOnly' or 'WriteOnly' specifier must provide both a 'Get' and a 'Set'. Now, remove the private modifier from the setter in your C# class: namespace TestLibrary public class A { public virtual int X { get { return 0; } set { Console. WriteLine("A.

Set_X called. "); } } } } You will now get a compile-time error in your VB code: 'Public Overrides ReadOnly Property X As Integer' cannot override 'Public Overridable Property X As Integer' because they differ by 'ReadOnly' or 'WriteOnly'. The same code that references your library does not compile after you make the change, so it is a breaking change.

2 Good one! Inter-language breaks are some of the trickiest. – Eric Lippert Dec 21 '11 at 16:00 I really hate the way properties are implemented in .

Net; I what real advantage there is to having three types of properties, one of which includes two methods, rather than simply having property getters and setters be methods with some attribute that indicates that, for purposes of syntax, debugging display, serialization, etc.They should be regarded as properties? If it were possible for a routine to accept a parameter of type Property, I could see some real benefit to having the getter and setter bound together as a single entity, but it isn't. So what's the benefit?

– supercat Jan 10 at 23:28 @supercat That's exactly how properties work in . NET: IL never calls properties, only methods. Properties exist in metatada only.

The fact that the getter and setter methods aren't easily accessible is a matter of language design: they are public methods but hidden by most languages because they have a SpecialName attribute. Why C# explicitly prevents you from calling these methods, I don't know. – Jeffrey Sax 2 days ago @JeffreySax: One cannot just mark two independent methods and have languages regard them as a property getter and setter.

There must also be a defined "Property" which links the named property with the getter and setter method; a similar concept exists (equally uselessly, IMHO) for events. – supercat 2 days ago.

This depends on how strict you want to be about "breaking change". One definition is: does it break the build or execution of the referring code. Another is, can the other code determine that you made the change.By the first definition, this is not a breaking change.

By the second definition, it is a breaking change.

Eric's answer and Jeffrey's Answer offer edge cases where it will break compilation. – Brian Dec 20 '11 at 19:50.

It might be, if those other assemblies use reflection to test for the presence of the setter. But early-bound code will not break. Another thing to consider is whether this is a change in semantics.

If the property was formerly set only during construction, and now can be modified at any time, that definitely could break consumers who cached the value, used it as a Dictionary key, etc.

Since the visibility of the property is increased now there should not be any problems with statically linked assemblies and static references -- those assemblies just treated your property as read-only until now and they continue doing so. The only problem that might potentially arise here is if some assemblies use reflection to get information about your properties and perform some actions. For example, if this is a property of a buisiness object and you use data binding to show your data in some control, like GridView or ListView (independent from the actual framework), it might be that this property will be recognized as "updateable" now and the user might change some values you (and some other assemblies) considered immutable so far.

I don't think but you should know what is the wisdom behind to make this setter private. Access modifiers are important to library consumers and in encapsulation.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions