C# and immutability and readonly fields… a lie?

You’ve asked like five questions in there. I’ll answer the first one: Having all readonly fields in a class does not necessarily make that class’s instance immutable because there are “ways” to change the readonly field values even after construction. How? Is it possible to change a readonly field after construction? Yes, if you are … Read more

Do we have a Readonly field in java (which is set-able within the scope of the class itself)?

Create a class with public final fields. Provide constructor where those fields would be initialized. This way your class will be immutable, but you won’t have an overhead on accessing the values from the outside. For example: public class ShortCalendar { public final int year, month, day; public ShortCalendar(Calendar calendar) { if (null == calendar) … Read more

How do I set a readonly field in an initialize method that gets called from the constructor?

Rob’s answer is the way to do it, in my book. If you need to initialize multiple fields you can do it using out parameters: public class Class { private readonly int readonlyField1; private readonly int readonlyField2; public Class() { Init(out readonlyField1, out readonlyField2); } protected virtual void Init(out int field1, out int field2) { … Read more

Conditionally make input field readonly in Angular 2 or 4: Advice + Best/which way to do it

You need to use the following (Angular 4): <input [readonly]=”isReadOnly”> If you use att.readonly then the input will always be read-only because the readonly attribute will be present even if its value is false. By using [readonly] Angular will only place the attribute if isReadOnly is true. In HTML, the following is sufficient to cause … Read more

Read-Only properties

If you want a “read-only” stored property, use private(set): private(set) var isEquilateral = false If it is a property calculated from other properties, then, yes, use computed property: var isEquilateral: Bool { return a == b && b == c } For the sake of completeness, and probably needless to say, if it is a … Read more

Write-Only pointer type

I’d probably write a tiny wrapper class for each: template <class T> class read_only { T volatile *addr; public: read_only(int address) : addr((T *)address) {} operator T() volatile const { return *addr; } }; template <class T> class write_only { T volatile *addr; public: write_only(int address) : addr ((T *)address) {} // chaining not allowed … Read more

Why Tuple’s items are ReadOnly?

Tuples originated in functional programming. In (purely) functional programming, everything is immutable by design – a certain variable only has a single definition at all times, as in mathematics. The .NET designers wisely followed the same principle when integrating the functional style into C#/.NET, despite it ultimately being a primarily imperative (hybrid?) language. Note: Though … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)