Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadOnly in C# vs in VB.NET

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?

like image 972
serhio Avatar asked Mar 09 '12 10:03

serhio


People also ask

What is read only in C?

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.

What is the use of readonly?

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.

What is a readonly?

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.

What is a readonly method?

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.


1 Answers

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.

like image 82
RB. Avatar answered Sep 28 '22 06:09

RB.