func

How can I pass in a func with a generic type parameter?

孤人 提交于 2019-11-28 11:52:44
I like to send a generic type converter function to a method but I can't figure out how to do it. Here's invalid syntax that explains what I like to achieve, the problem is I don't know how to specify the generic type together with my func: public void SomeUtility(Func<T><object,T> converter) { var myType = converter<MyType>("foo"); } Edit (see also my discussion in the comments with Lawrence) : By "generic type converter" I meant I would like to pass in a converter that can convert to any strong type <T> (not object), so the next line in my method could be: var myOtherType = converter

How to declare a generic delegate with an out parameter [duplicate]

懵懂的女人 提交于 2019-11-28 09:39:56
This question already has an answer here: Func<T> with out parameter 4 answers Func<a, out b, bool> , just don't compile, how to declare that i want the second parameter be an out one? I want to use it like this: public class Foo() { public Func<a, out b, bool> DetectMethod; } Andrew Bezzub Actually, Func is just a simple delegate declared in the .NET Framework. Actually, there are several Func delegates declared there: delegate TResult Func<TResult>() delegate TResult Func<T, TResult>(T obj) delegate TResult Func<T1, T2, TResult>(T1 obj1, T2 obj2) delegate TResult Func<T1, T2, T3, TResult>(T1

swift ios - How to run function in ViewController from AppDelegate

回眸只為那壹抹淺笑 提交于 2019-11-28 04:59:43
问题 I am trying to run a function in certain ViewController using AppDelegate func applicationDidBecomeActive(_ application: UIApplication) { ViewController().grabData() } But somehow the function does not seem to run at all when the app has become active after entering the app from the background. The function looks like this func grabData() { self._DATASERVICE_GET_STATS(completion: { (int) -> () in if int == 0 { print("Nothing") } else { print(int) for (_, data) in self.userDataArray.enumerated

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

喜你入骨 提交于 2019-11-28 04:55:41
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? nawfal I think other answers here talk about what an Action / Func is and its use. I will try to answer how to choose between Action / Func and method. The differences first: 1) From a raw performance point of

How to convert System.Linq.Enumerable.WhereListIterator<int> to List<int>?

筅森魡賤 提交于 2019-11-27 17:49:13
问题 In the below example, how can I easily convert eventScores to List<int> so that I can use it as a parameter for prettyPrint ? Console.WriteLine("Example of LINQ's Where:"); List<int> scores = new List<int> { 1,2,3,4,5,6,7,8 }; var evenScores = scores.Where(i => i % 2 == 0); Action<List<int>, string> prettyPrint = (list, title) => { Console.WriteLine("*** {0} ***", title); list.ForEach(i => Console.WriteLine(i)); }; scores.ForEach(i => Console.WriteLine(i)); prettyPrint(scores, "The Scores:");

Func<T>() vs Func<T>.Invoke()

喜你入骨 提交于 2019-11-27 13:23:53
问题 I'm curious about the differences between calling a Func directly vs using Invoke() on it. Is there a difference ? Is the first, syntactical sugar, and calls Invoke() underneath anyway ? public T DoWork<T>(Func<T> method) { return (T)method.Invoke(); } vs public T DoWork<T>(Func<T> method) { return (T)method(); } Or am I on the wrong track entirely :) Thanks. 回答1: There's no difference at all. The second is just a shorthand for Invoke , provided by the compiler. They compile to the same IL.

Can someone explain what the C# “Func<T,T>” does?

ぃ、小莉子 提交于 2019-11-27 11:06:05
问题 I'm reading the Pro MVC 2 book, and there is an example of creating an extension method for the HtmlHelper class. Here the code example: public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int,string> pageUrl) { //Magic here. } And here is an example usage: [Test] public void Can_Generate_Links_To_Other_Pages() { //Arrange: We're going to extend the Html helper class. //It doesn't matter if the variable we use is null HtmlHelper html = null; PagingInfo

Can you get a Func<T> (or similar) from a MethodInfo object?

只谈情不闲聊 提交于 2019-11-27 11:01:01
I realize that, generally speaking, there are performance implications of using reflection. (I myself am not a fan of reflection at all, actually; this is a purely academic question.) Suppose there exists some class that looks like this: public class MyClass { public string GetName() { return "My Name"; } } Bear with me here. I know that if I have an instance of MyClass called x , I can call x.GetName() . Furthermore, I could set a Func<string> variable to x.GetName . Now here's my question. Let's say I don't know the above class is called MyClass ; I've got some object, x , but I have no idea

Why is Func<> created from Expression<Func<>> slower than Func<> declared directly?

余生颓废 提交于 2019-11-27 09:32:38
问题 Why is a Func<> created from an Expression<Func<>> via .Compile() considerably slower than just using a Func<> declared directly ? I just changed from using a Func<IInterface, object> declared directly to one created from an Expression<Func<IInterface, object>> in an app i am working on and i noticed that the performance went down. I have just done a little test, and the Func<> created from an Expression takes "almost" double the time of an Func<> declared directly. On my machine the Direct

golang “undefined” function declared in another file?

纵饮孤独 提交于 2019-11-27 09:23:03
问题 I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns: undefined: NewEmployee Here is the source code: main.go : package main func main() { emp := NewEmployee() } employee.go : package main type Employee struct { name string age int } func NewEmployee() *Employee { p := &Employee{} return p } func PrintEmployee (p *Employee) { return "Hello world!" } 回答1: Please read "How to Write Go Code". Don't use /src in your