Tag Archives: c#

LINQ LINQ LINQ

I’ve been doing a lot of research on LINQ for episode 6 of the Coding Blocks podcast and I was a bit surprised by what I came up with.

I had originally thought of LINQ as a feature. I had heard the parable of the guy at the white board, writing what they thought code should look like and then worked backwards on how to get there. This makes sense, but what surprised me was just how much of the building blocks were already there.

I wrote a blog post about it over at codingblocks.net, so go check it out: What’s So Special About LINQ?.

ColorMine.org - Color Converters and Delta E Calculators

ColorMine.org

I’m still putting together my 2013 goals, but I do know that one of them is to launch more sites.

I’ve been doing this whole internet thing for a long time now, I really aught to have more to show for it.

Knowing that perfect is the enemy of good enough I’ve opted to take a release early and release often approach to launching sites.

First up is colormine.org. It’s a simple site that wraps a small color library I wrote.
Continue reading

To uint, or not to uint

I like unsigned integers, always have. It’s more correct, concise and expressive, you get more (positive) space out of it and you’re preventing bugs by design. What’s not to love?

Well…

I’ve been nooking CLR via C# and it’s a fantastic read for anyone who wants to ‘get serious’ about .Net. The book’s so crammed full of good information it’s hard not to gush, but I digress.

I was nooking, you see, and I came across this recommendation:

“Use signed data types (such as Int32 and Int64 instead of unsigned numeric types such as UInt32 and UInt64) wherever possible.”

Say it aint so!

Jeffrey Richter continues:

“This allows the compiler to detect more overflow/underflow errors. In addition, various parts of the class library (such as Array’s and String’s Length properties) are hard-coded to return signed values, and less casting is required as you move these values around in your code.”

Casting is ugly, no argument there but I don’t care much about the overflow/underflow errors. I generally don’t enable overflow checking, there’s no political statement here, it’s just never come up.

And it still feels wrong for me to declare a value signed when I know that it shouldn’t be.

(Richter recommends enabling checking for debug builds, and disabling for release. Sound advice!)

But here’s real the kicker for me:

In addition, unsigned numeric types are not CLS-compliant.

Doh. I did a bit of searching to try and track down why it’s not part of the CLS, and I found that Brad Abrams expresses it best in this post:

“The general feeling among many of us is that the vast majority of programming is done with signed types. Whenever you switch to unsigned types you force a mental model switch (and an ugly cast). In the worst cast you build up a whole parallel world of APIs that take unsigned types. The value of avoiding the ‘< 0' check is not worth the inclusion of generics in the CLS"

Alright, I give in. I’m apparently in the minority, these guys are way smarter than I am, and it is easier, if not more concise. So it goes.

Goodbye for now uint, you’ll always hold a positive place in my heart.

Just like Weezer:

Flood It .NET

I’ve been playing a bit with Silverlight this weekend, and I made a little app based on little game called Flood-It! Unfortunately, at the time I started I couldn’t remember the name of the app, so it’s a bit of an interpretation…

Anyhow, the goal of the game is to ‘flood’ the screen with all one color. It’s a bit difficult to explain, so just click around a bit and it will start making sense.

So far I’ve really liked working in Silverlight, it’s very similar to Flex except that C# blows ActionScript out of the water. I’ve got a few more little projects I’d like to try before I give my final verdict, so don’t touch that dial!

Delving into C#

This year I’ve decided to really get into C#. My .NET experience is shall at best so aiming to rectify, I picked up C# in Depth and commenced skimming!

Now, I’ve made my fair share of M$ snide asides, but I’m having a hard time coming to gripes with C#. Everything I run into either “just works” or exceeds my expectations. And the cool features are in fact, quite cool! Color me impressed!

Noob!

Noob!


For fun I rewrote a few of my Project Euler Solutions to buff up on the syntax. After I got the semi-colons and brackets all figured out, I moved on to something a little bigger.

I wanted a simple program to run and benchmark my solutions, so I wouldn’t have to do as much leg work every time I converted a problem. I figured this would be a simple enough thing to do, and it would provide a good foundation for a future gui application and beginning unit testing.

I wanted to share some particulars that I thought were pretty cool, you can grab the code I’m talking about from the google code repository, and follow along…or something.

Generics, Delegates and Lambdas
Generic Collections provide a data structure that I can access and use just like an array, but also provides methods for dealing with delegates.

Delegates are very similar to closures, blocks, procs, and lambdas like I’ve worked with in other languages, so the transition was smooth. The lambda syntax was particularly reminiscent of pythonic list comprehensions.

Thanks to delegates, I can turn this:

var matchingTypes = new List<Type>();
foreach(t in CurrentTypes) {
	if(t.IsSubclassOf(parentType) {
		matchingTypes.Add(t);
	}
}
return matchingTypes;

Into this:

return CurrentTypes.FindAll(
	delegate(Type t)
	{
		return t.IsSubclassOf(parentType);
	}
);

And finally, via lambda, to this!

return CurrentTypes.FindAll(
	t => t.IsSubclassOf(parentType)
);

Not too shabby, eh?

Reflection
Most of my programming has been in ColdFusion, JavaScript and Ruby. There’s been a little bit of this and a little bit of that peppered in there, particually C and Java while I was at UCF, but for the most part I’ve enjoyed working with dynamic and/or interpreted languages. Meta-programming is common in these types of languages, but I was surprised and impressed to read up on reflection. In this case, reflection allows me to dynamically detect and run my problems, which makes it easier (and cleaner) for me to add new solutions.

Here’s a simplified “ClassMaster” class I use to wrap my reflection calls for listing and creating classes, so you can see what I’m on about:

class ClassMaster
{
	private Assembly CurrentAssembly { get; set; }
	private List<Type> CurrentTypes { get; set; }

	public ClassMaster()
	{
		CurrentAssembly = Assembly.GetExecutingAssembly();
		CurrentTypes = new List<Type>(CurrentAssembly.GetTypes());
	}

	// should probably take arguments to pass thru...somehow
	public Object CreateClass(Type classType)
	{
		return CurrentAssembly.CreateInstance(classType.FullName);
	}

	public List<Type> getTypesByParentClass(Type parentType)
	{
		return CurrentTypes.FindAll(
			t => t.IsSubclassOf(parentType)
		);
	}
}

That’s it for now. I’ll be looking into LINQ and unit testing in the next couple weeks, and then I’m on to the gui. ASP, SilverLight, and WPF here I come!

Here are those links again:
Release
Latest Version