| ||||||
| ||||||

ClassGoals:
Overview
C# code is primarily organized into classes. A class contains both the data and operations
needed to implement a type. For example, if a drawing program needed the concept of
'rectangle' then all the code needed to implement the rectangle type would be collected into
a Class
A class represents a concept in the application domain. Programmers working on a graphics
program might have classes such as
A class is defined using the keyword
FieldsA class can contain fields to store data. Fields are defined using the type name and the field name. Fields defined in this way are called "instance fields" or "instance variables" since each instance of the class gets it own copy. Note that fields are defined at class scope; that is, they are not defined inside a method.
Object creation
Objects of class type are created using the
Member access
The dot operator is used to access class members. Instance members must be accessed using
an object so the syntax is typically
Methods
The operations supported by a class are defined using methods. For example, a rectangle
type might support operations such as
The set of methods offered by a stock class might look something like the following.
Methods are invoked by applying the dot operator to an object, selecting the desired method by name, and enclosing the arguments in parentheses.
Methods typically need access to the fields of the class. For example, the rectangle
Each time an instance method is invoked, a particular object must be specified. For
example, a method call such as
Method implementations use
In the previous example, the method parameter and a field were both named
Methods may return a value. To return a value, the method declaration must specify
the type of data returned and the method implementation must use the keyword
The entire implementation of the stock class is shown below for reference.
Method overloading
It is legal for a class to offer more than one method with the same name as long as
the parameter lists are different. For example, the stock class could offer multiple
versions of the
Parameter passing
There are three primary ways to pass parameters to methods: value, A value parameter can be thought of as "in only"; that is, the data passed is copied into the method and the method then operates on the local copy. Any changes made by the method modify only the local copy and are lost when the method ends. Pass by value is the default parameter passing mechanism so there is no keyword needed to specify its use.
An out parameter is "out only"; that is, no data is passed into the method but
an assignment to the parameter is visible in the client code. The keyword
A
Member access
C# provides the ability to control access to class members. The three most common
access levels are
The class designer can choose to limit the usage of a field or method by adding the
keyword
Private is the default access level, so the following two declarations are equivalent.
To allow client code to access a field or method, the member can be made public by adding
the keyword
The public/private split allows a class to be divided into interface and implementation. The implementation is kept private while the interface is made public. Client code can only access the public interface and is prevented from directly accessing the implementation. This concept is often described using the terms 'encapsulation' and 'information hiding'. The most commonly cited advantage of this coding style is that the class designer is free to modify the implementation at any time without breaking any client code. This is possible since the client code cannot access the implementation directly anyway so changes have no impact. Naming conventionsSome naming conventions have been adopted by C# developers in order to increase the uniformity of code written by different programmers in different organizations. Most names use the "intercaps" convention where the first letter of each word is capitalized. Class names are intercaps with an initial capital letter. Method names are intercaps with an initial capital letter. Method parameters are intercaps with an initial lower case letter. Public fields are intercaps with an initial capital letter. Private fields are intercaps with an initial lower case letter.
This material is excerpted from the Programming C# course offered by DevelopMentor. |