C# imposes definite assignment, which requires that all variables be assigned a value before they are used. In Example 4-7, if you don’t initializetheHour,theMinute, andtheSecondbefore you pass them as parameters toGetTime(), the compiler will complain. Yet, the initialization that is done merely sets their values to0before they are passed to the method:
int theHour = 0; int theMinute = 0; int theSecond = 0; t.GetTime( ref theHour, ref theMinute, ref theSecond);
It seems silly to initialize these values because you immediately pass them by reference intoGetTime where they’ll be changed, but if you don’t, the following compiler errors are reported:
Use of unassigned local variable 'theHour' Use of unassigned local variable 'theMinute' Use of unassigned local variable 'theSecond'
C# provides theoutparameter modifier for this situation. Theoutmodifier removes the requirement that a reference parameter be initialized. The parameters toGetTime(), for example, provide no information to the method; they are simply a mechanism for getting information out of it. Thus, by marking all three asoutparameters, you eliminate the need to initialize them outside the method. Within the called method, theoutparameters must be assigned a value before the method returns. The following are the altered parameter declarations forGetTime():
public void GetTime(out int h, out int m, out int s) { h = Hour; m = Minute; s = Second; }
And here is the new invocation of the method inMain():
t.GetTime( out theHour, out theMinute, out theSecond);
To summarize, value types are passed into methods by value.refparameters are used to pass value types into a method by reference. This allows you to retrieve their modified values in the calling method.outparameters are used only to return information from a method. Example 4-8 rewrites Example 4-7 to use all three.
Example 4-8. Using in, out, and ref parameters
#region Using directives
using System; using System.Collections.Generic; using System.Text;
#endregion
namespace InOutRef { public class Time { // private member variables private int Year; private int Month; private int Date; private int Hour; private int Minute; private int Second;
// public accessor methods public void DisplayCurrentTime() { System.Console.WriteLine( "{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second ); }
public int GetHour() { return Hour; }
public void SetTime( int hr, out int min, ref int sec ) { // if the passed in time is >= 30 // increment the minute and set second to 0 // otherwise leave both alone if ( sec >= 30 ) { Minute++; Second = 0; } Hour = hr; // set to value passed in
// pass the minute and second back out min = Minute; sec = Second; }
// constructor public Time( System.DateTime dt ) { Year = dt.Year; Month = dt.Month; Date = dt.Day; Hour = dt.Hour; Minute = dt.Minute; Second = dt.Second; } }
public class Tester { static void Main() { System.DateTime currentTime = System.DateTime.Now; Time t = new Time( currentTime ); t.DisplayCurrentTime();
int theHour = 3; int theMinute; int theSecond = 20;
t.SetTime( theHour, out theMinute, ref theSecond ); System.Console.WriteLine( "the Minute is now: {0} and {1} seconds", theMinute, theSecond );
theSecond = 40; t.SetTime( theHour, out theMinute, ref theSecond ); System.Console.WriteLine( "the Minute is now: " + "{0} and {1} seconds", theMinute, theSecond ); } } }
Output: 11/17/2007 14:6:24 the Minute is now: 6 and 24 seconds the Minute is now: 7 and 0 seconds
SetTimeis a bit contrived, but it illustrates the three types of parameters.theHouris passed in as a value parameter; its entire job is to set the member variableHour, and no value is returned using this parameter.
TherefparametertheSecondis used to set a value in the method. IftheSecondis greater than or equal to 30, the member variableSecond is reset to 0, and the member variableMinuteis incremented.
You must specifyrefon the call and the destination when using reference parameters.
Finally,theMinuteis passed into the method only to return the value of the member variableMinute, and thus is marked as anoutparameter.
It makes perfect sense thattheHourandtheSecondmust be initialized; their values are needed and used. It is not necessary to initializetheMinute, as it is anoutparameter that exists only to return a value. What at first appeared to be arbitrary and capricious rules now make sense; values are required to be initialized only when their initial value is meaningful.
Often, you’ll want to have more than one function with the same name. The most common example of this is to have more than one constructor. In the examples shown so far, the constructor has taken a single parameter: a DateTime object. It would be convenient to be able to set newTimeobjects to an arbitrary time by passing in year, month, date, hour, minute, and second values. It would be even more convenient if some clients could use one constructor, and other clients could use the other constructor. Function overloading provides for exactly these contingencies.
The signature of a method is defined by its name and its parameter list. Two methods differ in their signatures if they have different names or different parameter lists. Parameter lists can differ by having different numbers or types of parameters. For example, in the following code, the first method differs from the second in the number of parameters, and the second differs from the third in the types of parameters:
A class can have any number of methods, as long as each one’s signature differs from that of all the others.
Example 4-9 illustrates theTimeclass with two constructors: one that takes aDateTimeobject, and the other that takes six integers.
Example 4-9. Overloading the constructor
#region Using directives
using System; using System.Collections.Generic; using System.Text;
#endregion
namespace OverloadedConstructor { public class Time { // private member variables private int Year; private int Month; private int Date; private int Hour; private int Minute; private int Second;
// public accessor methods public void DisplayCurrentTime() { System.Console.WriteLine( "{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second ); }
// constructors public Time( System.DateTime dt ) { Year = dt.Year; Month = dt.Month; Date = dt.Day; Hour = dt.Hour; Minute = dt.Minute; Second = dt.Second; }
public Time( int Year, int Month, int Date, int Hour, int Minute, int Second ) { this.Year = Year; this.Month = Month; this.Date = Date; this.Hour = Hour; this.Minute = Minute; this.Second = Second; } }
public class Tester { static void Main() { System.DateTime currentTime = System.DateTime.Now;
Time t1= new Time( currentTime ); t.DisplayCurrentTime();
Time t2 = new Time( 2007, 11, 18, 11, 03, 30 ); t2.DisplayCurrentTime();
} } }
As you can see, theTimeclass in Example 4-9 has two constructors. If a function’s signature consisted only of the function name, the compiler would not know which constructors to call when constructingt1andt2. However, because the signature includes the function argument types, the compiler is able to match the constructor call fort1with the constructor whose signature requires aDateTimeobject. Likewise, the compiler is able to associate thet2constructor call with the constructor method whose signature specifies six integer arguments.
When you overload a method, you must change the signature (i.e., the name, number, or type of the parameters). You are free, as well, to change the return type, but this is optional. Changing only the return type doesn’t overload the method, and creating two methods with the same signature but differing return types will generate a compile error, as you can see in Example 4-10.
Example 4-10. Varying the return type on overloaded methods
#region Using directives
using System; using System.Collections.Generic; using System.Text;
#endregion
namespace VaryingReturnType { public class Tester { private int Triple( int val) { return 3 * val; }
private long Triple( long val ) { return 3 * val; }
public void Test() { int x = 5; int y = Triple( x ); System.Console.WriteLine( "x: {0} y: {1}", x, y );
long lx = 10; long ly = Triple( lx ); System.Console.WriteLine( "lx: {0} ly: {1}", lx, ly ); } static void Main() { Tester t = new Tester(); t.Test(); } } }
In this example, theTesterclass overloads theTriple()method, one to take an integer, the other to take a long. The return type for the twoTriple()methods varies. Although this is not required, it is very convenient in this case.
Properties allow clients to access class state as though they were accessing member fields directly, while actually implementing that access through a class method.
This is ideal. The client wants direct access to the state of the object and doesn’t want to work with methods. The class designer, however, wants to hide the internal state of his class in class members, and provide indirect access through a method.
By decoupling the class state from the method that accesses that state, the designer is free to change the internal state of the object as needed. When theTimeclass is first created, theHourvalue might be stored as a member variable. When the class is redesigned, theHour value might be computed or retrieved from a database. If the client had direct access to the originalHourmember variable, the change to computing the value would break the client. By decoupling and forcing the client to go through a method (or property), theTimeclass can change how it manages its internal state without breaking client code.
Properties meet both goals: they provide a simple interface to the client, appearing to be a member variable. They are implemented as methods, however, providing the data-hiding required by good object-oriented design, as illustrated in Example 4-11.
Example 4-11. Using a property
#region Using directives
using System; using System.Collections.Generic; using System.Text;
#endregion
namespace UsingAProperty { public class Time { // private member variables private int year; private int month; private int date; private int hour; private int minute; private int second;
// public accessor methods public void DisplayCurrentTime() {
// constructors public Time( System.DateTime dt ) { year = dt.Year; month = dt.Month; date = dt.Day; hour = dt.Hour; minute = dt.Minute; second = dt.Second; }
// create a property
public int Hour { get { return hour; }
set { hour = value; } } }
public class Tester { static void Main() { System.DateTime currentTime = System.DateTime.Now; Time t = new Time( currentTime ); t.DisplayCurrentTime();
int theHour = t.Hour; System.Console.WriteLine( "\nRetrieved the hour: {0}\n", theHour ); theHour++; t.Hour = theHour; System.Console.WriteLine( "Updated the hour: {0}\n", theHour ); } } }
To declare a property, write the property type and name followed by a pair of braces. Within the braces you may declaregetandsetaccessors. Neither of these has explicit parameters, though theset(), accessor has an implicit parametervalue, as shown next.
In Example 4-11,Houris a property. Its declaration creates two accessors:getandset:
public int Hour { get { return hour; }
set { hour = value; } }
Each accessor has an accessor body that does the work of retrieving and setting the property value. The property value might be stored in a database (in which case the accessor body would do whatever work is needed to interact with the database), or it might just be stored in a private member variable:
The body of the get accessor is similar to a class method that returns an object of the type of the property. In the example, the accessor for Houris similar to a method that returns anint. It returns the value of the private member variable in which the value of the property has been stored:
get { return hour; }
In this example, a localintmember variable is returned, but you could just as easily retrieve an integer value from a database, or compute it on the fly.
Whenever you read the property, the get accessor is invoked:
Time t = new Time(currentTime); int theHour = t.Hour;
In this example, the value of theTime object’sHour property is retrieved, invoking thegetaccessor to extract the property, which is then assigned to a local variable.
The set Accessor
The set accessor sets the value of a property and is similar to a method that returns void. When you define asetaccessor, you must use thevaluekeyword to represent the argument whose value is passed to and stored by the property:
set { hour = value; }
Here, again, a private member variable is used to store the value of the property, but thesetaccessor could write to a database or update other member variables as needed.
When you assign a value to the property, thesetaccessor is automatically invoked, and the implicit parametervalueis set to the value you assign:
theHour++; t.Hour = theHour;
The two main advantages of this approach are that the client can interact with the properties directly, without sacrificing the data-hiding and encapsulation sacrosanct in good object-oriented design, and that the author of the property can ensure that the data provided is valid.
It is possible to set an access modifier (protected, internal,private) to modify access to either thegetorsetaccessor. To do so, your property must have both asetand agetaccessor, and you may modify only one or the other. Also, the modifier must be more restrictive than the accessibility level already on the property or the indexer (thus, you may addprotected to theget orsetaccessor of a public property, but not to a private property):
public string MyString { protected get { return myString; } set { myString = value; } }
In this example, access to thegetaccessor is restricted to methods of this class and classes derived from this class, whereas thesetaccessor is publicly visible.
Note that you may not put an access modifier on an interface (see Chapter 8) or on explicit interface member implementation. In addition, if you are overriding a virtual property or index (as discussed next), the access modifier must match the base property’s access modifier.
readonly Fields
You might want to create a version of the Time class that is responsible for providing public static values representing the current time and date. Example 4-12 illustrates a simple approach to this problem.
Example 4-12. Using static public constants
#region Using directives
using System; using System.Collections.Generic; using System.Text;
#endregion
namespace StaticPublicConstants { public class RightNow { // public member variables public static int Year; public static int Month; public static int Date; public static int Hour; public static int Minute; public static int Second;
static RightNow()
{ System.DateTime dt = System.DateTime.Now; Year = dt.Year; Month = dt.Month; Date = dt.Day; Hour = dt.Hour; Minute = dt.Minute; Second = dt.Second; } }
This works well enough, until someone comes along and changes one of these values. As the example shows, theRightNow.Yearvalue can be changed, for example, to2008. This is clearly not what we’d like.
You’d like to mark the static values as constant, but that is not possible because you don’t initialize them until the static constructor is executed. C# provides the keywordreadonlyfor exactly this purpose. If you change the class member variable declarations as follows:
public static readonly int Year; public static readonly int Month; public static readonly int Date; public static readonly int Hour; public static readonly int Minute; public static readonly int Second;
and then comment out the reassignment inMain():
// RightNow.Year = 2008; // error!
the program will compile and run as intended.
* The terms argument and parameter are often used interchangeably, though some programmers insist on differentiating between the parameter declaration and the arguments passed in when the method is invoked.
* When you write your own constructor, you’ll find that these values have been initialized before the constructor runs. In a sense, there are two steps to building new objects—some CLR-level magic that zeros out all the fields and does whatever else needs to be done to make the thing a valid object, and then the steps in the constructor you create (if any).
* A pointer is a variable that holds the address of an object in memory. C# doesn’t use pointers with managed objects. Some C++ programmers have become so used to talking about a this pointer that they’ve carried the term over (incorrectly) to C#. We’ll refer to the this reference, and pay a 25-cent fine to charity each time we forget.
* As noted earlier, btnUpdate and btnDelete are actually variables that refer to the unnamed instances on the heap. For simplicity, we’ll refer to these as the names of the objects, keeping in mind that this is just shorthand for "the name of the variables that refer to the unnamed instances on the heap."
* Actually, the CLR guarantees to start running the static constructor before anything else is done with your class. However, it only guarantees to start running the static constructor; it doesn’t actually guarantee to finish running it. It is possible to concoct a pathological case where two classes have a circular dependency on each other. Rather than deadlock, the CLR can run the constructors on different threads so that it meets the minimal guarantee of at least starting to run both constructors in the right order.
* Most of the time you will not write classes that deal with unmanaged resources such as raw handles directly. You may, however, use wrapper classes such as FileStream and Socket, but these classes do implement IDisposable, in which case you ought to have your class implement IDisposable (but not a finalizer). Your Dispose method will call Dispose on any disposable resources that you’re using.