func

Is it bad practice to use Action and Func all the time instead of making corresponding delegates?

被刻印的时光 ゝ 提交于 2019-12-23 12:24:01
问题 A lot of time when creating simple events in my program that other classes can subscribe to instead of making a delegate and creating an event from the delegate I just create the event with either Action or Func to avoid having to create the delegate. Is there any downsides to doing this? 回答1: Not really, the only downside I can think of is that if you have a logical intention (beyond the parameters and return values expected) that you want the user to satisfy that may get lost using the

Body from Func<T>

懵懂的女人 提交于 2019-12-23 09:34:51
问题 how can I get a body from function Func<bool> methodCall = () => output.SendToFile(); if (methodCall()) Console.WriteLine("Success!"); I need to get this output.SendToFile() as a string Another example: string log = ""; public void Foo<T>(Func<T> func) { try { var t = func(); } catch (Exception) { //here I need to add the body of the lambda // log += func.body; } } public void Test() { var a = 5; var b = 6; Foo(() => a > b); } Edit: For more information on this topic see: Expression Trees 回答1

Using a Func<> over an interface?

非 Y 不嫁゛ 提交于 2019-12-23 08:53:09
问题 I have an already existing generic class public class Foo<T> { private T _item; public Foo(T item){ _item = item;} } I have to create a method which will return a certain property of T. I see two solutions here. Creating an interface : public const string TestVar = "bar"; public interface INamable { string Name { get; } } public class Bar : INamable { public string Name { get { return TestVar; } } } public class Foo<T> where T : INamable { private T _item; public Foo(T item) { _item = item; }

C# creating function queue

允我心安 提交于 2019-12-22 10:08:48
问题 I have written a class called QueueManager: class QueueManager { Queue functionsQueue; public bool IsEmpty { get { if (functionsQueue.Count == 0) return true; else return false; } } public QueueManager() { functionsQueue = new Queue(); } public bool Contains(Action action) { if (functionsQueue.Contains(action)) return true; else return false; } public Action Pop() { return functionsQueue.Dequeue() as Action; } public void Add(Action function) { functionsQueue.Enqueue(function); } public void

Reflection MemberInfo to Func<T1, T2>

末鹿安然 提交于 2019-12-22 06:31:43
问题 I'm looking for a method to convert instance of MemberInfo to "Func" type (to use it through lambda expression later). Lets, say I have a member function of type public bool func(int); Using reflection, I somehow get instance of MemberInfo "mi", now i want to convert it to Func<int, bool>; type. something like: Func<int, bool f = myType.GetMember(mi.Name); Is there a way to do it? ps. Marc Grawell's answer resolves my issue, no need for further comments 回答1: Func<int,bool> f = Delegate

Get the name of a field from a class without an instance

巧了我就是萌 提交于 2019-12-22 04:22:07
问题 So I use the following utility to get the name of a field/property from an instance of a class... public static string FieldName<T>(Expression<Func<T>> Source) { return ((MemberExpression)Source.Body).Member.Name; } This allows me to do the following: public class CoolCat { public string KaratePower; } public class Program { public static Main() { public CoolCat Jimmy = new CoolCat(); string JimmysKaratePowerField = FieldName(() => Jimmy.KaratePower); } } This is great for serialization and

wrong generic type in swift

谁说胖子不能爱 提交于 2019-12-21 05:56:49
问题 After run following code in playgroud, why x value is 2? Is there anything wrong with swift generic type and "is" operator? class Item {} class Campaign: Item {} class AdGroup : Item {} class A<T: Item> { func val() -> Int{ let item = T() if item is Campaign { return 1 } else { return 2 } } } var m = A<Campaign>() let x = m.val() 来源: https://stackoverflow.com/questions/27899744/wrong-generic-type-in-swift

Lambda\Anonymous Function as a parameter

时光总嘲笑我的痴心妄想 提交于 2019-12-18 19:03:26
问题 I'm a very new to C#. Just playing around with it. Not for a real purpose. void makeOutput( int _param) { Console.WriteLine( _param.ToString()); } //... // Somewhere in a code { makeOutput( /* some not c# code for an example for what do I want */ function : int () { return 0; } ); } Is it possible to use a REAL anonymous functions (means returning result)? I do not want to use delegates such as // Somewhere in a code { Func<int> x = () => { return 0; }; makeOutput( x()) } Also I DO NOT want

Func delegate doesn't chain methods

蓝咒 提交于 2019-12-18 13:07:16
问题 Lets imagine simple delegate calls: void Main() { Func<int, int, string> tfunc = null; tfunc += Add; // bind first method tfunc += Sub; // bind second method Console.WriteLine(tfunc(2, 2)); } private string Add(int a, int b) { return "Add: " + (a + b).ToString(); } private string Sub(int a, int b) { return "Sub: " + (a - b).ToString(); } The result of this program is: Sub: 0 So, why Add method was not called? I'm expecting to call Method Add, and then method Sub. 回答1: Add was correctly

Why is Action/Func better than a regular Method in .Net?

陌路散爱 提交于 2019-12-18 11:30:09
问题 I much prefer using an Action or a Func if I need a quick reusable piece of code, however others on my team don't like them or understand them. At the moment my only real argument is about preference and more up to date code practices, but those are just bad arguments. Why is it better to do this: Action<FormView,String> hideControl = (form,name) => { var button = form.GetControl<Button>(name); if (button != null) button.Visible = false; } than: public static void HideControl<T>(this FormView