Appendix F Method Declarations
This appendix provides information about method declarations. A property procedure includes a pair of methods, which are also described here.
Methods
In C# all methods must be inside a class. The syntax for creating a method is as follows.
«attributes» «accessibility» «modifiers» return_type name(«parameters»)
{
code...
}
The following list describes the pieces of this declaration.
- attributes—Attributes that specify extra properties for the method.
- accessibility—One of
public
,internal
,protected
,internal protected
, orprivate
. - modifiers—One of
new
(hides a parent class’s version of the method),static
(shared by all instances of the class),virtual
(the method can be overridden in a descendant class),override
(the method is overriding a version in an ancestor class),sealed
(indicates an overriding method cannot be further overridden),abstract
(defines only the method’s signature), orextern
(the method is defined outside of the assembly). - return_type—The type of the data that the method returns. If the method doesn’t return a value, this should be
void
. - name—The name you want the method to have.
- parameters—The method’s parameters.
- code—The code that the method should execute.
Use a return
statement to exit the method and return a value (if the method returns a value).
Property Procedures
Properties have get
and set
accessors. The syntax for creating a read/write property is as follows.
public data_type name
{
get
{
...
return value; } set ...
Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.