func

What's so great about Func<> delegate?

廉价感情. 提交于 2019-11-27 09:19:47
问题 Sorry if this is basic but I was trying to pick up on .Net 3.5. Question: Is there anything great about Func<> and it's 5 overloads? From the looks of it, I can still create a similar delgate on my own say, MyFunc<> with the exact 5 overloads and even more. eg: public delegate TResult MyFunc<TResult>() and a combo of various overloads... The thought came up as I was trying to understand Func<> delegates and hit upon the following scenario: Func<int,int> myDelegate = (y) => IsComposite(10);

Explanation of Func

家住魔仙堡 提交于 2019-11-27 09:04:55
问题 I was wondering if someone could explain what Func<int, string> is and how it is used with some clear examples. 回答1: Are you familiar with delegates in general? I have a page about delegates and events which may help if not, although it's more geared towards explaining the differences between the two. Func<T, TResult> is just a generic delegate - work out what it means in any particular situation by replacing the type parameters ( T and TResult ) with the corresponding type arguments ( int

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

心不动则不痛 提交于 2019-11-27 06:34:23
问题 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

Func delegate with ref variable

人走茶凉 提交于 2019-11-27 04:44:06
public object MethodName(ref float y) { //method } How do I defined a Func delegate for this method? It cannot be done by Func but you can define a custom delegate for it: public delegate object MethodNameDelegate(ref float y); Usage example: public object MethodWithRefFloat(ref float y) { return null; } public void MethodCallThroughDelegate() { MethodNameDelegate myDelegate = MethodWithRefFloat; float y = 0; myDelegate(ref y); } In .NET 4+ you can also support ref types this way... public delegate bool MyFuncExtension<in string, MyRefType, out Boolean>(string input, ref MyRefType refType); 来源

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

耗尽温柔 提交于 2019-11-27 02:42:11
问题 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; } 回答1: 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

How to make a random color with Swift

Deadly 提交于 2019-11-26 20:28:30
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 } } 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) } } Then you can use this to create a random colour: extension UIColor { static func random() -> UIColor {

Does Ninject support Func (auto generated factory)?

我的梦境 提交于 2019-11-26 17:35:07
Autofac automatically generates factories for Func<T> ; I can even pass parameters. public class MyClass { public MyClass(Func<A> a, Func<int, B> b) { var _a = a(); var _b = b(1); } } Can I do the same with Ninject? If not, what workaround can I apply? Thanks. Update : Just found this post, seems the answer is no: How do I handle classes with static methods with Ninject? Remo Gloor NB Ninject 3.0 and later has this fully supported using the Ninject.Extensions.Factory package, see the wiki:- https://github.com/ninject/ninject.extensions.factory/wiki EDIT: NB there is a Bind<T>().ToFactory()

Creating delegates manually vs using Action/Func delegates

一笑奈何 提交于 2019-11-26 17:32:59
Today I was thinking about declaring this: private delegate double ChangeListAction(string param1, int number); but why not use this: private Func<string, int, double> ChangeListAction; or if ChangeListAction would have no return value I could use: private Action<string,int> ChangeListAction; so where is the advantage in declaring a delegate with the delegate keyword? Is it because of .NET 1.1, and with .NET 2.0 came Action<T> and with .NET 3.5 came Func<T> ? The advantage is clarity. By giving the type an explicit name it is more clear to the reader what it does. It will also help you when

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

若如初见. 提交于 2019-11-26 15:23:29
问题 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.

Func delegate with ref variable

一曲冷凌霜 提交于 2019-11-26 12:45:47
问题 public object MethodName(ref float y) { //method } How do I defined a Func delegate for this method? 回答1: It cannot be done by Func but you can define a custom delegate for it: public delegate object MethodNameDelegate(ref float y); Usage example: public object MethodWithRefFloat(ref float y) { return null; } public void MethodCallThroughDelegate() { MethodNameDelegate myDelegate = MethodWithRefFloat; float y = 0; myDelegate(ref y); } 回答2: In .NET 4+ you can also support ref types this way...