Print
Fields And Properties

Fields and properties are used to store and control access to data (integers, strings, etc.) inside a class.

Fields store the values, and by default are "protected", they can only be accessed from methods inside the class (or a subclass). You can declare a field as "public", however (see example code below).

Properties are similar to fields, except that you define a getter and a setter method that are called when one tries to retrieve the value of the property or set the value of the property. This is useful for example if you want to check the value before setting it, or making a read-only property. The default access level of properties is "public".

Here is some code demonstrating different kinds of fields and properties:

Readonly and Constant Fields

Use the "final" keyword to create a field that cannot be modified. This is similar to "readonly" in C#. The value of the field is set only once when a class is instantiated (the constructor is called). If you mark a field "static final", then the value is hard-coded at compile time. This is similar to "const" in C#.

More Info

To better understand properties and fields in boo, you may find it helpful to search for information on properties and fields in C# and Visual Basic .NET, such as these resources:

Powered by Atlassian Confluence