2

I'm doing C# (with Unity3D / Mono Runtime 2.6.5) quite for a while now, but recently I encountered a very strange behaviour, when calling an event where a method with a boolean default parameter is attached to.

When attaching it directly (Case 1), it somehow becomes true, even though the default value is false. While when attaching it via a lambda expression (Case 2), it is correctly set to false.

I really had expected it to become false in any case, where the param is not provided.

Attached you can find an example code of this strange behaviour:

public class SomeClass
{
    public event Action SomeEvent;


    public void TriggerSomeEvent()
    {
        if (SomeEvent != null)
        {
            SomeEvent();
        }
    }
}


public class AnotherClass
{
    public AnotherClass(SomeClass someClass)
    {
        someClass.SomeEvent += AnotherMethod; // Case 1:  somehow "someParam" becomes true
        someClass.SomeEvent += () => AnotherMethod(); // Case 2: "someParam" becomes the expected false
    }


    void AnotherMethod(bool someParam = false)
    {
        // How can "someParam" in the first case (see above) become true?
        Debug.Log(someParam);
    }
}

Invoke the example by:

void OnGUI() 
{
    if(GUILayout.Button("test"))
    {
        var someClass = new SomeClass();
        var anotherClass = new AnotherClass(someClass);

        someClass.TriggerSomeEvent();
    }
}

Output:

true
false

Update

Like Iain Smith pointed out the output is more of a random nature. So the first output does not always become true, it also turns to false from time to time.

Community
  • 1
  • 1
d4Rk
  • 6,622
  • 5
  • 46
  • 60
  • 1
    Trying to compile this, getting errors in someClass.SomeEvent += AnotherMethod; Can you please describe the environment you're in or double check your example? – oppassum Jul 06 '15 at 17:52
  • 1
    I get a `No overload for 'AnotherMethod' matches delegate 'System.Action'` on your `someClass.SomeEvent += AnotherMethod;` line (case 1). – Jashaszun Jul 06 '15 at 17:53
  • I double checked the example, it's working without any issues here. I'm doing this in Unity3D / Xamarin Studio / Mono Runtime 2.6.5 (shipped with unity 5.1.1). May be it is a Unity/Mono specific issue? – d4Rk Jul 06 '15 at 17:57
  • Just updated my question accordingly. – d4Rk Jul 06 '15 at 18:00
  • Did you create the event? Its obviously passing a boolean parameter of true. – maraaaaaaaa Jul 06 '15 at 18:49
  • Where does it "obviously passing a boolean parameter of true"? Could you explain this more in details? – d4Rk Jul 06 '15 at 19:19
  • Well i was asking if you created the event? Could you show the *actual* event definition? because by what you are showing it is clearly just passing a true parameter. – maraaaaaaaa Jul 06 '15 at 20:52
  • I cant see how it is 'clearly' passing true. This is really odd, I changed the type to int and it gives -1779033024, changed it to string and its gives System.Action. It might possibly be something to do this http://stackoverflow.com/a/7690501/1107580 "The runtime is not aware of optional parameters" but still i would have thought it would default to false. – Iain Smith Jul 06 '15 at 21:22
  • Good finding! It really seems to be more of a random nature, as using an integer will provide a random number, each time. When I invoke it using my original bool param, and run it many times, it does also become `true` and `false` from time to time.. – d4Rk Jul 07 '15 at 16:41

0 Answers0