Monthly Archives: March 2011

Puzzle 1: Passing reference types by value (Beginner)

With this post I’ll start a series about programming puzzles and kata’s.

These small exercises will focus on the specific features of C#1,2 & 3.

Have fun…

 

What is the output of: ValueRef.Problem() ?

Can you explain why?

 

  1: public class ValueRef
  2: {
  3:         public static void Problem()
  4:         {
  5:             StringBuilder myStrBuilder = new StringBuilder("x");
  6:             AppendHello(myStrBuilder);
  7:             Console.WriteLine(myStrBuilder.ToString());
  8:             Console.ReadLine();
  9:         }
 10: 
 11:         static void AppendHello(StringBuilder builder)
 12:         {
 13:             builder.Append("hello");
 14:             builder = null;
 15:         }
 16: }

kick it on DotNetKicks.com