oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Top Ten Traps in C# for C++ Programmers
Pages: 1, 2, 3

Trap #7: You may not initialize in the header

Initialization works differently in C# than it does in C++. Suppose you have a class "Person" with a private member-variable age, and a derived lass "Employee" with a private member variable salaryLevel. In C++ you might initialize salaryLevel in the initialization part of the Employee constructor, like this:

	Employee::Employee(int theAge, int theSalaryLevel):
	Person(theAge) // initialize base
	salaryLevel(theSalaryLevel) // initialize member variable
	   // body of constructor

This construct is not legal in C#. While you can still initialize the base, the initialization of the member variable as shown here would cause a compile error. You can, however, set the initial value for the member variable in C# when you declare it:

Class Employee : public Person
   // declarations here
   private salaryLevel = 3; // initialization

Note also that you do not add a semicolon after the class declaration, and that each member must have its access declared explicitly.

Trap #8: Boolean values do not convert to integers

In C#, Boolean values (true, false) do not equate to integer variables. Thus, you may not write:

	if ( someFuncWhichReturnsAValue() )

and count on the idea that if someFuncWhichReturnsAValue returns a zero it will evaluate false, otherwise true. The good news is that the old error of using assignment versus equality is no longer a problem. Thus, if you write:

	if ( x = 5 )

you will not inadvertently assign 5 to x, you will get a compile error, since x = 5 evaluates to 5, which is not a Boolean value.

Trap #9: You may not “fall through” in switch statements

In C#, a switch statement may not “fall through” to the next statement if it does any work. Thus, while the following is legal in C++, it is not legal in C#:

	switch (i)
	   case 4:
	   case 5: // error, no fall through

To accomplish this, you need to use an explicit Go To statement:

	switch (i)
	   case 4:
	      goto case 5;
	   case 5:

If the case statement does no work (has no code within it) then you can fall through:

	switch (i)
	   case 4: // fall through
	   case 5: // fall through
	   case 6:

Trap #10: C# requires definite assignment

C# imposes definite assignment, which requires that all variables be assigned a value before they are used. Thus, you can declare a variable without initializing it, but you may not pass it to a method until it has a value.

This raises a problem with values you create simply to pass them to a method by reference, to act as "out" parameters. For example, suppose you have a method that returns the current hour, minute, and second. If you were to write:

int theHour;
int theMinute;
int theSecond;
timeObject.GetTime( ref theHour, ref theMinute, ref theSecond)

You would get a compile error for using theHour, theMinute, and theSecond without initializing them:

	Use of unassigned local variable ‘theHour’
	Use of unassigned local variable ‘theMinute’
	Use of unassigned local variable ‘theSecond’

You can initialize them to zero or some other innocuous value to quiet the pesky compiler:

	   int theHour = 0;
	   int theMinute = 0;
	   int theSecond = 0;
	timeObject.GetTime( ref theHour, ref theMinute, ref theSecond)

But that is too silly. The point of these variables is to pass them by reference into GetTime, where they’ll be changed. C# provides the out parameter modifier for this situation. The out modifier removes the requirement that a reference parameter be initialized. The parameters to GetTime, for example, provide no information to the method; they are simply a mechanism for getting information out of it. Thus, by marking all three as out parameters, you eliminate the need to initialize them outside the method. Out parameters must be assigned a value before the method they are passed into returns. Here are the altered parameter declarations for GetTime:

	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 GetTime method:

	timeObject.GetTime( out theHour, out theMinute, out theSecond);

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.

O'Reilly & Associates will soon release (February 2002) Programming C#, 2nd Edition.

For more information, or to order the book, click here.

Return to the .NET DevCenter.