extension-methods

Is it possible to extend arrays in C#?

喜夏-厌秋 提交于 2019-11-29 03:51:36
I'm used to add methods to external classes like IEnumerable. But can we extend Arrays in C#? I am planning to add a method to arrays that converts it to a IEnumerable even if it is multidimensional. Not related to How to extend arrays in C# static class Extension { public static string Extend(this Array array) { return "Yes, you can"; } } class Program { static void Main(string[] args) { int[,,,] multiDimArray = new int[10,10,10,10]; Console.WriteLine(multiDimArray.Extend()); } } Yes. Either through extending the Array class as already shown, or by extending a specific kind of array or even a

Does Array.ToArray<>() return the original array if it is the same type?

心已入冬 提交于 2019-11-29 02:48:51
I deal with a framework on a daily basis where we sometimes provide methods that accept IEnumerable<MyBusinessObject> as a parameter in order to show user interfaces, perform calculations etc. If I pass in an array of MyBusinessObject like so: MyBusinessObject[] myArray = new MyBusinessObject { obj1, obj2, ..., objN }; frameworkClass.MyMethod(myArray); .... public class FrameworkClass { public void MyMethod(IEnumerable<MyBusinessObject> objs) { // Other code that uses the enumerable MyBusinessObject[] objectArray = objs.ToArray(); // More code that uses the enumerable } } Does the line objs

Existing LINQ extension method similar to Parallel.For? [duplicate]

自作多情 提交于 2019-11-29 02:32:55
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> The linq extension methods for ienumerable are very handy ... but not that useful if all you want to do is apply some computation to each item in the enumeration without returning anything. So I was wondering if perhaps I was just missing the right method, or if it truly doesn't exist as I'd rather use a built-in version if it's available ... but I haven't found one

Overriding ToString() of List<MyClass>

烈酒焚心 提交于 2019-11-28 21:17:06
I have a class MyClass, and I would like to override the method ToString() of instances of List: class MyClass { public string Property1 { get; set; } public int Property2 { get; set; } /* ... */ public override string ToString() { return Property1.ToString() + "-" + Property2.ToString(); } } I would like to have the following: var list = new List<MyClass> { new MyClass { Property1 = "A", Property2 = 1 }, new MyClass { Property1 = "Z", Property2 = 2 }, }; Console.WriteLine(list.ToString()); /* prints: A-1,Z-2 */ Is it possible to do so? Or I would have to subclass List<MyClass> to override the

Extension interface patterns

我与影子孤独终老i 提交于 2019-11-28 21:13:08
The new extensions in .Net 3.5 allow functionality to be split out from interfaces. For instance in .Net 2.0 public interface IHaveChildren { string ParentType { get; } int ParentId { get; } List<IChild> GetChildren() } Can (in 3.5) become: public interface IHaveChildren { string ParentType { get; } int ParentId { get; } } public static class HaveChildrenExtension { public static List<IChild> GetChildren( this IHaveChildren ) { //logic to get children by parent type and id //shared for all classes implementing IHaveChildren } } This seems to me to be a better mechanism for many interfaces.

Is it possible to refactor this extension method?

心已入冬 提交于 2019-11-28 19:59:51
问题 I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument) where T : class { if (value == null) { throw new ArgumentNullException(argument); } } and this is an example of its usage.... // Note: I've poorly named the argument, on purpose, for this question. public void Save(Category qwerty) { qwerty.ThrowIfArgumentIsNull("qwerty"); .... } works 100% fine. But, I don't like how I have to provide the name of the variable, just to help my

Is there a workaround for generic type constraint of “special class” Enum in C# 3.0? [duplicate]

北战南征 提交于 2019-11-28 19:45:04
This question already has an answer here: Anyone know a good workaround for the lack of an enum generic constraint? 12 answers Update: See the bottom of this question for a C# workaround. Hi there, Consider the following extension method: public static bool HasFlags<T>(this T value, T flags) where T : System.Enum { // ... } This will, as you may know, throw an error at compile-time, since a class is not normally allowed to inherit from System.Enum . The problem is that any enumeration specified using the enum keyword does in fact inherit from System.Enum , so the above code would be the ideal

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

谁说胖子不能爱 提交于 2019-11-28 19:04:36
I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't be sure if I saw it though, or I am just dreaming. And I can't seem to find an extension on any of these collections that accepts a Func<> or a delegate in their method signatures. Is there such an overload/extension method? Or, if not, is it possible to muck around like this and pass an algorithm (read delegate) where a single-method interface is

Why use TagBuilder instead of StringBuilder?

别说谁变了你拦得住时间么 提交于 2019-11-28 18:35:11
what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable? aren't they generating the same thing?? Bashir Magomedov TagBuilder is a class that specially designed for creating html tags and their content. You are right saying that result will be anyway a string and of course you still can use StringBuilder and the result will be the same, but you can do things easier with TagBuilder . Lets say you need to generate a tag: <a href='http://www.stackoverflow.com' class='coolLink'/> Using StringBuilder you need to write something

Extension methods defined on value types cannot be used to create delegates - Why not?

五迷三道 提交于 2019-11-28 17:29:40
Extension methods can be assigned to delegates that match their usage on an object, like this: static class FunnyExtension { public static string Double(this string str) { return str + str; } public static int Double(this int num) { return num + num; } } Func<string> aaMaker = "a".Double; Func<string, string> doubler = FunnyExtension.Double; Console.WriteLine(aaMaker()); //Prints "aa" Console.WriteLine(doubler("b")); //Prints "bb" If the type they're extending is a value type, it won't work: Func<int> eightMaker = 4.Double; //Error CS1113: Extension methods 'FunnyExtension.Double(int)' defined