func

Dictionary with Func as key

前提是你 提交于 2019-11-30 08:57:11
I am wondering if this is a sane choice of key for a dictionary? What I want to do is use an expression as the key in a dictionary, something like: var map3 = new Dictionary<Func<int, bool>, int>(); map3.Add((x) => x % 2 == 0, 1); map3.Add((x) => x % 10 == 0, 2); // ... var key = map3.Keys.SingleOrDefault(f => f(2)); // key = (x) => x % 2 // map3[key] = 1 The idea being this is a cleaner way than having big if-else or switch statements. Does this make sense? Will it work? Is there an simpler way? Mike Zboray No, C# constructs a new delegate instance whenever a lambda is used so you wouldn't be

Cannot assign a delegate of one type to another even though signature matches

断了今生、忘了曾经 提交于 2019-11-30 05:19:01
My morbid curiosity has me wondering why the following fails: // declared somewhere public delegate int BinaryOperation(int a, int b); // ... in a method body Func<int, int, int> addThem = (x, y) => x + y; BinaryOperation b1 = addThem; // doesn't compile, and casting doesn't compile BinaryOperation b2 = (x, y) => x + y; // compiles! C# has very limited support for "structural" typing. In particular, you can't cast from one delegate-type to another simply because their declarations are similar. From the language specification: Delegate types in C# are name equivalent, not structurally

Using Func<> in LINQ Query

懵懂的女人 提交于 2019-11-29 23:03:15
问题 I have a Func<ProductItemVendor, bool> stored in CompareProductItemVendorIds . I would like to use that expression in a LINQ query. It appears the following is legal: var results = Repository.Query<ProductItemVendor>().Where(CompareProductItemVendorIds); However, the following is not legal: var results = from v in Repository.Query<ProductItemVendor>() where CompareProductItemVendorIds(v) select v; This code produces an error: The LINQ expression node type 'Invoke' is not supported in LINQ to

Go map of functions

落爺英雄遲暮 提交于 2019-11-29 20:35:33
I have Go program that has a function defined. I also have a map that should have a key for each function. How can I do that? I have tried this, but this doesn't work. func a(param string) { } m := map[string] func { 'a_func': a, } for key, value := range m { if key == 'a_func' { value(param) } } Are you trying to do something like this? I've revised the example to use varying types and numbers of function parameters. package main import "fmt" func f(p string) { fmt.Println("function f parameter:", p) } func g(p string, q int) { fmt.Println("function g parameters:", p, q) } func main() { m :=

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

≯℡__Kan透↙ 提交于 2019-11-29 18:45:47
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 ways (patterns) do you use Func<> and Action<> to solve real problems? using System; using System

Dictionary with Func as key

人走茶凉 提交于 2019-11-29 13:04:24
问题 I am wondering if this is a sane choice of key for a dictionary? What I want to do is use an expression as the key in a dictionary, something like: var map3 = new Dictionary<Func<int, bool>, int>(); map3.Add((x) => x % 2 == 0, 1); map3.Add((x) => x % 10 == 0, 2); // ... var key = map3.Keys.SingleOrDefault(f => f(2)); // key = (x) => x % 2 // map3[key] = 1 The idea being this is a cleaner way than having big if-else or switch statements. Does this make sense? Will it work? Is there an simpler

Share expressions between Linq to Entities and Linq to Objects

非 Y 不嫁゛ 提交于 2019-11-29 12:39:51
I'm trying to "share" a set of conditions between a Linq to Entities call and a some other code, to reduce possible mismatches in conditions between the two calls. I started off by declaring my conditions: private Func<DateTime, Status, bool> _submissionDateExpiredCondition = (submissionDate, status) => submissionDate < DateTime.Now && status == Status.OK; private Func<DateTime, Status, bool> _submissionDateWithinOneWeekCondition = (submissionDate, status) => DateTime.Now < DbFunctions.AddDays(submissionDate, -7) && status == Status.Pending; private Func<DateTime?, Status, bool>

swift ios - How to run function in ViewController from AppDelegate

风流意气都作罢 提交于 2019-11-29 11:22:47
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() { let number = Double(data["wage"]!) let x = number!/3600 let z = Double(x * Double(int)) self.money

Difference between Func<> with delegate and lambda expression [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-29 08:05:30
问题 This question already has an answer here: delegate keyword vs. lambda notation 6 answers while deepening myself to more advanced features of C#, I came across some code, which I didn't exactly know the difference of. It's about these two lines: Func<string, int> giveLength = (text => text.Length); and Func<string, int> giveLength = delegate(string text) { return text.Length; }; This can be used in the same way: Console.WriteLine(giveLength("A random string.")); So basically.. What is the

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

不问归期 提交于 2019-11-29 03:49:00
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:"); foreach (int score in evenScores) { Console.WriteLine(score); } You'd use the ToList extension: var