C# 4.0 is to introduce the static type “dynamic” (and other things)

At Microsoft’s Professional Developer Conference (PDC) yesterday, Anders Hejlsberg unveiled plans for C# version 4.

One of the key changes is the introduction of a new type called “dynamic”. By declaring a variable of type dynamic, the compiler makes no attempt to resolve any of its members at compile time, instead performing late-binding checks at run time. This feature enables C# to use the DLR, and to use DLR-based Python and Ruby libraries.

Another key change, is the inclusion of named and optional parameters. Default values can now be specified for a method’s parameters, and parameters can be specified in any order in a call by using the parameter := value convention to name the parameters. This, combined with dynamic types, suddenly makes COM invocation a lot simpler. For example, the current mess of “ref missing” parameters will just go away.

There are various other additions, such as covariance and contravariance for generics (which I do not yet fully understand). You can read more details of the changes by downloading the document “New features in C# 4.0

If you are eager to try out an early CTP of VS2010, with .NET 4  and C# 4, you can download the 7.5Gb virtual PC image from here.

MIX:UK is dead! Long live ReMix UK!

ReMix UK

Last year, Microsoft hosted the first ever MIX:UK event, a two day conference on all things Microsoft, of interest to developers and designers alike. I was beginning to think that the first MIX conference in the UK would be the last as there was no word anywhere that another was on its way. However never fear, MIX:UK is back, just re-badged as ReMix UK. As a bonus (for me at least; it’ll be a pain for many), the venue has moved from London to glorious Brighton this year.

Despite being an Eclipse, Java & Flex man these days, the new technologies – such as Silverlight – that Microsoft are pushing are still likely to be hugely influential to anyone involved with web or RIA development. So the event is well worth attending in my view. Even my MS hating, Mac weilding boss agreed (once he has stopped laughing at my “may I go to a Microsoft conference please?” request and realised I was being serious for once).

If you are thinking of going, don’t delay. The sneaky folk that are organising the event are only offering the £239 “Early Bird” price to the first 300 registrants. After that, the price jumps to £349. So get registering and I’ll see you there…

All change at Arno#

Today was the day when my employment with Eurotherm Ltd finished and my employment with Enigma Data Solutions began. I swapped a 45 minute drive through the traffic jams of the A27 for a traffic jam on the A22 due to an overturned lorry. Doh! Luckily though I was only delayed by 10 minutes. Some in the office – who came from the opposite direction – were stuck for two hours. Ouch!

