C#

Is C# the Boost of C-family languages?

For all the cons of giving a single entity control over C#, one pro is that it gives the language an unmatched agility to try new things in the C family of languages. LINQ—both its language integration and its backing APIs—is an incredibly powerful tool for querying and transforming data with very concise code. I really can’t express how much I’ve come to love it.

The new async support announced at PDC10 is basically the holy grail of async coding, letting you focus on what your task is and not how you’re going to implement a complex async code path for it. It’s an old idea that many async coders have come up with, but, as far as I know, has never been successfully implemented simply because it required too much language support.

The lack of peer review and standards committee for .​NET shows—there’s a pretty high rate of turnover as Microsoft tries to iron down the right way to tackle problems, and it results in a very large library with lots of redundant functionality. As much as this might hurt .​NET, I’m starting to view C# as a sort of Boost for the C language family. Some great ideas are getting real-​world use, and if other languages eventually feel the need to get something similar, they will have a bounty of experience to pull from.

C++, at least, is a terrifyingly complex language. Getting new features into it is an uphill battle, even when they address a problem that everyone is frustrated with. Getting complex new features like these into it would be a very long process, with a lot of arguing and years of delay. Any extra incubation time we can give them is a plus.

Alex Filo has the right idea, UniformWrapPanel

WPF’s WrapPanel is missing a key feature: the ability to create vertical columns, but still fill the maximum amount of width available. One less annoyance in WPF, thanks to Alex Filo’s UniformWrapPanel.

Databinding TextBlocks with XAML

I’ve become frustrated lately with trying to databind a list of strings to a textblock. Ie, if I have:

string[] mytext = new string[] { "a", "b", "c" };

And I want the effective end result to be:

<TextBlock>a, b, c</TextBlock>

Or maybe I want something more complex like hyperlinks:

<TextBlock>
   <Hyperlink>a</Hyperlink>,
   <Hyperlink>b</Hyperlink>,
   <Hyperlink>c</Hyperlink>
</TextBlock>

Basically what I’m looking for is a ItemsControl that works on TextBlocks (or Spans, etc.), with a Separator template to insert those “, ” in between the items.

Your first instinct might be to use a StackPanel instead, but StackPanel wasn’t made for displaying text. It might fool you initially, but you quickly notice it doesn’t behave anything like text: there’s no proper kerning, RTL, wrapping, or anything else the text classes were made to support.

I’m pretty surprised that WPF doesn’t have anything like this, as it seems like displaying lists as text would be a common enough thing for any app. Unfortunately I’m still not well versed enough in WPF to create such a custom control for myself, and haven’t had a whole lot of time to investigate it.

Using HSL colors in WPF

One thing that has always irritated me about WPF is you’re still stuck specifying colors in RGB. HSL just feels so much more natural from a design standpoint. Well, we’re not completely out of luck—we’ve got markup extensions.

So I created my own HslColor and HslBrush extensions which are fairly simple to use:

<Window xmlns:e="clr-namespace:WpfExtensions"
        Background="{e:HslBrush H=300,S=50,L=75,A=80}"/>

Hue is specified in degrees from 0 to 360, while Saturation, Lightness, and Alpha are from 0 to 100. The parameters are all doubles and it converts to scRGB behind the scenes, which means you actually get a much higher color precision than if you had just used the equivalent RGB hex. With Windows 7 having native support for scRGB, this will future-proof your application to make good use of upcoming monitors with Deep Color support.

C#: lamer by the moment

To my disbelief, you can’t inherit from generic type parameters in C#. Something like this is currently impossible:

class Foo<T> : T
{
}

It’s sad, really, how Microsoft continues to focus on other crap but leaves out basic things like constraint-based overload resolution or simple inheritance. And still no Spec# features.

Visual Studio 2010 CTP now available

Coinciding with the 2008 PDC, the first Visual Studio 2010 CTP is now available for download. At first glance, it includes a few interesting things for C++:

I’ll be posting more as I take a closer look at these and other features.