extension-methods

Possible pitfalls of using this (extension method based) shorthand

你说的曾经没有我的故事 提交于 2019-11-26 07:41:59
问题 C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? myObject.StringProperty : null; // C#6 propertyValue1 = myObject?.StringProperty; The question below still applies to older versions, but if developing a new application using the new ?. operator is far better practice. Original Question: I regularly want to access properties on possibly null objects: string propertyValue1 = null; if( myObject1 != null ) propertyValue1 = myObject1.StringProperty; int

Extension methods syntax vs query syntax

浪子不回头ぞ 提交于 2019-11-26 07:25:17
问题 I\'m trying to get a handle on if there\'s a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, just are written differently. Is it purely a matter of style? var query = from p in Products where p.Name.Contains(\"foo\") orderby c.Name select p; // or with extension methods: var query = Products .Where(p => p.Name.Contains(\"foo\")) .OrderBy(p => p.Name); They\'re very similar with the second example being a bit more

Is there any way in C# to override a class method with an extension method?

拥有回忆 提交于 2019-11-26 06:41:12
问题 There have been occasions where I would want to override a method in a class with an extension method. Is there any way to do that in C#? For example: public static class StringExtension { public static int GetHashCode(this string inStr) { return MyHash(inStr); } } A case where I\'ve wanted to do this is to be able to store a hash of a string into a database and have that same value be used by all the classes that use the string class\'s hash (i.e. Dictionary, etc.) Since the built-in .Net

Static extension methods [duplicate]

痴心易碎 提交于 2019-11-26 06:29:35
问题 Possible Duplicate: Can I add extension methods to an existing static class? Is there any way I can add a static extension method to a class. specifically I want to overload Boolean.Parse to allow an int argument. 回答1: In short, no, you can't. Long answer, extension methods are just syntactic sugar. IE: If you have an extension method on string let's say: public static string SomeStringExtension(this string s) { //whatever.. } When you then call it: myString.SomeStringExtension(); The

Can I “multiply” a string (in C#)?

ぃ、小莉子 提交于 2019-11-26 06:24:54
问题 Suppose I have a string, for example, string snip = \"</li></ul>\"; I want to basically write it multiple times, depending on some integer value. string snip = \"</li></ul>\"; int multiplier = 2; // TODO: magic code to do this // snip * multiplier = \"</li></ul></li></ul>\"; EDIT: I know I can easily write my own function to implement this, I was just wondering if there was some weird string operator that I didn\'t know about 回答1: In .NET 4 you can do this: String.Concat(Enumerable.Repeat(

In C#, what happens when you call an extension method on a null object?

若如初见. 提交于 2019-11-26 06:14:24
问题 Does the method get called with a null value or does it give a null reference exception? MyObject myObject = null; myObject.MyExtensionMethod(); // <-- is this a null reference exception? If this is the case I will never need to check my \'this\' parameter for null? 回答1: That will work fine (no exception). Extension methods don't use virtual calls (i.e. it uses the "call" il instruction, not "callvirt") so there is no null check unless you write it yourself in the extension method. This is

Impossible to use ref and out for first (“this”) parameter in Extension methods?

不打扰是莪最后的温柔 提交于 2019-11-26 05:58:42
问题 Why is it forbidden to call Extension Method with ref modifier? This one is possible: public static void Change(ref TestClass testClass, TestClass testClass2) { testClass = testClass2; } And this one not: public static void ChangeWithExtensionMethod(this ref TestClass testClass, TestClass testClass2) { testClass = testClass2; } But why? 回答1: You have to specify ref and out explicitly. How would you do this with an extension method ? Moreover, would you really want to? TestClass x = new

Java equivalent to C# extension methods

我的未来我决定 提交于 2019-11-26 05:58:07
问题 I am looking to implement a functionality in a list of object as I would in C# using an extension method. Something like this: List<DataObject> list; // ... List initialization. list.getData(id); How do I do that in Java? 回答1: Java does not support extension methods. Instead, you can make a regular static method, or write your own class. 回答2: Extension methods are not just static method and not just convenience syntax sugar, in fact they are quite powerful tool. The main thing there is

Anonymous Types - Are there any distingushing characteristics?

六眼飞鱼酱① 提交于 2019-11-26 05:31:44
问题 Is there anything to use, to determine if a type is actually a anonymous type? For example an interface, etc? The goal is to create something like the following... //defined like... public static T Get<T>(this IAnonymous obj, string prop) { return (T)obj.GetType().GetProperty(prop).GetValue(obj, null); } //... //And then used like... var something = new { name = \"John\", age = 25 }; int age = something.Get<int>(\"age\"); Or is that just the beauty of an anonymous type? Nothing to identify it

Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?

末鹿安然 提交于 2019-11-26 05:26:13
问题 In the course of my maintenance for an older application that badly violated the cross-thread update rules in winforms, I created the following extension method as a way to quickly fix illegal calls when I\'ve discovered them: /// <summary> /// Execute a method on the control\'s owning thread. /// </summary> /// <param name=\"uiElement\">The control that is being updated.</param> /// <param name=\"updater\">The method that updates uiElement.</param> /// <param name=\"forceSynchronous\">True