delegates

Unit testing that an event is raised in C#, using reflection

十年热恋 提交于 2019-12-20 10:36:46
问题 I want to test that setting a certain property (or more generally, executing some code) raises a certain event on my object. In that respect my problem is similar to Unit testing that an event is raised in C#, but I need a lot of these tests and I hate boilerplate. So I'm looking for a more general solution, using reflection. Ideally, I would like to do something like this: [TestMethod] public void TestWidth() { MyClass myObject = new MyClass(); AssertRaisesEvent(() => { myObject.Width = 42;

What is the difference between delegate in c# and function pointer in c++? [duplicate]

痴心易碎 提交于 2019-12-20 10:07:10
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: are there function pointers in c#? I'm interested in finding the difference between delegate in C# and function pointer in C++. 回答1: A delegate in C# is a type-safe function pointer with a built in iterator. It's guaranteed to point to a valid function with the specified signature (unlike C where pointers can be cast to point to who knows what). It also supports the concept of iterating through multiple bound

Observer pattern implemented in C# with delegates?

点点圈 提交于 2019-12-20 09:59:39
问题 There is a question already answered which is In C#, isn't the observer pattern already implemented using Events? It asks if the observer pattern is already implemented in c# using events. While I get the events and observer pattern, isn't the observer pattern really just delegates and events is a further implementation? 回答1: You are correct. An event is simply a delegate with some slightly different functionality. All of the observer pattern can be implemented with delegates without ever

Call a higher order F# function from C#

偶尔善良 提交于 2019-12-20 09:37:37
问题 Given the F# higher order function (taking a function in parameter): let ApplyOn2 (f:int->int) = f(2) and the C# function public static int Increment(int a) { return a++; } How do I call ApplyOn2 with Increment as parameter (from C#)? Note that ApplyOn2 is exported as Microsoft.FSharp.Core.FSharpFunc<int,int> which do not match with Increment 's signature. 回答1: If you would like to provide a more friendly interop experience, consider using the System.Func delegate type directly in F#: let

Why do 2 delegate instances return the same hashcode?

一笑奈何 提交于 2019-12-20 08:57:28
问题 Take the following: var x = new Action(() => { Console.Write("") ; }); var y = new Action(() => { }); var a = x.GetHashCode(); var b = y.GetHashCode(); Console.WriteLine(a == b); Console.WriteLine(x == y); This will print: True False Why is the hashcode the same? It is kinda surprising, and will make using delegates in a Dictionary as slow as a List (aka O(n) for lookups). Update: The question is why. IOW who made such a (silly) decision? A better hashcode implementation would have been:

Why are there memory allocations when calling a func

笑着哭i 提交于 2019-12-20 08:56:10
问题 I have the following program which construct a local Func from two static methods. But strangely, when I profile the program, it allocated close to a million Func objects. Why invoking Func object is also creating Func instances? public static class Utils { public static bool ComparerFunc(long thisTicks, long thatTicks) { return thisTicks < thatTicks; } public static int Foo(Guid[] guids, Func<long, long, bool> comparerFunc) { bool a = comparerFunc(1, 2); return 0; } } class Program { static

Open delegate for generic interface method

百般思念 提交于 2019-12-20 08:36:17
问题 I'm trying to create an open instance delegate for a generic interface method, but I keep receiving a NotSupportedException. Here is the simplified code that won't run: interface IFoo { void Bar<T>(T j); } class Foo : IFoo { public void Bar<T>(T j) { } } static void Main(string[] args) { var bar = typeof(IFoo).GetMethod("Bar").MakeGenericMethod(typeof(int)); var x = Delegate.CreateDelegate(typeof(Action<IFoo, int>), null, bar); } The last line throws NotSupportedException, "Specified method

Using delegates in C#

情到浓时终转凉″ 提交于 2019-12-20 08:09:20
问题 In C# language and .NET framework, could you help me with understanding delegates? I was trying to check some code, and found that the results I received were unexpected for me. Here it is: class Program { public static int I = 0; static Func<string> del = new Func<string>(I.ToString); static void Main(string[] args) { I = 10; Console.WriteLine("{0}", del()); } } The answer was 0, but not 10. Why? 回答1: The reason is the following: The way you declare the delegate it points directly to the

Delegates vs Interfaces in C#

喜你入骨 提交于 2019-12-20 08:01:03
问题 I would like to pose this question as long as I am trying currently to dig into the use and the purpose of delegates, although it is likely to have been asked in similar formulations. I know that delegates serve as function pointers used in C++. The matter of the fact is if in C# they serve mostly as an alternative to interfaces and polymorphism. Since I can create subclasses of a specific class and supply them the appropriate methods to each one, what offer delegates additionally to that?

How to use a function from the view controller in the app delegate?

六眼飞鱼酱① 提交于 2019-12-20 07:42:11
问题 I have an app that is connected to a device by Bluetooth. I want the app to send a command that indicates that the app is going to close in the app delegate method : (void)applicationWillTerminate:(UIApplication *)application { 回答1: One word: NSNotificationCenter I'm not sure what you need to set the data to because you can't pass the data seamlessly via NSNotificationCenter ; however you were going to figure that out in your UIApplicationDelegate anyway, so why can't you do it in the view