June 29, 2011

Contravariance and Covariance

Good simple MSDN guide
Many definitions here, choose the one that is easiest to understand
Here is the Wikipedia view

Covariant: converting from a broader type (more generic) to a narrower (more specific) type. Can use a more derived type than that specified in the definition in an output (returned type). Covariance means that you can use an object of a specialized class (e.g. String) where the declaration states a more general class (e.g. object).

Contravariant: converting from a narrower (more specific) type to a broader (more generic) type.  Can use a base type of that specified in the definition. Contravariance means that you can use an object of a more general class (e.g. object) where the declaration states a more specific class (e.g. string).

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
Func<string, string> upper = str => str.ToUpper();
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 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