Chris McGrath’s Blog

Just another Developer’s Blog

Posts Tagged ‘C#

In value set

leave a comment »

One thing which I quite often wish C# had a clean way to say if a variable is equal to one of many values.

Traditionally we’re stuck doing

myVal == 5 || myVal == 8 || myVal == 100;

we could say

new [] { 5, 8, 100 }.Contains(myVal);

but to me this doesn’t allow your intent to be read nicely. In fact, it just seems the wrong way around.

What we need is something like SQL’s in. so I could say

myVal in (5, 6, 100)

that reads much better. I remember that Delphi also had a similar command.

Written by Chris McGrath

July 9, 2009 at 3:38 pm

Posted in Blog

Tagged with , ,

Path.Combine Problem

leave a comment »

In a recent post I praised this method. However, I have found out a querk with it which really takes away from it’s usability.

The idea of the function is provide a location and a relative location and it creates the absolute URL. Like this…

Path.Combine(@”C:\Windows”, “System32”);

The case that it handles that makes it so useful is it figures out whether it should add a slash after the first path.

But for some reason there is a special exception – when the first string ends in a colon…

If I say

Path.Combine(“C:”, “Windows”);

I would expect the output to be
C:\Windows
but instead it’s
C:Windows

This case is outlined by the documentation but I can’t see why they did it. I posted a note about it here. And they gave the reason “intentional for FAT compatibility.” I would love if someone could give me the exact case that it is needed for.

Written by Chris McGrath

July 2, 2009 at 2:19 pm

Posted in Blog

Tagged with ,

Casting in LINQ

with one comment

I recently wrote a LINQ query on a List of WeakReferences. The target of the weak references all implemented my interface (let’s call it IMyInterface). I wanted the query to return a list of all alive IMyInterfaces. It’s a very simple query but was wondering what is the best way to cast the target to IMyInterface? The two ways are…

ref.Where(o => o.IsAlive).Select(o => (IMyInterface)o.Target).ToList();

and

ref.Where(o => o.IsAlive).Select(o => o.Target).Cast<IMyInterface>().ToList();

The traditional o-o developer in me initially to the first. But then when I thought of the second I couldn’t help but like it. It reads so well from left to right. The only ambiguity it has is whether it is casting each element or the list object to IMyInterface. Maybe it should have been called CastAll or CastEach

Written by Chris McGrath

May 11, 2009 at 12:02 pm

Posted in Blog

Tagged with , , ,

dotNet Tip: Path.Combine

leave a comment »

A long time ago when I was new to the dotNet framework I was doing a fair bit of File IO. The thing that annoyed me most was checking if a path ended with a slash before appending something to the end. I then found out about

System.IO.Path.Combine(path1, path2);

I’m just doing a bit of IO at the moment and I’m so glad I remembered this. It turns

string path = WindowsDir;
if (!path.EndsWith(‘\\’)) path += “\\”;
path += “Help”;

To simply

string path = Path.Combine(WindowsDir, “Help”);

Written by Chris McGrath

April 20, 2009 at 6:20 pm

Posted in Blog

Tagged with ,

Tip: Remember using System.Linq

leave a comment »

I was updating an old file that hadn’t been touched since dotNet 2 and wanted to use the Linq extension method ‘Select’ method. But it wasn’t showing up. I checked to make sure the assembly was dotNet 3.5 and it had a reference to System.Core. I was stumped.

Luckily it eventually came to me that I was missing System.Linq from my using list. But it’s an easy thing to miss and the Smart Tags don’t detect it. Maybe there should be an option to make it a warning if a file is missing it.

Written by Chris McGrath

April 16, 2009 at 1:02 pm

Posted in Blog

Tagged with , ,

Extended DataGridView for WinForms

leave a comment »

I while back I made a extension to the DataGridView and an article about it. For those who still have to use WinForms I would highly recommend it.

It’s avaliable on CodeProject at Extending the DataGridView.

Written by Chris McGrath

April 6, 2009 at 6:47 am

Posted in Blog

Tagged with , ,