File this in my learn something new every day bucket. I received an email from Steve Maine after he read a blog post in which I discuss the anonymous object as dictionary trick that Eilon came up with. He mentioned that there is an object initializer syntax for collections and dictionaries. var foo = new Dictionary<string, string>()
{
{ "key1", "value1" },
{ "key2", "value2" }
};
That’s pretty nice!
Here is a post by Mads Torgersen about collections and collection initializers.
Naturally someone will mention that the Ruby...
David Meyer recently published a .NET class library that enables duck typing (also sometimes incorrectly described as Latent Typing as Ian Griffiths explains in his campaign to disabuse that notion) for .NET languages.
The term duck typing is popularly explained by the phrase
If it walks like a duck and quacks like a duck, it must be a duck.
For most dynamic languages, this phrase is slightly inaccurate in describing duck typing. To understand why, let’s take a quick look at what duck typing is about.
Duck Typing Explained
Duck typing allows an object to be passed in to a method that expects a certain...
I have a confession. I am an Open Source developer and my platform of choice is the .NET framework by Microsoft. Not only that, I actually believe it is a good thing to support Open Source projects in the Microsoft ecosystem. Yeah, really. In response to this tidbit, I have heard and am bracing to hear replies such as... Why would you do that (support Open Source on Microsoft)? Or It can’t be Open Source if it’s on the Microsoft platform. One rationale given is that it ain’t really Open Source unless the entire stack is Open Source. I call this the all or nothing...
System.IO.Path
How often do you see code like this to create a file path?
public string GetFullPath(string fileName)
{
string folder = ConfigurationManager.AppSettings["somefolder"];
return folder + fileName;
}
Code like this drives me crazy because it is so prone to error. For example, when you set the folder setting, you have to remember to make sure it ends with a slash. Having too many things to remember makes this setup fragile.
Sure, you write some code to ensure that the folder has an ending slash, but I’d rather let someone write that code. For example, Microsoft.
The .NET framework is definitely huge so it can...
In a recent post, I compared the expressiveness of the Ruby style of writing code to the current C# style of writing code. I then went on and demonstrated one approach to achieving something close to Ruby’s expressiveness using Extension Methods in C# 3.0. The discussion focused on how well each code sample expresses the intent of the author. Let’s look at the comparison: Ruby: 20.minutes.ago C#: DateTime.Now.Subtract(TimeSpan.FromMinutes(20)); C# 3.0 using Extension Methods: 20.Minutes().Ago(); It seems obvious to me that the C# 3.0 example is more expressive than the classic C# approach, but not everyone agrees. Several people...
UPDATE: Looks like Ian Cooper had posted pretty much the same code in the comments to Scott’s blog post. I hadn’t noticed it. He didn’t have a chance to compile it, so consider this post a validation of your example Ian! :) Scott Hanselman recently wrote a post about how Ruby has tits or is the tits or something like that. I agree with much of it. Ruby is in many respects a nice language to use if you think in Ruby. One of the comparisons of the syntactic sugar Scott showed was this: Java: new Date(new Date().getTime()...