Posty

Wyświetlanie postów z marzec, 2018

[Part 2] Is c# safe? Readonly fields... Really readonly?

Hi Today I present you part 2 of "Is c# safe" series.  Few days ago I though about whether possibility to set value to readonly field outside the constructor. Quoting the Microsoft website: "The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. Example In this example, the value of the field year cannot be changed in the method ChangeYear, even though it is assigned a value in the class constructor..." As last time, I wrote unit tests to see if it is possible.  commit to my github Unfortunately Microsoft... Using reflection above sentence isn't true. I added new class named " InternalCallReadonlyVsConst " and it looks like below namespace InternalClasses { class InternalCallReadonlyVsConst { private readonly int privateR