Wednesday, April 02, 2008

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");
}

Monday, March 31, 2008

C# quiz: 2/?

Continuing on the series of C# and/or .NET Framework short questions:

        private void Foo(List<int> bar) {
bar = new List<int>();
// ...
}

With the method above and the code below:

        List<int> bar = null;
Foo(bar);
if (bar != null) {
Console.WriteLine("populated");
} else {
Console.WriteLine("still null");
}
If or else ?

EDIT: fixed typo in code sample.

C# quiz: 1/?

I'm starting a series with small questions C# and/or .NET Framework related questions:

First out, NaN values:

            double foo = double.NaN;
// ...
if (foo == double.NaN) {
Console.WriteLine("NaN");
} else {
Console.WriteLine("a number");
}

If or else ?

Wednesday, March 19, 2008

Safari - first impressions

I thought that I might give it a shot after all so I installed it. First impressions:

- it's a memory hog! 4 sites in 4 tabs consumes a whopping 210MB, while Firefox 3 beta 4 for the same sites consumes "only" 74MB

- the UI is ugly

- scroll wheel doesn't work properly in bookmarks

- close tab X button on the left !?!

- As I don't have mouse gestures, I often right-click and choose back. If I happen to have the cursor over a word, (quite likely), safari chooses to select the word and only select search with google or copy. WTF?

...my default browser isn't changing.

Tuesday, March 18, 2008

Safari for Windows - Apple spamming users

I have an ipod, and therefore I have ITunes installed. Today Apple Software Update popped up and greeted me with the following totally unrelated thing: Safari 3.1 (22.65Mb)

Safari for Windows is the fastest and easiest-to-use web browser for the PC.  It displays web pages faster than any other browser and is filled with innovative features -- all delivered in an efficient and elegant user interface.

For more information on Safari 3.1, please visit http://docs.info.apple.com/article.html?artnum=307467

Hmm, that sounds too good to be true.

...and why does it notify me about it in the first place?

Friday, March 14, 2008

Gotcha: Hidden [Flags] attribute on bitfield enumerations

 

Just got reminded of a still unfixed bug.

Consider an enumeration of the following kind:

    /// <summary>
/// Modes enumeration
/// </summary>
[Flags]
public enum Modes {
/// <summary>
/// Yes
/// </summary>
Foo = 1,
/// <summary>
/// No
/// </summary>
Bar = 2,
/// <summary>
/// Maybe
/// </summary>
Both = Foo|Bar
}

For the user of this enumeration, it is essential to know that it in fact is a bitfield (as specified by the FlagsAttribute).


However, both Intellisense and object browser fails to give any hint about this fact!?! (Yes, this occurs in both VS 2005 and VS 2008)


So for now, remember to document (within in the summary, so it is visible) this fact for your fellow developers.