Changes

Jump to: navigation, search

C Sharp Object Oriented Programming

15 bytes removed, 15:46, 1 April 2009
no edit summary
This is the key to what is called ''data encapsulation''. Object-oriented programming convention dictates that data should be encapsulated in the class and accessed and set only through the methods of the class (typically called ''getters'' and ''setters'').
We can now extend our BankAccount class to add member variables to hold the account name and number. True to the concept of data encapsulation we will be making some of these creating members ''private'' and writing methods to access these values later:
<pre>
== Static, 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.
</pre>
It is also poossible possible to declare the object variable and assign the object in a single statement:
<pre>
The above code sets the account number using the ''setter'' method and then displays the account number using the ''getter'' method.
Now that we have looked at method class members the next task is to look at two special class methods, the consructor ''constructors'' and ''finalizers''.
== C# Constructors and Finalizers ==
Despite the grand sounding names, C# class constructors and finalizers are nothing more than mehtods methods which get called when an object is instantiated and destroyed. The constructor is particularly useful for allowing initialization values to be passed through to an object at creation time. Lets Let's say that we would like to be able to initialize the accountName and accountNumber members at the point that we initialize the custAccount object. To do so we need to declare a constructor.
Constructors are declared the same way as other methods with the exception that the name of the mentod method must match the class name:
<pre>
Finalizers are used to clean up any resources used by a class object when the object is destroyed. Unlike constructors which can be triggered from code using the ''new'' keyword there is no way to explicitly call a finalizer (for example there is no ''delete'' equivalent to the ''new'' keyword). Instead, the finalizer will be called when the ''garbage collector'' decides that the object instance is no longer needed. All the programmer can be sure of is that the finalizer will be called at some time between the when the object is no longer needed by the code and the point that the application terminates.
Finalizers are defined in the same way as constructors with the exeception exception that the name is receded preceded by a tilde (~):
<pre>

Navigation menu