Questions tagged [anonymous-methods]

An anonymous method is a procedure or function that does not have a name associated with it.

An anonymous method is a procedure or function that does not have a name associated with it. An anonymous method treats a block of code as an entity that can be assigned to a variable or used as a parameter to a method. In addition, an anonymous method can refer to variables and bind values to the variables in the context in which the method is defined. They are similar to the construct of closures defined in some languages.

An anonymous method in C# is a way to pass a code block as a delegate parameter.

320 questions
0
votes
2 answers

How to reduce this code using anonymous methods or anonymous types?

I have a web service and some methods and I do NOT want to use this code below, which works fine, because I intend to use this in a multi-threaded app where static (I have read) is not thread safe. So, what I have done now is simply repeat the code…
PaulDecember
  • 167
  • 2
  • 13
0
votes
2 answers

Unsubscribing events in case variable closure

I'm always try to unsubscribe events where it possible and may. In case when variable closure happen I do the following: int someVar; EventHandler moveCompleted = null; moveCompleted = delegate(object sender, EventArgs e) { //... //here is…
user824249
0
votes
1 answer

Cannot convert async lambda expression to delegate type 'Func'

I am getting, Cannot convert async lambda expression to delegate type 'Func'. An async lambda expression may return void, Task or Task, none of which are convertible to 'Func'. with below code, var requestHandler = new…
Sara
  • 55
  • 5
0
votes
1 answer

Why does passing lambda parameter into a method not work?

Given this code: class Pet { public string Name { get; set; } public int Age { get; set; } } public static void OrderByEx1() { Pet[] pets = { new Pet { Name="Barley", Age=8 }, new Pet { Name="Boots", Age=4 }, …
0
votes
1 answer

Need help writing an anonymous method?

Forgive me if my question is technically worded wrong but I basically need an anonymous method or a Func delegate to encapsulate the following functionality: if (Cache.CurrentCustomer == null) { …
IbrarMumtaz
  • 4,235
  • 7
  • 44
  • 63
0
votes
0 answers

Why does C# compiler generate anonymous delegate without closures as an instance method, rather than as a static method on the same type?

Consider the following code creating and running anonymous delegate: static class Program { static void Main(string[] args) { Action action = () => Console.WriteLine("Test"); action(); } } In .NET 5, when decompiled…
Ivan Shimko
  • 179
  • 1
  • 9
0
votes
3 answers

Why is this delegate syntax "legal"?

I am reading about anonymous methods and am trying to wrap my head around this example: List evenNumbers = list.FindAll(delegate(int i) { return (i % 2) == 0; } ) Why is delegate(int i) legal? You aren't having to declare new delegate void or…
richard
  • 12,263
  • 23
  • 95
  • 151
0
votes
5 answers

C# How to do this anonymous method injection

How do I use something like this in C#. Console.WriteLine("yaya instant"); Server.registerEvent(new Event(5000) { public void doWork() { this.stop(); Console.WriteLine("yaya 5 seconds later"); } }); Event class and the…
SSpoke
  • 5,656
  • 10
  • 72
  • 124
0
votes
1 answer

Error in Anonymous Method contained variable

I have the following method. When the Writeline Methods are calling, missing results arise. The result of the second Writeline must true otherwise, it is false, could you please advise me? public static void IsGreater() { var…
0
votes
1 answer

can I change authentication provider from google account to be anonymous in firebase authentication?

An user login using Google account as the provider for authentication like the image above. I want to make when the user performs log out, then the provider should change from Google to be anonymous. so I want to make, when user logout, they will…
0
votes
2 answers

Cast Exception in C# Where clause

It's been a while since I am trying to figure out what is causing a cast exception in this case. Following is my code. var ratingTermDetails = dtRating.AsEnumerable() .Where(data => data.Field("RATINGID") == ratingId) …
0
votes
1 answer

Anonymous Class return to parent method

Is it possible to have an anonymous inner class return a value for a method it is contained in? For example, I want to do some calculations in something(), which is an anonymous inner class, and have doStuff() return that value. private int…
0
votes
0 answers

Selecting a number of points on a 3d surface

I try to select (by mouse clicking) a number of points on a 3D triangular surface mesh using matlab and save their coordinates in a matrix. This has proved to be far more notorious than I expected. It seems like callbacks is the right thing to do…
Paramar
  • 287
  • 1
  • 5
  • 22
0
votes
0 answers

C# Difference between delegate type and local Method

I'm pretty new to Delegates, Expression Lambda and local Methods. But can anyone explain the difference between these two code blocks? public static void Main() { bool IsDivisible(int x, int y) { return x % y == 0; } …
MindSwipe
  • 7,193
  • 24
  • 47
0
votes
1 answer

Anonymous method as event handler

Edit: this question is not about how to unsubscribe from events because I know how to do that if I want to. The question is about if there is a conflict with garbage collection in my specific scenario. In some code I wrote a while ago I registered…
oliver
  • 2,771
  • 15
  • 32