It seems that VB.NET and C# readonly
keyword have some differences...
Say, a ReadOnly property in C# can be assigned in some conditions, but in VB.NET - never?
In practice, this means that a C or C++ program that wants to be portable has to avoid modifying constant strings. In general, the compiler will not allow you to modify the contents of of "const" variables, so you can consider "const" to mean "read only" in most cases.
The readonly keyword is a modifier that can be used in four contexts: In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class.
Read-only is a file attribute which only allows a user to view a file, restricting any writing to the file. Setting a file to “read-only” will still allow that file to be opened and read; however, changes such as deletions, overwrites, edits or name changes cannot be made.
The readonly keyword can be used to define a variable or an object as readable only. This means that the variable or object can be assigned a value at the class scope or in a constructor only. You cannot change the value or reassign a value to a readonly variable or object in any other method except the constructor.
In C#, readonly is a field modifier. It specifies that the field can be assigned to only on initialization or in the constructor.
VB.NET is the same, except that ReadOnly is also a property modifier. It specifies that the property cannot be assigned to - i.e., it is a getter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With