I'd like to add a method to the Int32 to do a "FromSomething" stuff.  Something that could allow me to write something like this:  Int32 i = Int32.FromSomething(mySourceData);   How could I achieve this?
21166
I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console.  For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can go:  Console.WriteBlueLine("This text is blue");   I tried this by adding a local, public static method, with Console as a 'this' parameter... but no dice!      public static class Helpers {     public static void WriteBlueLine(this Console c, string text)     {         Console.ForegroundColor = ConsoleColor.Blue;         Console.WriteLine(text);         Console.ResetColor();     } }   This didn't add a 'WriteBlueLine' method to Console... am I doing it wrong? Or asking for the impossible?
38244
I have a method that's about ten lines of code.  I want to create more methods that do exactly the same thing, except for a small calculation that's going to change one line of code.  This is a perfect application for passing in a function pointer to replace that one line, but Java doesn't have function pointers.  What's my best alternative?
30163
What is the difference between this:  void MyMethod(IMyInterface value) {     //... }   and this:  void MyMethod&lt;T&gt;(T value) where T : IMyInterface {     //... }
27491
Suppose I have this interface  public interface IFoo {     ///&lt;summary&gt;     /// Foo method     ///&lt;/summary&gt;     void Foo();      ///&lt;summary&gt;     /// Bar method     ///&lt;/summary&gt;     void Bar();      ///&lt;summary&gt;     /// Situation normal     ///&lt;/summary&gt;     void Snafu(); }   And this class  public class Foo : IFoo {     public void Foo() { ... }     public void Bar() { ... }     public void Snafu() { ... } }   Is there a way, or is there a tool that can let me automatically put in the comments of each member in a base class or interface?  Because I hate re-writing the same comments for each derived sub-class!
92147
typeof(int).Name   Will return System.Int32 does anyone know of a way to return "int"
65261
What are some real world places that call for delegates?  I'm curious what situations or patterns are present where this method is the best solution.  No code required.
77376
var i : integer;  i := 1234567;   Given the above, I want the string "1,234,567" as output (assuming UK locale). IntToStr just gives me "1234567". I'm sure there's a one-liner for this, but I can't find it...
1725
I have a generic method with this (dummy) code (yes I'm aware IList has predicates, but my code is not using IList but some other collection, anyway this is irrelevant for the question...)  static T FindThing&lt;T&gt;(IList collection, int id) where T : IThing, new() {     foreach T thing in collecion     {         if (thing.Id == id)             return thing;     }     return null;  // ERROR: Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead. }   This gives me a build error      "Cannot convert null to type parameter   'T' because it could be a value type.   Consider using 'default(T)' instead."   Can I avoid this error?
81715
Edit:  Of course my real code doesn't look exactly like this. I tried to write semi-pseudo code to make it more clear of whay I wanted to do.  Looks like it just messed things up instead.  So, what I actually would like to do is this:  Method&lt;Interface1&gt;(); Method&lt;Interface2&gt;(); Method&lt;Interface3&gt;(); ...   Well ... I thought that maybe I could turn it into a loop using reflection. So the question is: How how do I do it. I have very shallow knowledge of reflection. So code examples would be great.  The scenario looks like this:  public void Method&lt;T&gt;() where T : class {} public void AnotherMethod() {     Assembly assembly = Assembly.GetExecutingAssembly();      var interfaces = from i in assembly.GetTypes()     where i.Namespace == "MyNamespace.Interface" // only interfaces stored here     select i;      foreach(var i in interfaces)     {         Method&lt;i&gt;(); // Get compile error here!     }       Original post:  Hi!  I'm trying to loop through all interfaces in a namespace and send them as arguments to a generic method like this:  public void Method&lt;T&gt;() where T : class {} public void AnotherMethod() {     Assembly assembly = Assembly.GetExecutingAssembly();      var interfaces = from i in assembly.GetTypes()     where i.Namespace == "MyNamespace.Interface" // only interfaces stored here     select i;      foreach(var interface in interfaces)     {         Method&lt;interface&gt;(); // Get compile error here!     } }   The error I get is "Type name expected, but local variable name found". If I try  ...     foreach(var interface in interfaces)     {         Method&lt;interface.MakeGenericType()&gt;(); // Still get compile error here!     } }   I get "Cannot apply operator '&lt;' to operands of type 'method group' and 'System.Type'" Any idea on how to get around this problem?
1314
I have a char in c#:  char foo = '2';   Now I want to get the 2 into an int.  I find that Convert.ToInt32 returns the actual decimal value of the char and not the number 2.  The following will work:  int bar = Convert.ToInt32(new string(foo, 1));   int.parse only works on strings as well.    Is there no native function in C# to go from a char to int without making it a string?  I know this is trivial but it just seems odd that there's nothing native to directly make the conversion.
3258
Given a method DoSomething that takes a (parameterless) function and handles it in some way. Is there a better way to create the "overloads" for functions with parameters than the snippet below?  public static TResult DoSomething&lt;TResult&gt;(Func&lt;TResult&gt; func) {     //call func() and do something else }  public static TResult DoSomething&lt;T0, TResult&gt;(     Func&lt;T0, TResult&gt; func,     T0 arg0) {     return DoSomething(() =&gt; func(arg0)); }  public static TResult DoSomething&lt;T0, T1, TResult&gt;(     Func&lt;T0, T1, TResult&gt; func,     T0 arg0, T1 arg1) {     return DoSomething(arg =&gt; func(arg, arg1), arg0); }  public static TResult DoSomething&lt;T0, T1, T2, TResult&gt;(     Func&lt;T0, T1, T2, TResult&gt; func,     T0 arg0, T1 arg1, T2 arg2) {     return DoSomething(arg =&gt; func(arg, arg1, arg2), arg0); }
13274
Does anybody have useful example of this assignment inside a C# method? I have been asked for it once during job interview, and I am still interested in answer myself.
21733
EmployeeNumber = string.IsNullOrEmpty(employeeNumberTextBox.Text)     ? null     : Convert.ToInt32(employeeNumberTextBox.Text),  I often find myself wanting to do things like this (EmployeeNumber is a Nullable&lt;int&gt; as it's a property on a LINQ-to-SQL dbml object where the column allows NULL values).  Unfortunately, the compiler feels that  There is no implicit conversion between 'null' and 'int'  even though both types would be valid in an assignment operation to a nullable int on their own. Using the null coalescing operator is not an option as far as I can see because of the inline conversion that needs to happen on the .Text string if it's not null. As far as I know the only way to do this is to use an if statement and/or assign it in two steps.  In this particular case I find that very frustrating because I wanted to use the object initializer syntax and this assignment would be in the initialization block... Does anyone know a more elegant solution?
12224
Inspired by another question asking about the missing Zip function: Why is there no ForEach extension method on the IEnumerable interface? Or anywhere? The only class that gets a ForEach method is List&lt;&gt;. Is there a reason why it's missing, maybe performance?
44000
Actionscript 3.0 (and I assume Javascript and ECMAScript in general) lacks pass-by-reference for native types like ints. As a result I'm finding getting values back from a function really clunky.  What's the normal pattern to work around this?    For example, is there a clean way to implement swap( intA, intB ) in Actionscript?
80089
I have seen a solution that I don't get to work - I need to change data type from string to integer and I did what is shown i the image below Can someone point out what I do wrong?
107627
Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.