Bug 17860 - New "Use of unassigned local variable" error in 3.2.7
Summary: New "Use of unassigned local variable" error in 3.2.7
Status: RESOLVED FEATURE
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.2.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-02-18 17:24 UTC by Adam Patridge
Modified: 2014-02-19 12:44 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description Adam Patridge 2014-02-18 17:24:32 UTC
After updating to Mono 3.2.7, a chunk of previously-compiling code started causing a compile error.

    public static Task<T> GetEventAsync<T>(this object eventSource, string eventName) where T : EventArgs {
        var tcs = new TaskCompletionSource<T>();
        var type = eventSource.GetType();
        var ev = type.GetEvent(eventName);

        EventHandler handler = (sender, e) => {
            ev.RemoveEventHandler(eventSource, handler); // <-- Error CS0165: Use of unassigned local variable `handler'
            tcs.SetResult((T)e);
        };

        ev.AddEventHandler(eventSource, handler);
        return tcs.Task;
    }

This code is derived from code put out by Frank Krueger in a blog post (http://praeclarum.org/post/45231096776/await-in-the-land-of-ios-drag-n-drop), which also causes the same build error on 3.2.7, but I can't verify this code compiled previously.

    EventHandler handler;
    handler = delegate (object sender, EventArgs e) {
        ev.RemoveEventHandler(eventSource, handler);
        tcs.SetResult((T)e);
    };

Here's a workaround for as long as it is needed.

    EventHandler handler = null;
    handler = (sender, e) => {
        ev.RemoveEventHandler(eventSource, handler);
        tcs.SetResult((T)e);
    };
Comment 1 Marek Safar 2014-02-19 12:44:45 UTC
The compiler error is correct. The local variable is unassigned