How to add a ReadOnly property in C#?
Property - A property is an entity that describes the features of an object.
A property is a piece of data contained within a class that has an exposed interface
for reading/writing. Looking at that definition, you might think you could declare a
public variable in a class and call it a property. While this assumption is somewhat
valid, the true technical term for a public variable in a class is a field. The key
difference between a field and a property is in the inclusion of an interface.
We make use of Get and Set keywords while working with
properties. We prefix the variables used within this code block with an underscore.
Value is a keyword, that holds the value
which is being retrieved or set. See code below to set a property as ReadOnly. If
a property does not have a set accessor, it becomes a ReadOnly property.
Default Values of Types
Const - ReadOnly
Interface Reference, is, as
public class ClassA
private int length = 0;
public ClassA(int propVal)
length = propVal;
public int length
More Interview Questions...