This chapter is divided into three main parts. The first part provides an overview of the different kind of members a class can contain. The second part focuses on data members and, in particular, static instance variables. The third part zoomed in on the parts of the method construct we haven't yet discussed in previous parts of the book. These are reference and output parameters, parameter arrays, method overloading and the this keyword.

Some of the most important points discussed in this chapter are as follows.

The class construct can contain members of three different categories data members, function members, and nested types. Data members represent the data of the class, function members contain blocks with series of statements that are executed in a sequential fashion, and a nested type can be one of the class, enum, struct, and interface types.

An instance variable is associated with a specific object. A static variable is associated with a class. static variables allow us to represent data that are associated with a class (and, therefore, associated with all class objects as a group) in a memory- and execution-efficient manner.

Constant members are, by default, static.

Methods can be divided into two main groups instance methods that are executed in relation to a particular object and static methods that are associated with the class in which they've been defined.

A static method can be called in three different ways. If called from outside the class in which it resides, it can be called either through an object or through the classname. If called from within the class in which it resides, it is called through the static method's name alone.

A method should be declared static:

  • When the method does not belong to any particular object

  • To access a static member variable

If you find that a large portion of the methods in your program are static, your overall design might be flawed.

You can declare a formal parameter of a method to be a reference parameter. The argument passed to a reference parameter and the reference parameter itself represent the same value, so any changes made to a reference parameter during the execution of its method are reflected in the argument.

An output parameter is the same as a reference parameter with one difference, the output parameter accepts uninitialized variables.

If a formal parameter is declared to be a parameter array with the params keyword, it accepts a varying number of arguments and will store those values in a dedicated array that can be accessed, like a conventional array, during the execution of the method.

Method overloading allows us to define two (or more) methods inside the same class with the same name. If two methods are to be correctly overloaded, they must have the same name and at least one of the following two points must be true:

  • The number of formal parameters must be different between the two methods.

  • At least one of the formal parameter types of one method must be different from the corresponding formal parameter of the other method.

When many methods are overloaded and contain the same name, they must all differ from each other as described in the two points.

The method signature is an important concept related to method overloading. The method signature consists of the name of the method and the sequence and number of the formal parameter types. The return type and the formal parameter names are not part of the signature.

Method overloading is frequently used to create methods with the same name that perform similar operations but on different data types.

If no direct match is found between the types of the arguments in a method call and a set of overloaded methods, the compiler will use implicit conversion paths to find a close match.

When the keyword this is positioned inside a method, it references the object instance in which the method resides. The keyword this is often used in the following three scenarios:

  • To differentiate formal parameters and local variables from instance variables

  • To pass the reference of the current object as an argument to another method

  • To return the reference of the current object back to the caller of the method


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: