extension-methods

Alternative class to allow for extensible Strings: How to work around java.lang.String being final? [closed]

大城市里の小女人 提交于 2021-02-20 05:35:59
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 days ago . Improve this question Recreating java.lang.String is not hard. See attached code. I'm really writing this to show how easy it is. If anybody wants to figure out how to get the native isBigEndian() and add it in the solutions, that'd be great. Other String methods would be great too. Just put this

Alternative class to allow for extensible Strings: How to work around java.lang.String being final? [closed]

夙愿已清 提交于 2021-02-20 05:34:05
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 days ago . Improve this question Recreating java.lang.String is not hard. See attached code. I'm really writing this to show how easy it is. If anybody wants to figure out how to get the native isBigEndian() and add it in the solutions, that'd be great. Other String methods would be great too. Just put this

Is Extension method thread safe?

江枫思渺然 提交于 2021-02-19 00:41:01
问题 Is this Extension method thread safe? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { if (handler != null) handler(sender, args); } } or do I need to change it to this? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { var h = handler; if (h!= null) h(sender, args); } } 回答1: You found an interesting loop hole, it tripped

Is Extension method thread safe?

醉酒当歌 提交于 2021-02-19 00:39:56
问题 Is this Extension method thread safe? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { if (handler != null) handler(sender, args); } } or do I need to change it to this? public static class Extensions { public static void Raise<T>(this EventHandler<T> handler, object sender, T args) where T : EventArgs { var h = handler; if (h!= null) h(sender, args); } } 回答1: You found an interesting loop hole, it tripped

Declarations from extensions cannot be overridden yet in Swift 4

狂风中的少年 提交于 2021-02-18 20:12:46
问题 I have recently migrated my code to Swift 4 . There is an issue that I am facing with extensions , i.e.: Declarations from extensions cannot be overridden yet I have already read multiple posts regrading this issue. But none of them entertains the scenario described below: class BaseCell: UITableViewCell { //Some code here... } extension BaseCell { func isValid() -> String? { //Some code here... } } class SampleCell: BaseCell { //Some code here... override func isValid() -> String? //ERROR..!

C# extension method as an interface implementation

懵懂的女人 提交于 2021-02-17 21:10:16
问题 I was wondering if a C# extension method of some class could act as an implementation of interface? What do I have: An iterface: public interface IEventHandler { void Notify(SEvent ev, IEventEmmiter source); } A class that implements it: class Sim : IEventHandler { /*public void Notify(SEvent ev, IEventEmmiter source) { Console.WriteLine("Got notified: " + ev.Name); }*/ } And a class that contains the extension method: public static class ReflectiveEventDispatcher { public static void Notify

C# extension method as an interface implementation

随声附和 提交于 2021-02-17 21:06:10
问题 I was wondering if a C# extension method of some class could act as an implementation of interface? What do I have: An iterface: public interface IEventHandler { void Notify(SEvent ev, IEventEmmiter source); } A class that implements it: class Sim : IEventHandler { /*public void Notify(SEvent ev, IEventEmmiter source) { Console.WriteLine("Got notified: " + ev.Name); }*/ } And a class that contains the extension method: public static class ReflectiveEventDispatcher { public static void Notify

How to update a global variable inside `where` clause in LINQ?

佐手、 提交于 2021-02-17 06:53:07
问题 I want to filter a list using LINQ with Where extension method. But apart from filtering I also want to update a global variable inside Where . However I cannot do it. Consider this example: var list = new List<string> { "1", "2", "3", "4", "5" }; bool flag = false; var newList = list.Where(item => { flag = true; return item == "2"; }); // Here I expect flag = true, but in fact it's false Console.Write(flag); As you can see I set flag = true , still the value flag == false after execution. It

Extension method and local 'this' variable

你。 提交于 2021-02-08 13:34:09
问题 To my knowledge this in a extension method is passed as a ref variable. I can verify this by doing public static void Method<T>(this List<T> list) { list.Add(default(T)); } List<int> ints = new List<int>(new int[] { 1, 2, 3, 4, 5 }); ints.Method(); My List<int> ints is now 1, 2, 3, 4, 5, 0 . However when I do public static void Method<T>(this List<T> list, Func<T, bool> predicate) { list = list.Where(predicate).ToList(); } List<int> ints = new List<int>(new int[] { 1, 2, 3, 4, 5 }); ints

Extension method and local 'this' variable

故事扮演 提交于 2021-02-08 13:32:57
问题 To my knowledge this in a extension method is passed as a ref variable. I can verify this by doing public static void Method<T>(this List<T> list) { list.Add(default(T)); } List<int> ints = new List<int>(new int[] { 1, 2, 3, 4, 5 }); ints.Method(); My List<int> ints is now 1, 2, 3, 4, 5, 0 . However when I do public static void Method<T>(this List<T> list, Func<T, bool> predicate) { list = list.Where(predicate).ToList(); } List<int> ints = new List<int>(new int[] { 1, 2, 3, 4, 5 }); ints