extension-methods

How do extension methods work?

烈酒焚心 提交于 2019-11-26 16:49:41
I want to make an extension method in Unity3d for the Vector3 class. But I don't quite seem to get it. This is what I have: public static class ExtensionMethods{ public static Vector3 MaxValue(this Vector3 _vec3) { return new Vector3(float.MaxValue,float.MaxValue,float.MaxValue); } } Now I want to make a Vector3.MaxValue just like float.MaxValue with this line of code: Vector3 closestPoint = Vector3.MaxValue; But then I get this error: error CS0117: `UnityEngine.Vector3' does not contain a definition for `MaxValue' I know that this would work: Vector3 closestPoint = new Vector3().MaxValue();

Why is it impossible to declare extension methods in a generic static class?

痞子三分冷 提交于 2019-11-26 16:41:31
问题 I'd like to create a lot of extension methods for some generic class, e.g. for public class SimpleLinkedList<T> where T:IComparable And I've started creating methods like this: public static class LinkedListExtensions { public static T[] ToArray<T>(this SimpleLinkedList<T> simpleLinkedList) where T:IComparable { //// code } } But when I tried to make LinkedListExtensions class generic like this: public static class LinkedListExtensions<T> where T:IComparable { public static T[] ToArray(this

AddRange to a Collection

荒凉一梦 提交于 2019-11-26 16:20:52
问题 A coworker asked me today how to add a range to a collection. He has a class that inherits from Collection<T> . There's a get-only property of that type that already contains some items. He wants to add the items in another collection to the property collection. How can he do so in a C#3-friendly fashion? (Note the constraint about the get-only property, which prevents solutions like doing Union and reassigning.) Sure, a foreach with Property. Add will work. But a List<T> -style AddRange

Java equivalent to C# extension methods

冷暖自知 提交于 2019-11-26 16:06:26
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? Java does not support extension methods. Instead, you can make a regular static method, or write your own class. user1686250 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 ability to override different methods based on different generic’s parameters instantiation. This is

Convert string to nullable type (int, double, etc…)

送分小仙女□ 提交于 2019-11-26 15:43:28
I am attempting to do some data conversion. Unfortunately, much of the data is in strings, where it should be int's or double, etc... So what I've got is something like: double? amount = Convert.ToDouble(strAmount); The problem with this approach is if strAmount is empty, if it's empty I want it to amount to be null, so when I add it into the database the column will be null. So I ended up writing this: double? amount = null; if(strAmount.Trim().Length>0) { amount = Convert.ToDouble(strAmount); } Now this works fine, but I now have five lines of code instead of one. This makes things a little

Timeout Pattern - How bad is Thread.Abort really?

自作多情 提交于 2019-11-26 15:31:31
I've read at various websites that Thread.Abort is not very good to use. In this case, how do you implement a timeout pattern? For instance, I've read that MS uses the pattern below (which I've wrapped in an extension method) throughout the framework. Personally, I think this is a pretty cool extension, but I'm worried about the Thread.Abort. Does anyone have a better way? public static bool CallandWait(this Action action, int timeout) { Thread subThread = null; Action wrappedAction = () => { subThread = Thread.CurrentThread; action(); }; IAsyncResult result = wrappedAction.BeginInvoke(null,

Raising C# events with an extension method - is it bad?

陌路散爱 提交于 2019-11-26 15:30:50
问题 We're all familiar with the horror that is C# event declaration. To ensure thread-safety, the standard is to write something like this: public event EventHandler SomethingHappened; protected virtual void OnSomethingHappened(EventArgs e) { var handler = SomethingHappened; if (handler != null) handler(this, e); } Recently in some other question on this board (which I can't find now), someone pointed out that extension methods could be used nicely in this scenario. Here's one way to do it:

No type inference with generic extension method

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 15:29:37
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs, bool> breakCond) where TEventInvocatorParameters : EventInvocatorParameters<TEventArgs> where TEventArgs : EventArgs { p.BreakCondition = breakCond; return p; } And this class public class EventInvocatorParameters<T> where T : EventArgs { public Func<T, bool> BreakCondition { get; set; } // Other properties used below omitted for brevity. } Now, I have the following problems: This extension method shows on all types, even string .

F# extension methods in C#

怎甘沉沦 提交于 2019-11-26 15:26:04
问题 If you were to define some extension methods, properties in an assembly written in F#, and then use that assembly in C#, would you see the defined extensions in C#? If so, that would be so cool. 回答1: [<System.Runtime.CompilerServices.Extension>] module Methods = [<System.Runtime.CompilerServices.Extension>] let Exists(opt : string option) = match opt with | Some _ -> true | None -> false This method could be used in C# only by adding the namespace (using using) to the file where it will be

How to extend C# built-in types, like String?

纵然是瞬间 提交于 2019-11-26 15:20:12
问题 Greetings everyone... I need to Trim a String . But I want to remove all the repeated blank spaces within the String itself, not only at the end or at the start of it. I could do it with a method like: public static string ConvertWhitespacesToSingleSpaces(string value) { value = Regex.Replace(value, @"\s+", " "); } Which I got from here. But I want this piece of code to be called within the String.Trim() itself, so I think I need to extend or overload or override the Trim method... Is there a