extension-methods

How do I convert IEnumerable to a custom type in C#?

狂风中的少年 提交于 2019-12-04 08:32:00
I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my custom collection? If your collection type implements IList<T> (to be able to Add() to it) you could write an extension method: public static Extensions { public static TColl ToTypedCollection<TColl, T>(this IEnumerable ien) where TColl : IList<T>, new() { TColl

C# - using extension methods to provide default interface implementation

我的梦境 提交于 2019-12-04 08:08:58
问题 I'm just learning about C# extension methods, and was wondering if I can use it to provide a default implementation for an interface. Say: public interface Animal { string MakeSound(); } public static string MakeSound(this Animal) { return ""; } Then public class Dog : Animal { string MakeSound() { return "Bark"; } } public class Porcupine : Animal { } And last: Animal dog = new Dog(); Animal porcupine = new Porcupine(); Print(dog.MakeSound()); Print(porcupine.MakeSound()); I'd like the

How to create a Between Extension Method

*爱你&永不变心* 提交于 2019-12-04 08:04:54
I have a variable whose value is populated at runtime. I want to check whether that value is between two same datatype values (say lowest and highest) or not using an Extension Method. I want to check like int a = 2; //here static but is can be changed at runtime if(a.Between(0,8)) DoSomething(); else DoNothing(); If a is 0 or 8 or any value between them, it should return true . If a is (-1 or less) or (9 or greater) then it should return false i want to create an extension method like public static bool Between<T1>(this T1 val1, T1 lowest, T1 highest) where ???? { What code to write here????

Mysterious “extra argument in call” error in Array extension method [duplicate]

梦想的初衷 提交于 2019-12-04 06:52:20
问题 This question already has an answer here : Swift 3.0: compiler error when calling global func min<T>(T,T) in Array or Dictionary extension (1 answer) Closed 2 years ago . This compiles: extension Array { func chunked(by chunkSize:Int) -> [[Element]] { return stride(from: 0, to: self.count, by: chunkSize).map { Array(self[$0..<[$0 + chunkSize, self.count].min()!]) } } } This doesn't (substituting the global min() function for the array min() method): extension Array { func chunked(by chunkSize

Complement of Two Lists?

两盒软妹~` 提交于 2019-12-04 06:26:59
lets say I have a list of strings: A,B,C,D Then another list of strings B,C,D I want to know what elements are in the first list that aren't in the second list, so the result would be A I don't know the name of the extension method to do this is. I know I can use concat, union, intersect for similar list comparisons, but just don't know the name to accomplish this particular task. Addendum, I am interested in duplicates, so if the first list is: A,A,A,B,C,D and the second list is B,C,D i want to get A,A,A Thanks! You can use the Except Extension Method to get all elements in a list that are

How to save a layered image using a extension function

纵饮孤独 提交于 2019-12-04 05:51:03
问题 I am using a extension function to save a uiview as a uiimage. The code works to save the uiimage. However what I am trying to do is save a transparent image over the image being saved to the photo gallery. So I am trying to save a layered image using a extension function. Right now only the uiivew is being save and the 2nd layer is not being saved. class ViewController: UIViewController,UINavigationControllerDelegate { @IBAction func press(_ sender: Any) { let jake = drawingView

An analog of String.Join(string, string[]) for IEnumerable<T>

强颜欢笑 提交于 2019-12-04 05:06:48
class String contains very useful method - String.Join(string, string[]) . It creates a string from an array, separating each element of array with a symbol given. But general - it doesn't add a separator after the last element! I uses it for ASP.NET coding for separating with " <br /> " or Environment.NewLine . So I want to add an empty row after each row in asp:Table . What method of IEnumerable<TableRow> can I use for the same functionality? I wrote an extension method: public static IEnumerable<T> Join<T>(this IEnumerable<T> src, Func<T> separatorFactory) { var srcArr = src.ToArray(); for

A different take on FirstOrDefault

坚强是说给别人听的谎言 提交于 2019-12-04 03:19:09
The IEnumerable extension method FirstOrDefault didn't exactly do as I wanted so I created FirstOrValue. Is this a good way to go about this or is there a better way? public static T FirstOrValue<T>(this IEnumerable<T> source, Func<T, bool> predicate, T value) { T first = source.FirstOrDefault(predicate); return Equals(first, default(T)) ? value : first; } Your code is probably incorrect; you probably haven't considered all of the cases. Of course, we cannot know if any code is correct or incorrect until we have a spec. So start by writing a one-line spec: " FirstOrValue<T> takes a sequence of

Operators as method parameters in C#

橙三吉。 提交于 2019-12-04 03:08:36
问题 I don't think it's possible to use operators as a parameters to methods in C# 3.0 but is there a way to emulate that or some syntactic sugar that makes it seem like that's what's going on? I ask because I recently implemented the thrush combinator in C# but while translating Raganwald's Ruby example (1..100).select(&:odd?).inject(&:+).into { |x| x * x } Which reads "Take the numbers from 1 to 100, keep the odd ones, take the sum of those, and then answer the square of that number." I fell

C#: Adding extension methods to a base class so that they appear in derived classes

[亡魂溺海] 提交于 2019-12-04 02:46:17
I currently have an extension method on System.Windows.Forms.Control like this: public static void ExampleMethod(this Control ctrl){ /* ... */ } However, this method doesn't appear on classes derived from Control, such as PictureBox. Can I make an extension method that appears not only in Control, but for classes derived from Control, without having to do an explicit cast? You must include the using statement for the namespace in which your extensions class is defined or the extension methods will not be in scope. Extension methods work fine on derived types (e.g. the extension methods defined