The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
Because if you're providing an interface, you provide a contract with the people who use that interface. If I write a library, I can use const to tell a user when they can trust my code not to change their stuff.
No longer a Microsoft MVP, but still happy to answer your questions.
public void DoSomething(const Employee emp)
emp.Age = 32; // This would not compile.
An object is passed by reference, so its properties are settable. You don't want to make the Age property read only, because normally, you want the consumer to be able to set it. But what if DoSomething was meant to be a final validation, and you didn't want anything in the object to change, because it could mess up state elsewhere? The const keyword would allow the developer to leave a property or field writable, but still be able to restrict when it could be written to.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
Ok, I think I get it.
So, I would be able to call methods and get properties and fields, but forbidden from setting fields and properties.
Well, there are still the methods to change the data, so I guess that optimization-wise, this doesn't help.
You say that VB.NET has this feature today? How does it work when you import a VB.NET library to another language, like C#?
"Computer Science is no more about computers than astronomy is about telescopes." - Edsger Dijkstra
1. one of main differences between high-level languages and assembly language is that they introduces many ways to limit the programmer
2. const keyword is not only a limitation it is also a reminder for you and for others that there is a reason why something should not be changed. And const is certainly a better solution than running around the office saying 'promise me that you will not try to change data returned by SomeLongAndCrypticFunctionName'
[My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
Last Visit: 18-Sep-20 17:27 Last Update: 18-Sep-20 17:27