C#

Is C# the Boost of C-family languages?

For all the cons of giv­ing a sin­gle en­tity con­trol over C#, one pro is that it gives the lan­guage an un­matched agility to try new things in the C fam­ily of lan­guages. LINQ—both its lan­guage in­te­gra­tion and its back­ing APIs—is an in­cred­i­bly pow­er­ful tool for query­ing and trans­form­ing data with very con­cise code. I re­ally can’t ex­press how much I’ve come to love it.

The new async sup­port an­nounced at PDC10 is ba­si­cally the holy grail of async cod­ing, let­ting you focus on what your task is and not how you’re going to im­ple­ment a com­plex 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 suc­cess­fully im­ple­mented sim­ply be­cause it re­quired too much lan­guage sup­port.

The lack of peer re­view and stan­dards com­mit­tee for .​NET shows—there’s a pretty high rate of turnover as Mi­crosoft tries to iron down the right way to tackle prob­lems, and it re­sults in a very large li­brary with lots of re­dun­dant func­tion­al­ity. As much as this might hurt .​NET, I’m start­ing to view C# as a sort of Boost for the C lan­guage fam­ily. Some great ideas are get­ting real-​world use, and if other lan­guages even­tu­ally feel the need to get some­thing sim­i­lar, they will have a bounty of ex­pe­ri­ence to pull from.

C++, at least, is a ter­ri­fy­ingly com­plex lan­guage. Get­ting new fea­tures into it is an up­hill bat­tle, even when they ad­dress a prob­lem that every­one is frus­trated with. Get­ting com­plex new fea­tures like these into it would be a very long process, with a lot of ar­gu­ing and years of delay. Any extra in­cu­ba­tion time we can give them is a plus.

Alex Filo has the right idea, UniformWrapPanel

WPF’s Wrap­Panel is miss­ing a key fea­ture: the abil­ity to cre­ate ver­ti­cal columns, but still fill the max­i­mum amount of width avail­able. One less an­noy­ance in WPF, thanks to Alex Filo’s Uni­formWrap­Panel.

Databinding TextBlocks with XAML

I’ve be­come frus­trated lately with try­ing to data­bind a list of strings to a textblock. Ie, if I have:

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

And I want the ef­fec­tive end re­sult to be:

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

Or maybe I want some­thing more com­plex like hy­per­links:

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

Ba­si­cally what I’m look­ing for is a Item­sCon­trol that works on TextBlocks (or Spans, etc.), with a Sep­a­ra­tor tem­plate to in­sert those “, ” in be­tween the items.

Your first in­stinct might be to use a Stack­Panel in­stead, but Stack­Panel wasn’t made for dis­play­ing text. It might fool you ini­tially, but you quickly no­tice it doesn’t be­have any­thing like text: there’s no proper kern­ing, RTL, wrap­ping, or any­thing else the text classes were made to sup­port.

I’m pretty sur­prised that WPF doesn’t have any­thing like this, as it seems like dis­play­ing lists as text would be a com­mon enough thing for any app. Un­for­tu­nately I’m still not well versed enough in WPF to cre­ate such a cus­tom con­trol for my­self, and haven’t had a whole lot of time to in­ves­ti­gate it.

Using HSL colors in WPF

One thing that has al­ways ir­ri­tated me about WPF is you’re still stuck spec­i­fy­ing col­ors in RGB. HSL just feels so much more nat­ural from a de­sign stand­point. Well, we’re not com­pletely out of luck—we’ve got markup ex­ten­sions.

So I cre­ated my own Hsl­Color and HslBrush ex­ten­sions which are fairly sim­ple to use:

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

Hue is spec­i­fied in de­grees from 0 to 360, while Sat­u­ra­tion, Light­ness, and Alpha are from 0 to 100. The pa­ra­me­ters are all dou­bles and it con­verts to scRGB be­hind the scenes, which means you ac­tu­ally get a much higher color pre­ci­sion than if you had just used the equiv­a­lent RGB hex. With Win­dows 7 hav­ing na­tive sup­port for scRGB, this will fu­ture-proof your ap­pli­ca­tion to make good use of up­com­ing mon­i­tors with Deep Color sup­port.

C#: lamer by the moment

To my dis­be­lief, you can’t in­herit from generic type pa­ra­me­ters in C#. Some­thing like this is cur­rently im­pos­si­ble:

class Foo<T> : T
{
}

It’s sad, re­ally, how Mi­crosoft con­tin­ues to focus on other crap but leaves out basic things like con­straint-based over­load res­o­lu­tion or sim­ple in­her­i­tance. And still no Spec# fea­tures.

Visual Studio 2010 CTP now available

Co­in­cid­ing with the 2008 PDC, the first Vi­sual Stu­dio 2010 CTP is now avail­able for down­load. At first glance, it in­cludes a few in­ter­est­ing things for C++:

I’ll be post­ing more as I take a closer look at these and other fea­tures.