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

How to unsubscribe from an event which uses a lambda expression?

First of all… yes its a good way of doing it, it’s clean, small form and easy to read & understand… the caveat of course is “Unless you later want to unsubscribe”. I believe Jon Skeet pointed out before that “the specification explicitly doesn’t guarantee the behaviour either way when it comes to equivalence of … 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

List readonly with a private set

I think you are mixing concepts. public List<string> myList {get; private set;} is already “read-only”. That is, outside this class, nothing can set myList to a different instance of List<string> However, if you want a readonly list as in “I don’t want people to be able to modify the list contents“, then you need to … 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

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); }

C# 3.0 generic type inference – passing a delegate as a function parameter

Maybe this will make it clearer: public class SomeClass { static void foo(int x) { } static void foo(string s) { } static void bar<T>(Action<T> f){} static void barz(Action<int> f) { } static void test() { Action<int> f = foo; bar(f); barz(foo); bar(foo); //these help the compiler to know which types to use bar<int>(foo); bar( … Read more

In C#, how can I know the file type from a byte[]?

As mentioned, MIME magic is the only way to do this. Many platforms provide up-to-date and robust MIME magic files and code to do this efficiently. The only way to do this in .NET without any 3rd party code is to use FindMimeFromData from urlmon.dll. Here’s how: public static int MimeSampleSize = 256; public static … Read more