Thursday, April 03, 2008

C# quiz 8/?

Now it's time for some one regarding serialization:

Given this serializable class:

    [Serializable]
public class Victim {
public static int InstanceCount;
public int Data { get; set; }

public Victim() {
InstanceCount++;
Data = 2;
}
}

and the following test code:

    class Program {
static void Main(string[] args) {
Victim v = new Victim();
v.Data = 3;
BinaryFormatter serial = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
serial.Serialize(ms, v);
ms.Seek(0, SeekOrigin.Begin);
v = (Victim)serial.Deserialize(ms);
Console.WriteLine("Data {0}", v.Data);
Console.WriteLine("Instances {0}", Victim.InstanceCount);
}
}

What is the output, and why?

C# quiz 7/?

If we compile the following code to a dll, for example using the following command line:

csc.exe /t:library Library.cs

namespace Library {
public class Library {
public const int FOO = 1;
public static readonly int BAR = 2;
}
}

And refer those in our test program:

    class Program {
static void Main(string[] args) {
Console.WriteLine(Library.Library.FOO);
Console.WriteLine(Library.Library.BAR);
}
}

Now, if we change FOO to 10 and BAR to 12 and recompile Library.dll without recompiling the test program, what is the output when we run the test program?

C# quiz: 6/?

Consider the following:

    class Test {
private int m_foo;
public int Foo {
get { return m_foo; }
}
public int set_Foo(int value) {
m_foo = value;
}
}

What happens when you compile, and why?


Wednesday, April 02, 2008

C# quiz: 5/?

Consider a console application with the following Main:

        static void Main(string[] args) {
System.Threading.Timer t = new Timer(dummy =>
{
Console.WriteLine("In callback: " + DateTime.Now);
GC.Collect();

}, null, 0, 1000);

Console.ReadLine();
t = null;
}

What does the code do? Does it matter if it is compiled Debug or Release, if so, why?


(To give proper credit where credit is due, this example is heavily inspired by CLR via C# by Jeffrey Richter)


C# quiz: 4/?

Given the following code:

    internal class Test {
public int Foo { get; set; }
public int Bar { get; set; }

#if CUSTOM
public override int GetHashCode() {
return Foo.GetHashCode() ^ Bar.GetHashCode();
}

public override bool Equals(object obj) {
if (object.ReferenceEquals(obj, null)) return false;
Test t = obj as Test;
if (t != default(Test)) {
return (Foo == t.Foo) && (Bar == t.Bar);
}
return false;
}
#endif
}

and the following test code:

            Dictionary<Test, string> dict = new Dictionary<Test, string>();
Test foo = new Test { Foo = 1, Bar = 2 };
dict[foo] = "Hello World";
foo.Bar = 42;
if (dict.ContainsKey(foo)) {
Console.WriteLine("it's there");
} else {
Console.WriteLine("it's not");
}

Answer the following questions:



  1. With CUSTOM not defined, what does the test code print?

  2. If CUSTOM is defined, does it change the outcome?

  3. What if Test is changed to a struct and CUSTOM is not defined, what is the output?

  4. If Test is a struct and CUSTOM is defined, what happens?

Tuesday, April 01, 2008

C# quiz: 3/?

Consider the following class:

    internal class Test {

public int Code { get; set; }

public static bool operator ==(Test lhs, Test rhs) {
if (object.ReferenceEquals(lhs, rhs)) return true;
if (lhs == null || rhs == null) {
return false;
} else {
return lhs.Code == rhs.Code;
}
}

public static bool operator != (Test lhs, Test rhs) {
return !(lhs == rhs);
}
}

What is the outcome of the following test code, and why?

            Test test1 = new Test { Code = 42 };
Test test2 = new Test { Code = 42 };
if (test1 != test2) {
Console.WriteLine("different");
} else {
Console.WriteLine("equals");
}