Professional Documents
Culture Documents
Oop Reviewer
Oop Reviewer
Oop Reviewer
ANATOMY OF A METHOD
Methods defined inside classes
Group program statements
o Based on functionality
o Called on or more times
All programs consist of at least one method
o Main()
User-defined method
Required method
o public static void Main()
public static - Modifiers
void - Return type
Main - Method name
() - Parameters or arguments
Modifiers
o Appear in method headings
o Appear in the declaration heading for classes and other class members
o Indicate how it can be accessed
o Types of modifiers
Static
Access
o Static Modifier
Indicates member belongs to the type itself rather than to a specific object of a class
Main() must include static in heading
Members of the Math class are static
Public static double Pow(double, double)
Methods that use the static modifier are called class methods
Instance methods require an object
o Access Modifiers
public - no restrictions
protected - limited to the containing class or classes derived from the containing class
internal - limited to current project
protected internal - limited to current project or classes derived from class
private - limited to containing class
Return Type
o Indicates what type of value is returned when the method is completed
o Always listed immediately before method name
o void
No value being returned
o return statement
Required for all non-void methods
Compatible value
Method Names
o Follow the rules for creating an identifier
Pascal case style
Action verb or prepositional phrase
o Example
CalculateSalesTax()
AssignSectionNumber()
DisplayResults()
InputAge()
ConvertInputValue()
Parameters
o Supply unique data to method
o Appear inside parentheses
Include data type and an identifier
In method body, reference values using identifier name
Parameter refers to items appearing in the heading
Arguments for items appearing in the call
Formal parameters - (int milesTraveled, double gallonsUsed)
Actual arguments - (289, 12,2)
o Like return types, parameters are optional
Method Body
o Enclosed in curly braces
o Include statements ending in semicolons
Declare variables
Do arithmetic
Call other methods
o Value-returning methods must include return statement
o Predefined Methods
Extensive class library
Console class
Write() - overloaded
WriteLine() - overloaded
Read()
ReadLine()