In Chapter 3, I drew a distinction between value types and reference types. The primitive C# types (int,char, etc.) are value types and are created on the stack. Objects, however, are reference types and are created on the heap, using the keywordnew, as in the following:
Time t = new Time();
t doesn’t actually contain the value for theTime object; it contains the address of that (unnamed) object that is created on the heap.titself is just a reference to that object.
VB 6 programmers take note: although there is a performance penalty in using the VB 6 keywordsDimandNew on the same line, in C#, this penalty has been removed. Thus, in C#, there is no drawback to using thenewkeyword when declaring an object variable.
Constructors
In Example 4-1, notice that the statement that creates the Time object looks as though it is invoking a method:
Time t = new Time();
In fact, a method is invoked whenever you instantiate an object. This method is called a constructor, and you must either define one as part of your class definition, or let the CLR provide one on your behalf. The job of a constructor is to create the object specified by a class and to put it into a valid state. Before the constructor runs, the object is undifferentiated memory; after the constructor completes, the memory holds a valid instance of the classtype.
TheTimeclass of Example 4-1 doesn’t define a constructor. If a constructor is not declared, the compiler provides one for you. The default constructor creates the object, but takes no other action.
Member variables are initialized to innocuous values (integers to 0, strings to null, etc.).* Table 4-2 lists the default values assigned to primitive types.
Table 4-2. Primitive types and their default values
Type numeric (int, long, etc.) bool
Default value 0 false
char enum
'\0' (null) 0
reference
null
Typically, you’ll want to define your own constructor and provide it with arguments so that the constructor can set the initial state for your object. In Example 4-1, assume that you want to pass in the current year, month, date, and so forth so that the object is created with meaningful data.
To define a constructor, you declare a method whose name is the same as the class in which it is declared. Constructors have no return type and are typically declared public. If there are arguments to pass, you define an argument list just as you would for any other method. Example 4-3 declares a constructor for theTimeclass that accepts a single argument, an object of typeDateTime.
Example 4-3. Declaring a constructor
#region Using directives
using System; using System.Collections.Generic; using System.Text;
#endregion
namespace DeclaringConstructor { public class Time {
// private member variables int Year; int Month; int Date; int Hour; int Minute; int Second;
// public accessor methods public void DisplayCurrentTime() { System.Console.WriteLine( "{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, 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(); } } }
Output: 11/16/2007 16:21:40
In this example, the constructor takes aDateTime object and initializes all the member variables based on values in that object. When the constructor finishes, theTimeobject exists and the values have been initialized. WhenDisplayCurrentTime()is called inMain(), the values are displayed.
Try commenting out one of the assignments and running the program again. You’ll find that the member variable is initialized by the compiler to0. Integer member variables are set to0if you don’t otherwise assign them. Remember, value types (e.g., integers) can’t be uninitialized; if you don’t tell the constructor what to do, it will try for something innocuous.
In Example 4-3, theDateTimeobject is created in theMain()method ofTester. This object, supplied by theSystemlibrary, offers a number of public values—Year,Month,Day,Hour,Minute, andSecond—that correspond directly to the private member variables of theTimeobject. In addition, theDateTimeobject offers a static member property,Now, which is a reference to an instance of aDateTimeobject initialized with the current time.
Examine the highlighted line inMain(), where theDateTimeobject is created by calling the static propertyNow.Nowcreates aDateTimevalue which, in this case, gets copied to thecurrentTimevariable on the stack.
ThecurrentTimevariable is passed as a parameter to theTimeconstructor. TheTimeconstructor parameter,dt, is a copy of theDateTimeobject.
It is possible to initialize the values of member variables in an initializer, instead of having to do so in every constructor. You create an initializer by assigning an initial value to a class member:
private int Second = 30; // initializer
Assume that the semantics of ourTime object are such that no matter what time is set, the seconds are always initialized to30. You might rewrite theTime class to use an initializer so that no matter which constructor is called, the value ofSecondis always initialized, either explicitly by the constructor or implicitly by the initializer. See Example 4-4.
Example 4-4 uses an overloaded constructor, which means that there are two versions of the constructor that differ by the number and type of parameters. I explain overloading constructors in detail later in this chapter.
Example 4-4. Using an initializer
#region Using directives
using System; using System.Collections.Generic; using System.Text;
#endregion
namespace Initializer { public class Time { // private member variables private int Year; private int Month; private int Date; private int Hour; private int Minute; private int Second = 30; // initializer
// public accessor methods public void DisplayCurrentTime() { System.DateTime now = System.DateTime.Now; System.Console.WriteLine( "\nDebug\t: {0}/{1}/{2} {3}:{4}:{5}", now.Month, now.Day, now.Year, now.Hour, now.Minute, now.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; //explicit assignment
} public Time( int Year, int Month, int Date, int Hour, int Minute ) { this.Year = Year; this.Month = Month; this.Date = Date; this.Hour = Hour; this.Minute = Minute; }
}
public class Tester { static void Main() { System.DateTime currentTime = System.DateTime.Now; Time t = new Time( currentTime ); t.DisplayCurrentTime();
Time t2 = new Time( 2007, 11, 18, 11, 45 ); t2.DisplayCurrentTime();
} } }
Output: Debug : 11/27/2007 7:52:54 Time : 11/27/2007 7:52:54
Debug : 11/27/2007 7:52:54 Time : 11/18/2007 11:45:30
If you don’t provide a specific initializer, the constructor will initialize each integer member variable to zero (0). In the case shown, however, theSecond member is initialized to30:
private int Second = 30; // initializer
If a value is not passed in forSecond, its value will be set to30whent2is created:
Time t2 = new Time(2007,11,18,11,45); t2.DisplayCurrentTime();
However, if a value is assigned toSecond, as is done in the constructor (which takes aDateTimeobject, shown in bold), that value overrides the initialized value.
The first time we invokeDisplayCurrentTime(), we call the constructor that takes aDateTimeobject, and the seconds are initialized to54. The second time the method is invoked, we explicitly set the time to11:45(not setting the seconds), and the initializer takes over.
If the program didn’t have an initializer, and did not otherwise assign a value toSecond, the value would be initialized by the CLR to 0.
C++ programmers take note: C# doesn’t have a copy constructor, and the semantics of copying are accomplished by implementing theICloneableinterface.
The .NET Framework defines an ICloneable interface to support the concept of a copy constructor. (We cover interfaces in detail in Chapter 8.) This interface defines a single method: Clone(). Classes that support the idea of a copy constructor should implementICloneable, and then should implement either a shallow copy (callingMemberwiseClone) or a deep copy (e.g., by calling the copy constructor and hand-copying all the members):
class SomeType: ICloneable { public Object Clone() { return MemberwiseClone(); // shallow copy } }
The this Keyword
The keyword this refers to the current instance of an object. The thisreference (sometimes referred to as athispointer*) is a hidden reference passed to every non-static method of a class. Each method can refer to the other methods and variables of that object by way of thethisreference.
Thethisreference is typically used in a number of ways. The first way is to qualify instance members otherwise hidden by parameters, as in the following:
public void SomeMethod (int hour) { this.hour = hour; }
In this example,SomeMethod()takes a parameter (hour) with the same name as a member variable of the class. Thethisreference is used to resolve the name ambiguity. Whereasthis.hourrefers to the member variable,hourrefers to the parameter.
The argument in favor of this style is that you pick the right variable name and then use it for both the parameter and the member variable. The counter argument is that using the same name for both the parameter and the member variable can be confusing.
The second use of thethisreference is to pass the current object as a parameter to another method. For instance:
class myClass { public void Foo(OtherClass otherObject) { otherObject.Bar(this); } }
Let’s unpack this example. Here, we have a method namedmyClass.Foo. In the body of this method, you invoke theBarmethod of theOtherClassinstance, passing in a reference to the current instance ofmyClass. This allows theBarmethod to fiddle with the public methods and members of the current instance ofmyClass.
The third use ofthisis with indexers, covered in Chapter 9.
The fourth use of thethisreference is to call one overloaded constructor from another, for example:
class myClass { public myClass(int i) { //... } public myClass() : this(42) { //... } }
In this example, the default constructor invokes the overloaded constructor that takes an integer, by using thethiskeyword.
The final way that thethiskeyword is used is to explicitly invoke methods and members of a class, as a form of documentation:
public void MyMethod(int y) { int x = 0; x = 7; // assign to a local variable y = 8; // assign to a parameter this.z = 5; // assign to a member variable this.Draw(); // invoke member method }
In the cases shown, the use of thethisreference is superfluous, but it may make the programmer’s intent clearer and does no harm (except, arguably, to clutter the code).
The members of a class (variables, methods, events, indexers, etc.) can be either instance members or static members. Instance members are associated with instances of a type, whereas static members are considered to be part of the class. You access a static member through the name of the class in which it is declared. For example, suppose you have a class named Button, and have instantiated objects of that class namedbtnUpdateandbtnDelete.* Suppose as well that theButtonclass has a static methodSomeMethod(). To access the static method, you write:
Button.SomeMethod();
rather than:
btnUpdate.SomeMethod();
In C#, it is not legal to access a static method or member variable through an instance, and trying to do so will generate a compiler error (C++ programmers take note).
Some languages distinguish between class methods and other (global) methods that are available outside the context of any class. In C#, there are no global methods, only class methods, but you can achieve an analogous result by defining static methods within your class.
VB 6 programmers take note: don’t confuse thestatickeyword in C# with theStatic keyword in VB 6 and VB.NET. In VB, theStatickeyword declares a variable that is available only to the method in which it was declared. In other words, theStaticvariable is not shared among different objects of its class (i.e., eachStaticvariable instance has its own value). However, this variable exists for the life of the program, which allows its value to persist from one method call to another.
In C#, thestatickeyword indicates a class member. In VB, the equivalent keyword isShared.
Static methods act more or less like global methods, in that you can invoke them without actually having an instance of the object at hand. The advantage of static methods over global, however, is that the name is scoped to the class in which it occurs, and thus you don’t clutter up the global namespace with myriad function names. This can help manage highly complex programs, and the name of the class acts very much like a namespace for the static methods within it.
In addition, static methods may be passed instance members as parameters (or may create such instances themselves within the static method). Because they are scoped to the class, instead of being scoped globally, they have access to the private members of the instances.
Resist the temptation to create a single class in your program in which you stash all your miscellaneous methods. It is possible, but not desirable, and it undermines the encapsulation of an object-oriented design.
Please check back next week for the continuation of this series.