I have an array of Button objects that I want to pass a function with different arguments into.  This function takes an int argument.  I've tried the following:  public void ButtonClick(int i){   //do things with value of i }  public void FillButtonClicks(){   for(int i = 0; i &lt; 3; i++){     ButtonArray[i].AddListener(delegate{ButtonClick(i);});   } }   This "works" in that the buttons receive the appropriate delegates, but it does NOT work in that the garbage collector is holding onto the value of 'i' in the for loop, i.e., when I click any of the three buttons, they all execute ButtonClick(3).  Is there a way to enforce the passing of values, or do I have to just do something dumb like:  ButtonArray[0].AddListener(delegate{ButtonClick(0);});     ButtonArray[1].AddListener(delegate{ButtonClick(1);});     ButtonArray[2].AddListener(delegate{ButtonClick(2);});
1840
I met an interesting issue about C#. I have code like below.  List&lt;Func&lt;int&gt;&gt; actions = new List&lt;Func&lt;int&gt;&gt;();  int variable = 0; while (variable &lt; 5) {     actions.Add(() =&gt; variable * 2);     ++ variable; }  foreach (var act in actions) {     Console.WriteLine(act.Invoke()); }   I expect it to output 0, 2, 4, 6, 8. However, it actually outputs five 10s.  It seems that it is due to all actions referring to one captured variable. As a result, when they get invoked, they all have same output.  Is there a way to work round this limit to have each action instance have its own captured variable?
3258
Given a method DoSomething that takes a (parameterless) function and handles it in some way. Is there a better way to create the "overloads" for functions with parameters than the snippet below?  public static TResult DoSomething&lt;TResult&gt;(Func&lt;TResult&gt; func) {     //call func() and do something else }  public static TResult DoSomething&lt;T0, TResult&gt;(     Func&lt;T0, TResult&gt; func,     T0 arg0) {     return DoSomething(() =&gt; func(arg0)); }  public static TResult DoSomething&lt;T0, T1, TResult&gt;(     Func&lt;T0, T1, TResult&gt; func,     T0 arg0, T1 arg1) {     return DoSomething(arg =&gt; func(arg, arg1), arg0); }  public static TResult DoSomething&lt;T0, T1, T2, TResult&gt;(     Func&lt;T0, T1, T2, TResult&gt; func,     T0 arg0, T1 arg1, T2 arg2) {     return DoSomething(arg =&gt; func(arg, arg1, arg2), arg0); }
5040
I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class but I'm getting compilation errors.   Here is what I did from my constructor:  m_cRedundencyManager-&gt;Init(this-&gt;RedundencyManagerCallBack);   This doesn't compile - I get the following error:      Error  8 error C3867: 'CLoggersInfra::RedundencyManagerCallBack': function call missing argument list; use '&amp;CLoggersInfra::RedundencyManagerCallBack' to create a pointer to member   I tried the suggestion to use &amp;CLoggersInfra::RedundencyManagerCallBack - didn't work for me.   Any suggestions/explanation for this??  I'm using VS2008.  Thanks!!
118537
In C#, if you want a method to have an indeterminate number of parameters, you can make the final parameter in the method signature a params so that the method parameter looks like an array but allows everyone using the method to pass as many parameters of that type as the caller wants.  I'm fairly sure Java supports similar behaviour, but I cant find out how to do it.
34475
I need to copy the subscribers of one event to another event. Can I get the subscribers of an event (like MyEvent[0] returning a delegate)?  If this is not possible I would use the add accessor to add the delegates to a list. Would that be the best solution?
37596
Say I have a class that's meant to perform a single function. After performing the function, it can be destroyed. Is there any reason to prefer one of these approaches?  // Initialize arguments in constructor MyClass myObject = new MyClass(arg1, arg2, arg3); myObject.myMethod();  // Pass arguments to method MyClass myObject = new MyClass(); myObject.myMethod(arg1, arg2, arg3);  // Pass arguments to static method MyClass.myMethod(arg1, arg2, arg3);   I was being intentionally vague about the details, to try to get guidelines for different situations. But I didn't really have in mind simple library functions like Math.random(). I'm thinking more of classes that perform some specific, complex task, but only require one (public) method to do it.
36511
I understand passing in a function to another function as a callback and having it execute, but I'm not understanding the best implementation to do that. I'm looking for a very basic example, like this:  var myCallBackExample = {     myFirstFunction : function( param1, param2, callback ) {         // Do something with param1 and param2.         if ( arguments.length == 3 ) {             // Execute callback function.             // What is the "best" way to do this?         }     },     mySecondFunction : function() {         myFirstFunction( false, true, function() {             // When this anonymous function is called, execute it.         });     } };   In myFirstFunction, if I do return new callback(), then it works and executes the anonymous function, but that doesn't seem like the correct approach to me.
20286
I have quickly read over the  documentation.  This kind of example has helped me to understand better, though:   delegate int del(int i); del myDelegate = x =&gt; x * x; int j = myDelegate(5); //j = 25   Still, I don't understand why it's such an innovation. It's just a method that dies when the "method variable" ends, right? Why should I use this instead of a real method?
13449
I can't understand where the final keyword is really handy when it is used on method parameters.    If we exclude the usage of anonymous classes, readability and intent declaration then it seems almost worthless to me.   Enforcing that some data remains constant is not as strong as it seems.    If the parameter is a primitive then it will have no effect since the parameter is passed to the method as a value and changing it will have no effect outside the scope.  If we are passing a parameter by reference, then the reference itself is a local variable and if the reference is changed from within the method, that would not have any effect from outside of the method scope.    Consider the simple test example below. This test passes although the method changed the value of the reference given to it, it has no effect.  public void testNullify() {     Collection&lt;Integer&gt; c  = new ArrayList&lt;Integer&gt;();           nullify(c);     assertNotNull(c);            final Collection&lt;Integer&gt; c1 = c;     assertTrue(c1.equals(c));     change(c);     assertTrue(c1.equals(c)); }  private void change(Collection&lt;Integer&gt; c) {     c = new ArrayList&lt;Integer&gt;(); }  public void nullify(Collection&lt;?&gt; t) {     t = null; }
44000
Actionscript 3.0 (and I assume Javascript and ECMAScript in general) lacks pass-by-reference for native types like ints. As a result I'm finding getting values back from a function really clunky.  What's the normal pattern to work around this?    For example, is there a clean way to implement swap( intA, intB ) in Actionscript?
4059
The situation is somewhat like-  var someVar = some_other_function(); someObj.addEventListener("click", function(){     some_function(someVar); }, false);   The problem is that the value of someVar is not visible inside the listener function of the addEventListener, where it is probably being treated as a new variable.
88408
I have this Visualforce code snippet in a page:  &lt;apex:outputLink value="javascript:if (window.confirm('Are you sure?')) DeleteQuoteLineItem('{!LineItem.QuoteLineItem.Id}');"&gt;     Del &lt;/apex:outputLink&gt;  &lt;apex:actionFunction action="{!DeleteQuoteLineItem}" name="DeleteQuoteLineItem" reRender="content"&gt;     &lt;apex:param value="test"/&gt; &lt;/apex:actionFunction&gt;   The two methods I'm using for testing are:  public void DeleteQuoteLineItem(String RecordID) {     System.debug('method with a paramater');     System.debug(RecordID); }  public void DeleteQuoteLineItem() {     System.debug('method without a parameter'); }   Everytime I click on the link to execute the apex:actionFunction, it's calling the DeleteQuoteLineItem() method without any parameter. I looked online and it seems like I need to create a class attribute and use assignTo= in my apex:param to populate it accordingly.  Is it possible to prevent this behavior and use standard method parameters, so I don't have to create class attributes? I want to work on a clean and optimized codebase, preventing unnecessary variable scope (so no class attribute when it's not required) and improve code re-usability by staying context agnostic.  Thanks
86340
It's known that you should declare events that take as parameters (object sender, EventArgs args). Why?
24218
Is this functionality going to be put into a later Java version?  Can someone explain why I can't do this, as in, the technical way Java's switch statement works?
38197
Can someone explain this method declaration syntax for me?  In this function, the number of rows of a UIPickerView (slot machine UI on the iPhone) is being returned.  From my understanding, the Method is called 'pickerView', and returns an NSInteger.    It passes in a pointer to the UIPickerview called 'pickerView' ... first, why is the method called the same name as the parameter?    Next there is NSInteger parameter called component that tells us which component we are counting the rows for.  The logic to decide which is in the body of the method.    What is 'numberOfRowsInComponent? It seems to describe the value we are returning, but it is in the middle of the parameters.    - (NSInteger) pickerView:(UIPickerView *)pickerView   numberOfRowsInComponent:(NSInteger)component {     if (component == kStateComponent)         return [self.states count];      return[self.zips count]; }
92031
Whats this syntax useful for :       function(String... args)   Is this same as writing       function(String[] args)    with difference only while invoking this method or is there any other feature involved with it ?
81621
If I write the following class:  public class Example {        int j;       int k;        public Example(int j, int k) {            j = j;            k = k;       }        public static void main(String[] args) {            Example exm = new Example(1,2);            System.out.println(exm.j);            System.out.println(exm.k);       }  }   The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write:  this.j = j; this.k = k;   But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand side of the expressions)? Is is the argument or the instance variable? Does it make a difference?  Are there other cases where the use of this is obligatory?