func

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

自作多情 提交于 2019-12-18 11:29:14
问题 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

How do you use Func<> and Action<> when designing applications?

我只是一个虾纸丫 提交于 2019-12-18 09:54:19
问题 All the examples I can find about Func<> and Action<> are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be solved only in a more complex way, i.e. I know how they work and I can see they are terse and powerful , so I want to understand them in a larger sense of what kinds of problems they solve and how I could use them in the design of applications. In what

How to inject Predicate and Func in Spring.net

元气小坏坏 提交于 2019-12-18 06:59:49
问题 I want to create an object with a constructor containing predicate and func objects in the xml config using spring. The Predicate and the Func arguments should point to a method of another configured object. How is this possible using Spring.net? I was not able to find a solution or hint in the documentation... A sample constructor would be: MyClass(Predicate<TInput> condition, Func<TInput, TOutput> result) 回答1: It is also possible to use the DelegateFactoryObject within Spring.net to create

How to use a Func in an expression with Linq to Entity Framework?

為{幸葍}努か 提交于 2019-12-18 02:50:37
问题 I am trying to write a linq to entity extension method that takes a Func to select a property Id and compare it against a list of ids. Classes public class A { public int AId { get; set; } } public class B { public int BId { get; set; } } Extension Method public static IQueryable<T> WithId<T>(this IQueryable<T> entities, Func<T, int> selector, IList<int> ids) { Expression<Func<T, bool>> expression = x => ids.Contains(selector(x)); return entities.Where(expression); // error here (when

Action/Func vs Methods, what's the point?

我的未来我决定 提交于 2019-12-17 17:38:40
问题 I know how to use Action and Func in .NET, but every single time I start to, the exact same solution can be achieved with a regular old Method that I call instead. This excludes when an Action or Func is used as an argument for something I don't control, like LINQ's .Where . So basically my question is...why do these exist? What do they give me extra and new that a simple Method doesn't? 回答1: I think other answers here talk about what an Action / Func is and its use. I will try to answer how

How to make a random color with Swift

烈酒焚心 提交于 2019-12-17 05:00:55
问题 How I can make a random color function using Swift? import UIKit class ViewController: UIViewController { var randomNumber = arc4random_uniform(20) var randomColor = arc4random() //Color Background randomly func colorBackground() { // TODO: set a random color view.backgroundColor = UIColor.yellow } } 回答1: You're going to need a function to produce random CGFloat s in the range 0 to 1: extension CGFloat { static func random() -> CGFloat { return CGFloat(arc4random()) / CGFloat(UInt32.max) } }

Implementing generics in a function using Func

时间秒杀一切 提交于 2019-12-14 04:07:37
问题 I have a follow static function: public static string codeList<T>(List<T> thelist, Func<T, string> coder); using this function with my own objects is not problem for example: string code = codeList<MyClass>(myclassList, MyClass.code); Where MyClass.code is a static function (defined in MyClass) that gets MyClass and returns string. The problem is when I try to use this function with List<int> or List<double> what I do now is predefining statics like Func<int,string> intCoder = (x) => x

Func for 5 arguments

為{幸葍}努か 提交于 2019-12-13 19:17:40
问题 I am working with System.Func but have reached a stumbling block with it. System.Func<TReturn> // (no arg, with return value) System.Func<T, TReturn> // (1 arg, with return value) System.Func<T1, T2, TReturn> // (2 arg, with return value) System.Func<T1, T2, T3, TReturn> // (3 arg, with return value) System.Func<T1, T2, T3, T4, TReturn> // (4 arg, with return value) The max it accepts is 4 arguments. Is there any way of extending this to 5 arguments? 回答1: You have a few options one is to

Can Ninject use an anonymous delegate (func) as a ConstructorArgument?

时间秒杀一切 提交于 2019-12-13 10:17:05
问题 I have a repository abstract class that encapsulates pretty much all of the CRUD functionality: public abstract class DataRepository<T> : IRepository<T> where T : class { public DataContext Context { get; private set; } public TransactionScope Transaction { get; private set; } /// <summary> /// A <see cref="bool"/> function that compares the keys for fetching a single item, for example: /// return item1.Id == item2.Id (as an anonymous delegate). /// </summary> public Func<T, T, bool>

Golang, how to return in func FROM another func?

余生长醉 提交于 2019-12-13 07:13:43
问题 I want to end execution in parent func apiEndpoint() upon calling/exiting in a child func apiResponse() func apiEndpoint() { if false { apiResponse("error") // I want apiResponse() call to return (end execution) in parent func // so next apiResponse("all good") wont be executed } apiResponse("all good") } func apiResponse(message string) { // returns message to user via JSON } 回答1: A function or method cannot control the execution (control flow) from where it was called from. You don't even