0

In the property vs. field debate, one point that always comes up is "Changing a variable to a property is a breaking change."

Consider I have an application which uses a library. In the library I have an interger field "Car.MaxSpeed", which I change to a property. If I make the next build of the application, the code is still referring to "Car.MaxSpeed". So in places where this is used, the application will still receive an int. The code does not need to be changed to access a property instead of a field.

I know that this would mess with reflection being used in the application, but reflection is always listed as a separate point in the property vs. field debate.

So what does actually "break"?

EDIT: This was marked as a duplicate of another question, so it asks me to edit it. I think it's quite clear that the (I think less then stellar) answer "Properties can throw any arbitrary exceptions" does not answer my question. Heck, even if you only read the titles of these 2 questions you should notice a difference.

0

1 Answer 1

2

Breaking change means that there is going to be a need to recompile the project.

You probably have a setup where your class library project is referenced in the application project through Visual Studio project reference. With this setup you will not be able to see the breaking change, because as soon as you build your console app, it will pick the change from the class library, as the dependent project has to build as well.

To see the actual breaking change. Do the following steps:

  1. Create a class library
  2. Define a class with a public field(variable) in it.
  3. Reference the class library in a console app
  4. In the console app, use the public field defined in the class library.
  5. (Important) At this point, open the folder containing the console app, go to the bin/debug folder and copy all the files to a separate location on your drive.
  6. Now go back to the class library in Visual Studio, modify the field and change it to property. Build the class library.
  7. (Important) Open the folder containing class library project, go to debug/bin folder, and copy the resulting dll file.
  8. Copy the dll file and paste it in the folder created in step 5.
  9. Try running the app.

With these steps you will see the following error:

Unhandled Exception: System.MissingFieldException: Field not found: //field name

Now this is a breaking change. Try the opposite process. Create a property and then change it to a field. See how it behaves.

The exception you will get:

Unhandled Exception: System.MissingMethodException: Method not found:

1
  • I understand why that would happen, but I do not understand why anybody would do that. So this lead to a follow-up question. Commented May 15, 2017 at 10:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.