Skip navigation

Write to debug stream

System.Diagnostics.Debug.Write(“text”) ;

Enum

excellent article here. Also regarding the question code works without [Flags], so what’s the point of [Flags]?

Repeated here:

Flags indicates that the enum is intended to be used as a set of bit fields. The ony practical difference it makes is in the implementation of ToString on the enum. without the attribute it simply prints out teh symbolic equivelent of the value or the value uf there is no equiveleint. With teh attribute if it can’t find the exact match it tries to oassembly the value from the bitwise orable fields and if successful prints out teh list of fields comma separated.

Richard Blewett – DevelopMentor

C# gotchas

You are not allowed to remove something from a List if you’re iterating through that list using a foreach loop.


    List list = new List( new int[] { 3, 6, -9, 12, 15 } );

    foreach( int val in list )
    {
      if( val < 0 )
      {
        list.Remove( val ) ;  // ILLEGAL:  RUN-TIME ERROR!
      }
    }

Instead, you must be iterating using a vanilla for-loop. Keep in mind that this reduces the Count of your list when you do this


    List list = new List( new int[] { 3, 6, -9, 12, 15 } );

    for( int i = 0; i < list.Count; i++ )
    {
      int val = list[ i ] ;
      if( val < 0 )
      {
        list.RemoveAt( i );  // OK for List
      }
    }

You are not allowed to write directly to (i.e. overwrite) “foreach iteration variables”.

So for instance:

    foreach( int val in list )
    {
      val = 0;  // ILLEGAL, can't overwrite value of a "foreach iteration variable"
    }

However, this doesn’t mean that val is readonly. If val is an OBJECT with references to other things, e.g.:

  class Point
  {
    public int x, y;
    public override string ToString()
    {
      return "( " + x + ", " + y + " )" ;
    }
  }

  List points = new List( new Point[]{ new Point(), new Point(), new Point() } ) ;
    
  Console.WriteLine( points.Count );

  foreach( Point p in points )
  {
    p.x = 5 ;  // Works fine, since we are not overwriting
    // p itself, but writing to something that p refers to
  }

keyword yield

This is one of those things where you’re like “oh.. cool.. ah, but I can’t really use that..”

A couple of links that explain yield pretty well: The yield statement in C#,
Behind the scenes of the C# yield keyword
, Give way to the YIELD keyword!.

I don’t like keyword yield. Its not that useful to me, and I find the very idea a lot like using a static variable in C++. You know about it, and you hardly ever use it unless you’re like WOW, I COULD use one here. I’ve only really usefully used a static variable in C++ about once or twice to my recollection.

[ May 15/09 ]: These may be useful yet. There’s an article in GameDeveloper May 2009, page 15 that talks about using coroutines in C++. Also, Using Coroutines for Asynchronous Programming.

Two types of Arrays

This is a weird one. In C#, you can create two types of 2D arrays: the “normal” kind that you are probably used to new string[2][2] is a 2×2 array of strings. And the MUST BE SQUARE kind: new string[2,2]. The comma separated arrays are weird in more ways than one as I’ll show here.

    int[] normal1D = { 2, 5, 7 };   // probably already know this

    int[][] jagged2D = { new int[] { 5, 2, 9 }, new int[] { 7, 5 } };
    // 0:  5 2 9
    // 1:  7 5

    int[ , ] square2D_1 = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
    // 0:  1 0 0
    // 1:  0 1 0
    // 2:  0 0 1

    // So this last array type with the int[,] type specifier
    // is actually a SQUARE 2D array.  It HAS to be square.

    // For example, this next array is illegal:
    //int[ , ] square2D_wrong = { { 1, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; // X!!!!!!
    // You can't have a JAGGED array when you use [,] syntax.

    // You could also write the above array like:
    int[ , ] square2D_2 = new int[ , ] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };

    // Or even:
    int[ , ] square2D_3 = new int[ 3, 3 ] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };

    // Ok so how else do the [ , ] square type arrays differ from "normal"
    // 2d arrays?  Well, their length property measures the length of the
    // ENTIRE array!  Talk about weird.

    Console.WriteLine( square2D_3.Length ) ; // PRINTS 9 !  (not 3!)
    Console.WriteLine( square2D_3[ 0 ] ) ;   // X ILLEGAL!!

    // You can't seem to access an "individual" array that makes up
    // this 2D array.  In general then, the [,] arrays are somewhat
    // more restrictive than "normal" 2D arrays which is probably why
    // they aren't seen in common use.  However, you COULD find
    // a use for them like as a z-buffer or something.

Eval in C#

Not easy to get .. a few hacks..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: