June 28, 2011

Silverlight and WPF relationship

Here is a neat image that succinctly describes the relationship between Silverlight and WPF.
Silverlight versus WPF

I took it from this site but I think it is the best indicator of the relationship between the 2 of them.

Here is another that relates them Windows Phone 7 as well
Silverlight, WPF and Windows Phone 7

C# Protected Internal

In C#, "protected internal" is the UNION of the terms not the intersection! So a "protected internal" object can be accessed by a derived class (derived from the class with the "protected internal" item) or from another class in the same assembly.
see this article

June 14, 2011

Func samples

Some Func<> samples I found on the web
First
Func<string, string> upper = str => str.ToUpper();
Then
Func<int, int> Factorial = null;
Factorial = (n) => n <= 1 ? 1 : n * Factorial(n - 1);

Parent/Child versus Owner/Owned in Windows


In pure Windows terms.
Owner property of a window in .NET

In pure windows terms:
Child windows are rendered within the client area of their parent window (eg: buttons, text boxes, etc.).
A child window has the WS_CHILD style and is confined to the client area of its parent window. An application typically uses child windows to divide the client area of a parent window into functional areas. A child window can have it's parent window changed.

Owned windows are rendered outside the client area of their owner window (dialog boxes, message boxes, etc.)
An owned window is an overlapped or pop-up window (WS_OVERLAPPED or WS_POPUP style style) and are used for rendering outside of a owner window’s client area. Ownership of Owned windows cannot be transferred.
The Owner is responsible for for creating/destroying the owned window.

Do WM_COMMAND messages get sent to the owner window or parent window first? My work colleague strongly argues that the OS first tries to send them to the owner window but if this is not set (NULL[==the desktop window]) then the message is sent to the parent window. However, I can not find any documentation that collaborates this.

This link argues that a window can have eiether an Owner or a Parent but not both.

Volatile

Volatile on Msdn
Keep forgetting to use this. The volatile keyword indicates that a field can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread. Volatile means that read/write operations will always target the main memory not a cached copy, it does not imply access to a variable is made thread safe through its usage.
Good explanantion of volatile/non-volatile reads and writes
Understand the Impact of Low-Lock Techniques in Multithreaded Apps

Watch out for warnings when using a volatile variable in an Interlocked operation. Use #pragma to remove it:
#pragma warning disable 420  // Volatile passed as reference to the interlocked API
  System.Threading.Interlocked.Exchange<SomeType>(ref this.somObject, newSomeObject);
#pragma warning restore 420

June 9, 2011

Regex Matches Tester

Can find and list matching regular expressions in a string
internal class TestRegexMatches
{

    public void Test()
    {
        string typeName = typeof(double).ToString().Replace('.', '_');
        string name = "Fudge Factor".Replace(" ", "_");
        string plugin = "Smb.Orca.Theo.Samba.Delta, Version=4.20111.99.0, Culture=neutral, PublicKeyToken=19ae4a476ca5c63x";
        string[] splitStr = plugin.Split(',');
        plugin = splitStr[0].Replace('.', '_');

        string id = typeName + "\t" + name + "\t" + plugin;
        int ix=1;
        MatchCollection matches = Regex.Matches(id, @"[^\t]+");
        foreach (Match match in matches)
        {
            Console.WriteLine("Match " + ix++.ToString() + ": " + match.Value);
        }
    }
}
produces the output
Match 1: System_Double
Match 2: Fudge_Factor
Match 3: Smb_Orca_Theo_Samba_Delta

April 5, 2011

Use TryGetValue instead of ContainsKey

See these pages for more info:
C# TryGetValue
C# TryGetValue Method

Basically, instead of
private Dictionary  xxxCache = new Dictionary ()

Xxx GetEntry(Key key)
{
  Xxx entry = null;
  // if entry found
  if (xxxCache.ContainsKey(key)) 
  { 
    entry = xxxCache[key] as Xxx;
  }
  else // add new entry
  {
    entry = new Xxx(...);
    xxxCache.Add(key, entry);
  }
  return entry;
}
use
Xxx GetEntry(Key key)
{
  Xxx entry = null;
  // if entry not found
  if (!xxxCache.TryGetValue(key, out entry)) 
  { // then add new entry
    entry = new Xxx(...);
    xxxCache.Add(key, entry);
  }
  return entry as Xxx;
}
This is assuming you do NOT want an exception to be thrown if the key is not found. I still find the first implementation as more readable