Useful C# and Visual Studio tips you might not know

While working on my next game, I tried to keep notes of interesting tips I used. They are related to C# and Visual Studio.

Overriding ToString()

When debugging or prototyping, you often need to display complex objects on the screen on in the debugger output. A handy way to do this when dealing with complex classes is to override the ToString() member. Here is an example:

class ComplexClass
{
   int _number = 0;
   string _string = "My String";

   public ComplexClass( int n, string s )
   {
      _number = n;
      _string = s;
   }

   public override string ToString()
   {
      return String.Format( "{0} / {1}", _number, _string );
   }
}

ToString() is also used by the debugger in the Watch dialog.

?? operator

I discovered the ?? operator a few months ago. I don’t use it much but could be useful in some cases. It allows you to replace the following code:

if( a != null )
   b = a;
else
   b = c;

by

b = a ?? c;

New class template

When you create a new class (using the right-click on the project, Add, New Item…), the file created is not empty but already has a template and a few using statements. You can edit this template easily. For instance, I added a “using fg;” with my own game library to this template. The template is found in the Visual Studio directory: {Program Files}Microsoft Visual Studio 10.0Common7IDEVCSExpressItemTemplatesCache1033class.zip. There are templates for anything and you will probably want to edit a few of them.

new does not always allocates

In C#, even when using the new operator, you might not allocate memory. For instance, the following line:

Vector2 point = new Vector2( 10, 20 );

does not allocate memory, because Vector2 is not a class, but a structure. The rule is as simple as that: a class allocates memory (and returns a pointer) and a struct does not allocate memory. It’s simple, but often misunderstood, especially if you come from C++.

Add As Link source file

Recently, I re-used source files from a previous project for a new one. I used the Add Existing Item option but Visual C# creates a copy of the source file in the current project directory instead of referencing the old file. I want to reference the old file so that it can be modified in both projects. To avoid that, you must select the hidden Add As Link option, by clicking the small arrow next to the Add button of the dialog box.

add_as_link

Overloading the [] operator

I often have objects containing a list of items. For instance, in Spring Up Harmony, I have a Ball Manager used to manage and draw the balls. I found it very handy to overload the [] operator to access elements of the list inside the manager from the outside directly.

List<Ball> _balls = new List<Ball>();
public int Count { get { return _balls.Count; } }
public Ball this[int i] { get { return _balls[i]; } }

That’s it!

I hope you found some of the tips useful!

Feel free to share more tips in the comments!

Follow me on twitter or facebook to get notified of new posts.