How to dynamic new Anonymous Class?

Anonymous types are just regular types that are implicitly declared. They have little to do with dynamic. Now, if you were to use an ExpandoObject and reference it through a dynamic variable, you could add or remove fields on the fly. edit Sure you can: just cast it to IDictionary<string, object>. Then you can use … Read more

Func delegate with ref variable

It cannot be done by Func but you can define a custom delegate for it: public delegate object MethodNameDelegate(ref float y); Usage example: public object MethodWithRefFloat(ref float y) { return null; } public void MethodCallThroughDelegate() { MethodNameDelegate myDelegate = MethodWithRefFloat; float y = 0; myDelegate(ref y); }

Extension methods require declaring class to be static

It’s dictated in the language specification, section 10.6.9 of the C# 4 spec: When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers … Read more

Converting integers to roman numerals

Try this, simple and compact: public static string ToRoman(int number) { if ((number < 0) || (number > 3999)) throw new ArgumentOutOfRangeException(“insert value betwheen 1 and 3999”); if (number < 1) return string.Empty; if (number >= 1000) return “M” + ToRoman(number – 1000); if (number >= 900) return “CM” + ToRoman(number – 900); if (number … Read more

What task is best done in a functional programming style?

I’ve just recently discovered the functional programming style […] Well, recently I was given a chance to give a talk on how to reduce software development efforts, and I wanted to introduce the concept of functional programming. If you’ve only just discovered functional programming, I do not recommend trying to speak authoritatively on the subject. … Read more

How to create a dynamic LINQ join extension method

I’ve fixed it myself now. It was a schoolboy error passing too many parameters to the CreateQuery(… ) call. Paste the following code into the Dynamic.cs file within the DynamicQueryable class for a dynamic Join extension method. You can find the source for the DynamicQuery sample project at http://code.msdn.microsoft.com/csharpsamples. Enjoy. public static IQueryable Join(this IQueryable … Read more