All posts by me

C# Conditional Attribute

#undef TEST_CONDITIONAL

public class TestMonoBehaviour : MonoBehaviour
{
    void Start()
    {
        ConditionalTest(TestFunction());
    }

    [System.Diagnostics.Conditional("TEST_CONDITIONAL")]
    public void ConditionalTest(string testData)
    {
        Debug.Log("ConditionalTest " + testData);
    }

    public string TestFunction()
    {
        Debug.Log("TestFunction");
        return "TestFunction";
    }
}

if use #undef TEST_CONDITIONAL
No log will be write.

if use #define TEST_CONDITIONAL
TestFunction
ConditionalTest

will write to console.

Generic Event Raiser

 public static void FireEvent(this EventHandler eventHandler, object sender, EventArgs e)
{
    if (eventHandler != null)
        eventHadler(sender, e);
}

public static void FireEvent<T>(this EventHandler<T> eventHandler, object sender, T e)
    where T : EventArgs
{
    if (eventHandler != null)
        eventHandler(sender, e);
}