Changes

Jump to: navigation, search

C Sharp Object Oriented Programming

1,278 bytes added, 17:46, 18 January 2008
Static, Readonly and Const Data Members
Now that we have looked at method class members the next task is to look at two special class methods, the consructor and finalizers.
== Static, Readonly Read-only and Const Data Members == In addition to the types of data member types we have looked at so far, C# also provides support for a number of additional member types. C# ''static'' member types (also referred to as ''class properties'') are used to store data values which are common to all object instances of class. For example, all bank customers would likely earn the same rate of interest on a savings account. An ''interestRate'' member would, therefore, be declared as static since it is common across all object instances of the class. Static members are declared using the ''static'' keyword. For example: <pre>public class BankAccount{ public static int interestRate;}</pre> Static members are accessed through the class, not though the object. For example we would change the ''interestRate'' member for all object instances by reference the BankAccount class as follows: <pre>BankAccount.interestRate = 10;</pre> For data members that must not be modified the ''cont'' and ''readonly'' keywords are provided by C#. Both achieve the same objective of preventing the value assigned to a data member from being changed after it has been declared. The value of a ''const'' or ''readonly'' member musty be assigned at creation time: <pre>public readonly daysInWeek = 7;</pre>
== C# Constructors and Finalizers ==

Navigation menu