anonymous-methods

How to return a generic list collection in C#?

橙三吉。 提交于 2019-12-30 09:44:11
问题 I have some linq to sql method and when it does the query it returns some anonymous type. I want to return that anonymous type back to my service layer to do some logic and stuff on it. I don't know how to return it though. I thought I could do this public List<T> thisIsAtest() { return query; } but I get this error Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) So not sure what assembly I am missing or if that is even

C#: Anonymous method vs Named method

痞子三分冷 提交于 2019-12-28 15:16:10
问题 I'm new to SO and programming and learning day by day with bits and pieces of tech (C#) jargons. After Googling for a while, below is what I've researched about methods A Method is a block of statements, which serves for code reusability & it also supports overloading with different SIGNATURE....for ex: drawShape(2pts), drawShape(3pts) etc... An Anonymous method is one with block of statements, but no name....(as its premature to ask, in wt situation we come across anonymous method...any

Where does anonymous function body variables saved ?

不羁岁月 提交于 2019-12-24 13:26:03
问题 Below code is working but why ? Where does the x and y coming/saved when I invoke anonymous method in the loop. thanks static void Main(string[] args) { int x=1; int y=2; var dic = GetDic(x, y); for (int i = 0; i < 5;i++ ) { System.Console.WriteLine(dic[i].Invoke().ToString()); } } private static Dictionary<int, Func<int>> GetDic(int x, int y) { var dic = new Dictionary<int, Func<int>>() { {0,()=>{return y;}}, {1,()=>{return x;}}, {2,()=>{return x+y;}}, {3,()=>{return x-y;}}, {4,()=>{return y

Can you use .net 3.5 Action or Func as Marshalled unmanaged delegates?

こ雲淡風輕ζ 提交于 2019-12-24 03:03:58
问题 After reading Dynamically calling unmanaged dlls in .net I've been trying to modify the code to my liking. I made a class that implements idisposable to wrap load calls in and free them when needed. However I can't seem to figure out the syntax if it is possible to use anonymous delegates with it. var loaded=DynamicLibraryLoader.TryLoad("User32.dll"); var beeper=loaded.GetProcAddress("MessageBeep"); var type=typeof(Action<UInt32>); Action<UInt32> beepAction2=(Action<UInt32>) Marshal

C#: Is it possible to declare a local variable in an anonymous method?

坚强是说给别人听的谎言 提交于 2019-12-23 07:00:15
问题 Is is possible to have a local variable in an anonymous c# methods, i.e. in the following code I would like to perform the count only once. IQueryable<Enquiry> linq = db.Enquiries; if(...) linq = linq.Where(...); if(...) linq = linq.Where(e => (x <= (from p in db.Orders where p.EnquiryId == e.Id select p).Count() && (from p in db.Orders where p.EnquiryId == e.Id select p).Count() <= y)); if(...) linq = linq.Where(...); var result = (from e in linq select e); Is there a "let" for anonymous

Is there any overhead in the use of anonymous methods?

核能气质少年 提交于 2019-12-21 04:13:22
问题 I would like to know if there is any overhead incurred through the use of anonymous methods when creating a Background worker. for example: public void SomeMethod() { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (sender, e) => { //large amount of code } worker.RunWorkerAsync(); } Would the example above be any better or worse than defining the //large amount of code in a separate method? Is there any overhead incurred in defining the background worker method in-line,

C# - anonymous functions and event handlers

情到浓时终转凉″ 提交于 2019-12-20 10:06:18
问题 I have the following code: public List<IWFResourceInstance> FindStepsByType(IWFResource res) { List<IWFResourceInstance> retval = new List<IWFResourceInstance>(); this.FoundStep += delegate(object sender, WalkerStepEventArgs e) { if (e.Step.ResourceType == res) retval.Add(e.Step); }; this.Start(); return retval; } Notice how I register my event member (FoundStep) to local in-place anonymous function. My question is: when the function 'FindStepByType' will end - will the anonymous function be

How do lambda expressions work internally?

左心房为你撑大大i 提交于 2019-12-19 02:52:14
问题 While looking up the answer to this question: "Why is an out parameter not allowed within an anonymous method?" I've got a little lost about how do lambda expression and anonymous methods actually work. In the comments JaredPar states that "Imagine for instance that the out parameter referred to a local variable on the stack. The lambda can execute at any arbitrary point in the future and hence could execute when that stack frame was no longer valid". I pointed out if wouldn't that be the

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

VB.NET RemoveHandler & Anonymous Methods

梦想的初衷 提交于 2019-12-17 18:51:36
问题 How do I use RemoveHandler with anonymous methods? This is how I add a handler for MyEvent event of the class MyClass : AddHandler MyClass.MyEvent, Sub() '... End Sub How do I then use RemoveHandler to remove the handler for the MyEvent event? 回答1: In general, if you need to unsubscribe from the event, I would recommend not using a lambda like this, and instead use a standard method. That being said, you can still use the anonymous method, but you need to store a reference to it for the