The new job will involve ActionScipt 3 (which I love already; it is such a huge improvement over ActionScript 2) and Java. This blog has been very Microsoft-centric (especially with regard to C#) up until now, but I may not use C# again for some time. This raises some issues for me:

  1. Do I abandon the C# tutorial series?
  2. Do I scrap the Arno# name?
  3. Do I scrap my plans to get involved in Pash?
  4. How will I convince my new boss that sending me to Mix:UK this year would be worth their while?
  5. Do I write some decent documentation on flexunit?

The last question is a no-brainer. I’ve started experimenting with flexunit, and it is a good unit testing framework for flex. The documentation for it is utterly crap though. I’ve worked out how to use it mainly by reading through the source code! So watch this space for some tutorials on using flexunit.

As for the others, time will tell…

C# “How To” Article Section Launched

c_sharp.pngI’m a big fan of C# and so have decided to launch my first side project on that subject: the C# “How To” Articles section. There are only two articles there at the time of writing, but I hope to add roughly an article a week. Thus over the coming months, it will hopefully grow into a truly useful resource.

There are of course many C# resources out there already, and so you might ask why I’m bothering. Take a look at the articles and hopefully you’ll see why. There are lots of little known, but often very useful, features tucked away in the C# language. My set of articles aims to explore these, at least as a starting point.

C# and Friend Assemblies Made Easy

friendassembly_small.pngIf you are familiar with C++, you’ll know about “friend classes” within that language. If you are not familiar with them, they are a neat way of exposing otherwise private methods to trusted classes. Class A has a bunch of private members, that are obviously hidden to other classes, but there is a problem with this model. Class B is a unit test class that tests class A. Because it is doing unit tests, it needs to delve deep inside class A to check what is going on. By making class B a friend of class A, the former is then granted access to the latter’s inner workings.

So what does this have to do with C#? Well C# partially fixed this with the “internal” keyword. Internal members of a class are visible to other classes within the same assembly, but are effectively private as far as classes outside the assembly are concerned. Internal members are very different from protected ones by the way. In the latter case, they are only visible to subclasses (whether in the same assembly or not). However, largely due to a limitation with Visual Studio, internal members used not to be the full answer to the needs of unit testing. The C# compiler supports compiling multiple projects into one assembly, but Visual Studio has a strict one to one project/ assembly mapping. So unit test classes used to have to reside within the same project as the product code (which wasn’t practical if the project’s assembly was a executable, rather than a dll.) The other alternative was to expose all the members that the unit test classes needed to access by making them public.

I talk in the past tense in the previous paragraph as this problem was fixed with the release of C# 2.0. This introduced the idea of Friend Assemblies. To explain, let’s go back to our class A and B. Class A resides in assembly A.dll. Class B resides in assembly B.dll. Normally class B would have no access to the internals of class A. Add the “InternalsVisibleTo” attribute to class A though, specifying B.dll as the trusted assembly and now class B can access those internals. Further, unlike with C++ and friend classes, private members in class A remain private; only internals are exposed via this method.

If both A.dll and B.dll are unsigned, the extra code is just:

which is a pretty neat solution.

Things get really messy though the moment signed assemblies are used. The reason is that one must put the public key (not the nice short token, the full-blown humongous key) in the attribute. So it becomes something like:

(I’ve wrapped the public key across many lines for clarity. In reality, it must be on one line)

The whole horrible nastiness of the matter doesn’t stop there though. To get that public key, one must find the sn.exe tool, plough through its dozens of obscure options, and then run the right one against the required assembly (or run a sequence of two even more obscure commands against the original key file). Then one can cut and paste the output of the command into the attribute in Visual Studio. This is what we in Britain politely term a “complete faff”.

Having banged my head against the wall performing this operation recently, I decided that a simply little utility was required to do all the work. So I wrote an app called FriendAssembly, which I’m sharing here.

Continue reading “C# and Friend Assemblies Made Easy”

Parallel.For quirk can leave your code running sequentially

I have been experimenting with the Task Parallel Library December CTP in preparation for a talk at work, and in the process I came across a really weird feature that turns out to have a completely mundane solution. As it will likely catch others out too, I’ve decided to write a blog entry about it.

If you try taking the following C# code:

and convert it to use the new parallel for loop:

and run it on a computer with four cores, you might expect an output such as:

You will however be very disappointed with the result if you do, as the whole loop actually runs on one thread in a sequential fashion. It turns out that the reason for this is simple practicalities of getting this stuff working at a basic level for the CTP. As I discovered on the MSDN parallel forum, the reason is that the Parallel.For method breaks the loop up into groups of eight iterations and assigns each group of eight to the next available thread. So if your loop is of eight or less iterations, then it will run sequentially. You can test this by simply adjusting the for loop to 0 .. 15 say and it will use two threads if you have two cores etc.

More VS2008 goodies: Silverlight and Visual Studio Shell

VS2008Silverlight 1.1 Tools Alpha for Visual Studio 2008

The VS2008 development tools for Silverlight 1.1 have been re-released and now work with the release version of VS2008. The tools ad-on can be downloaded from here.

Visual Studio 2008 Shell

Do you have a burning desire to develop your own IDE? Perhaps you have an amazing new feature to add to VS200? Either way, Visual Studio 2008 Shell may be just what you need. It comes in two flavours: integrated mode and standalone mode. In the first case, it is effectively a development framework for your amazing new feature that enables it to integrate seamlessly into VS2008. In the second case, it is the skeleton of an IDE that you can customise to your requirements and re-badge to fit your brand image, rather than Microsoft’s.

You can read more about it here.

Microsoft release a whole bundle of developer goodies

microsoft.pngMicrosoft saw fit to release a bunch of new stuff yesterday:

Visual Studio 2008
The express and team editions are available to download from MSDN (if you have a subscription). The express edition is available here (you’ll need Silverlight installed to access it), and a 90 day trial of the team addition can be downloaded from here.

You can read more about this release on Scott Guthrie’s blog and Somasegar’s blog.

.NET Framework 3.5
The .NET Framework 3.5 is bundled in with Visual Studio 2008, but it is also released as a separate download from here. Finally LINQ is let loose upon the great unwashed. Yah!

Sql Server 2008 CTP
The latest Community Technology Preview has been released for Sql Server 2008. More details here.

Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint 2007 File Formats
Version 3 of this package that enables you to read Ofiice 2007 files on Office XP/ 2003 was released a couple of days ago too. It can be downloaded from here. IMPORTANT: read the instructions carefully; you must install all priority updates before installing this support pack.

VLite 1.1 Beta 2
OK this isn’t a Microsoft product, but it’s a great one anyway and a new version was released yesterday, so I’m giving it a mention. VLite is a application that enables one to build a lightweight version of Vista with all the “bloatware” stripped out. The latest version can be downloaded from here.