Declaration of Anonymous types List [duplicate]
How about dynamic? List<dynamic> dynamicList = new List<dynamic>(); dynamicList.Add(new { Name = “Krishna”, Phones = new[] { “555-555-5555”, “666-666-6666” } });
How about dynamic? List<dynamic> dynamicList = new List<dynamic>(); dynamicList.Add(new { Name = “Krishna”, Phones = new[] { “555-555-5555”, “666-666-6666” } });
This: MyClass c = new MyClass { StringCollection = { “test2”, “test3” } }; is translated into this: MyClass tmp = new MyClass(); tmp.StringCollection.Add(“test2”); tmp.StringCollection.Add(“test3”); MyClass c = tmp; It’s never trying to call a setter – it’s just calling Add on the results of calling the getter. Note that it’s also not clearing the … Read more
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
Bellow is my answer but also note that Jon Skeet spoke about it today on his blog an about the fact that he is not totally ok with the MSDN meaning of “Lazy” as MSDN isn’t really clear of what lazy exactly mean when they use it in Just how lazy are you ? his … Read more
Try the following: public static Stream ToStream(this Image image, ImageFormat format) { var stream = new System.IO.MemoryStream(); image.Save(stream, format); stream.Position = 0; return stream; } Then you can use the following: var stream = myImage.ToStream(ImageFormat.Gif); Replace GIF with whatever format is appropriate for your scenario.
This option might also prove useful: LINQ AsHierarchy() extension method http://www.scip.be/index.php?Page=ArticlesNET18
Interesting article on that here. From there … … [B]y ensuring that the members do not change, we ensure that the hash is constant for the lifetime of the object.This allows anonymous types to be used with collections like hashtables, without actually losing them when the members are modified. There are a lot of benefits … Read more
Delayed execution
Take the type/ID checking out of the recursion, so just have a “give me all the controls, recursively” method, e.g. public static IEnumerable<Control> GetAllControls(this Control parent) { foreach (Control control in parent.Controls) { yield return control; foreach(Control descendant in control.GetAllControls()) { yield return descendant; } } } That’s somewhat inefficient (in terms of creating lots … Read more
A lambda expression can either be converted to a delegate type or an expression tree – but it has to know which delegate type. Just knowing the signature isn’t enough. For instance, suppose I have: public delegate void Action1(); public delegate void Action2(); … Delegate x = () => Console.WriteLine(“hi”); What would you expect the … Read more