Archive for the ‘c#’ Category

Linq Walk

So, we all know Linq is rad. Not in that silly faux-SQL syntax, but in the manly, declarative, fluent syntax.
But the sad part is that there is no walk extension method. Walk is a functional programming staple that iterates over a collection of stuff and applies a callback to each one. In PHP, there is [...]

March 24, 2010   Posted in: c#  One Comment

Setting null values in anonymous object initializer in C#

The spec claims you can’t do this:
It is a compile-time error for an expression in an anonymous object initializer to be of the null type.

And you can’t. Directly.

var obj = new {
Value = null //syntax error: "cannot assign null to anonymous type property"
};

var obj = new {
Value = (object)null //full of [...]

February 5, 2010   Posted in: c#  2 Comments

string.Format() in PHP

.NET’s sprintf() equivalent has ordered parameters.

var s = string.Format("My name is {1}, {0}", "Tommy", "Montgomery");
//s = "My name is Montgomery, Tommy"

var s = string.Format("My name is {{{0}}}", "Tommy");
//s = "My name is {Tommy}"

I needed that. Here it is, PHPified:

function format($format) {
$args = func_get_args();
$format = array_shift($args);

preg_match_all(‘/(?=\{)\{(\d+)\}(?!\})/’, $format, $matches, PREG_OFFSET_CAPTURE);
$offset = 0;
foreach ($matches[1] as $data) {
$i = [...]

January 9, 2010   Posted in: c#, php  No Comments