Greg Beech's Website

Greg Beech's Tech Blog

  • Emitting decimal pseudo-constants with Reflection.Emit

    Some languages such as C# let you treat the System.Decimal type as an integral type most of the time, even having the keyword decimal , and will let you declare constant fields of the type under some circumstances. However, decimal isn't a primitive type in the same way as the other numerics, and is implemented quite differently from them. At CIL level, you can't actually declare a constant...
  • Why doesn't System.Linq.Enumerable have a ForEach extension method?

    I don't know for sure, but my guess is because there are at least two implementations with different semantics: // immediate execution public static IEnumerable <T> ForEach<T>( this IEnumerable <T> source, Action <T> action) { foreach ( var item in source) { action(item); } return source; } // deferred execution public static IEnumerable <T> ForEach<T>( this...
    Filed under: ,
  • Ivory tower protocol wars

    There seem to be a lot of blog entries at the moment arguing over the relative merits of XML, JSON, YAML, S-Expressions, etc. as a data interchange format, and Web Services, WS-* Services and REST-style as the application protocol implementation. But while various articles make interesting academic points about which format is more human readable/easier to parse/represents numbers natively/allows cyclic...
  • Enabling WS-AtomicTransaction in WCF services and clients

    Support for all the WS-* standards is built into WCF and doesn't require any complex code to get working, but like all things in the WCF world does require a fair bit of attribution and configuration before it all hangs together. It took me a while to work out exactly which combination of bindings and behaviours and attributes would get transaction flow working between two WCF endpoints, so to...
    Filed under: ,
  • Why do iterators have deferred execution semantics?

    After my post about deferred execution semantics and re-entrancy I've had a couple of people ask why iterator code has deferred execution semantics rather than just executing immediately. To understand this we need to see how iterators are actually implemented. Consider the following very simple iterator method which produces a range of integers: public static IEnumerable < int > Range( int...
    Filed under: , ,
  • Retrieving a row exactly once with multiple polling processes in SQL Server

    If you're writing a message processing application based around SQL Server, then it might be tempting to use the service broker functionality. However if all you need to do is allow your host processes to periodically retrieve a batch of messages from a table, then using service broker could be like using a sledgehammer to crack a walnut. All you really need is a stored procedure which ensures...
    Filed under:
  • A functional approach to data access code, updated for C# 3.0

    I've been a bit too busy with work and other things to finish the Linq translation posts, though I will return to them when I get some free time. For now I'll post some other things I had already drafted, and which are probably more interesting to most people anyway. Last year I noted that most data access code follows very similar patterns, and described how higher order procedures can be...
  • Translating C# 3.0 query syntax for Linq-to-Objects, Part 4: Let

    This is the fourth entry in a series demonstrating how C# 3.0 query syntax is translated into the underlying method calls for Linq-to-Objects, and that you can write your own versions of the Linq extension methods. The previous entries were: Part 1: Introduction and Select Part 2: SelectMany Part 3: Where I know I said last time that this post would be about ordering, but now seemed an opportune point...
    Filed under: ,
  • Translating C# 3.0 query syntax for Linq-to-Objects, Part 3: Where

    This is the third entry in a series demonstrating how C# 3.0 query syntax is translated into the underlying method calls for Linq-to-Objects, and that you can write your own versions of the Linq extension methods. The previous entries were: Part 1: Introduction and Select Part 2: SelectMany Now we know how basic queries to select and project data are translated, we'll start looking at the more...
    Filed under: ,
  • Translating C# 3.0 query syntax for Linq-to-Objects, Part 2: SelectMany

    This is the second entry in a series demonstrating how C# 3.0 query syntax is translated into the underlying method calls for Linq-to-Objects, and that you can write your own versions of the Linq extension methods. In the first entry I outlined the types and collections I'd be using for this series, and looked at the translation of a simple Linq query with a single from clause to the Select extension...
    Filed under: ,
  • Translating C# 3.0 query syntax for Linq-to-Objects, Part 1: Select

    The Linq query syntax introduced in C# 3.0 is an extremely powerful way to manipulate collections of objects. Depending on the query provider the expression may be translated in a number of ways, such as SQL statements for Linq-to-SQL or XQuery expressions for Linq-to-XML , but the simplest is when you're dealing with collections of normal objects and the syntax is translated into method calls;...
    Filed under: ,
  • To var or not to var, implicit typing is the question

    The introduction of the var keyword in C# 3.0 was required to support anonymous types, however it may also be used to declare variables for named types. This seems to have sparked quite a lot of debate about where the use of var is appropriate. The C# Reference documentation page (linked previously) suggests that: Overuse of var can make source code less readable for others. It is recommended to use...
    Filed under:
  • Generating COM Interop types from IDL

    Recently I wrote a managed wrapper for BITS to use in blinkBox 's download manager application, which necessitates COM interop. This was made a little tricky due to the fact that BITS doesn't ship with primary interop assemblies or a type library; simply with an IDL interface definition file. However, using some command line tools it's quick to translate the IDL file into C# interop types...
  • Expressions involving deferred execution should be re-entrant

    If you create a method to return a random set of items from an enumerable list, implemented using an iterator... public static IEnumerable<T> GetRandom<T>(this IEnumerable<T> items) { var random = new Random(); foreach (var item in items) { if (random.Next() < 0.5) { yield return item; } } } ...then use it like this, where PrintColors uses a foreach loop to print each colour.....
  • Not using negatives does not make your code less readable

    Most people find negative statements more difficult to comprehend than positive statements. You probably had to double-check the title of this entry to see if it made sense - I know I did! This isn't surprising if you think about it because a positive statements presents a concept which can be used directly, whereas a negative statement presents a concept and then says "the opposite of that"...
    Filed under: ,
1 2 3 4 Next >
Copyright (C) Greg Beech. All rights reserved.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems