Can anyone tell me if I'm likely to run into unintended behavior if I use anonymous methods with Async I/O?
As an example:
Action<Socket> acceptedHandler = DoAccept
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.Completed += ((sender, ea) => acceptedHandler(ea.AcceptSocket));
// Server is a Socket
if (!Server.AcceptAsync(e))
...
Several C# questions on StackOverflow ask how to make anonymous delegates/lambdas with out or ref parameters. See, for example:
Calling a method with ref or out parameters from an anonymous method
Write a lambda or anonymous function that accepts an out parameter
To do so, you just need to specify the type of the parameter, as in:
p...
I have a LINQ query that looks like this:
public IEnumerable<Foo> SelectFooBars()
{
return
from
f in foos
join
b in bars
on f.BarId equals b.Id
select
AddMissingProp(f, b.MissingProp);
}
public void AddMissingProp(Foo foo, string missingProp) // substitute this...
Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does:
/// <summary>
/// immutable tuple for two
/// </summary>
public class Pair<TValue1, TValue2> : Singleton<TValue1>
{
public TValue2 Value2 { get; private set; }
public Pair(TValue1 value1, TValue2 value2, Fu...
Hello,
I was wondering if this actually worked ?
private void RegisterKeyChanged(T item) {
item.OnKeyChanged += (o, k) => ChangeItemKey((T)o, k);
}
private void UnRegisterKeyChanged(T item) {
item.OnKeyChanged -= (o, k) => ChangeItemKey((T)o, k);
}
how does the compiler know that the event handlers are ...
Hi,
I'm gonna create a BackgroundWorker with an anonymous method.
I've written the following code :
BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(
() =>
{
int i = 0;
foreach (var item in query2)
{
....
....
}
}
);
But Delegate '...
I am coming from a functional-programming background at the moment, so forgive me if I do not understand closures in C#.
I have the following code to dynamically generate Buttons that get anonymous event handlers:
for (int i = 0; i < 7; i++)
{
Button newButton = new Button();
newButton.Text = "Click me!";
newButton.Click ...
Hi, this is a constructed example. I don't want to post the original code here. I tried to extract the relevant parts though.
I have an interface that manages a list of listeners.
TListenerProc = reference to procedure (SomeInt : ISomeInterface);
ISomeInterface = interface
procedure AddListener (Proc : TListenerProc);
end;
No...
The following code (constructed only to demonstrate the problem) compiles and works in Delphi 2010. In Delphi 2009, compiler fails with "E2035 Not enough actual parameters".
program Project50;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TMyProc = reference to procedure(param: integer);
var
a: TProc;
b: TMyProc;
begin
b := proc...
Hi all, can anyone explain why the code below fails?
type TIDEThemeObserverFunc = reference to procedure(foo: integer);
var fObserverFuncs: TList<TIDEThemeObserverFunc>
function RegisterEventObserver(aObserverFunc: TIDEThemeObserverFunc): Pointer;
begin
fObserverFuncs.Add(aObserverFunc);
Result := @aObserverFunc;
// line below ...
How would you convert this to VB (using .NET 4.0 / VS2010) ?
bw.DoWork += (o, args) =>
{
Code Here
};
I thought maybe like this:
AddHandler bw.DoWork,
Function(o, args)
Code Here
End Function
But it says Function does not return a value on all code paths.
Ideas?
...
I am new in the functional side of C#, sorry if the question is lame.
Given the following WRONG code:
var jobSummaries = from job in jobs
where ...
select new
{
ID = job.ID,
Description = job.Description,
Fi...
Hi,
I had a question answered which raised another one, why following does not work? I do not understand it. The compiler says: Cannot convert anonymous method do string. But why?
public List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
a.IterateObjects(B);
// w...
Title kinda says it all. The usual SOS command !bpmd doesn't do a lot of good without a name.
Some ideas I had:
dump every method, then use !bpmd -md when you find the corresponding MethodDesc
not practical in real world usage, from what I can tell. Even if I wrote a macro to limit the dump to anonymous types/methods, there's no ob...
How can you identify anonymous methods via reflection?
...
I was asked to explain the ugly thing and advantages of anonymous method.
I explained possibly
Ugly thing
anonymous methods turning quickly into spaghetti code.
Advantages
We can produce thread safe code using anonymous method :Example
static List<string> Names = new List<string>(
new string[] {
"Jon Skeet",
"Marc Grav...
I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined.
Example :
List<Person> gurus =
new List<Person>()
{
new Person{id=1,Name="Jon Skeet"},
new Person{id=2,Name="Marc Gravell"},
new Person{id=3,N...
In a previous question, I asked how to get a MethodInfo from an Action delegate. This Action delegate was created anonymously (from a Lambda). The problem I'm having now is that I can't invoke the MethodInfo, because it requires an object to which the MethodInfo belongs. In this case, since the delegates are anonymous, there is no own...
Hi. I'm trying to iterate through all components and for those who implements ISupportsOpen allow to open a project.
The problem is when the anonymous method is called, then the component variable is always the same element (as coming from the outer scope from IEnumerable)
foreach (ISupportsOpen component in something.Site.Container.Com...
Can anyone explain what are the LINQ, Lambda, Anonymous Methods, Delegates meant?
How these 3 are different for each other?
Was one replaceable for another?
I didn't get any concrete answer when i did Googling
...