Bug 4285 - assigning inline variable in C# style not supported
Summary: assigning inline variable in C# style not supported
Status: RESOLVED INVALID
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 5.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-04-06 01:08 UTC by duskov
Modified: 2012-04-11 08:05 UTC (History)
2 users (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 Developer Community or GitHub 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 INVALID

Description duskov 2012-04-06 01:08:41 UTC
Consider this code (MonoTouch.Dialog classes):
// BEGIN ===========
EntryElement entry1;
RootElement root = new RootElement("Test") {
	new Section () {
		entry1 = new EntryElement("123", "123", null),
	},
};
// END =============

(1)
In C# is possible like this (inline) to initialize "entry1" variable, and then send it's value at the same place. MonoTouch compiler will raise error at that line:
		entry1 = new EntryElement("123", "123", null),


(2) Here are also 2 syntax errors. After closing "Section" class I placed comma on purpose. I also placed comma after closing "EntryElement" constructor's brackets. Since there are no objects defined after those commas, compiler should also report invalid syntax. However, compiler disregards those commas, and no error is shown.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-04-11 08:05:17 UTC
(1) The MonoTouch compiler is correct. Try this equivalent code in Visual Studio:

    object entry1;
    var list = new List<object> {
        entry1 = new string ('a', 20),
    };

and you'll get this error:

error CS0117: 'System.Collections.Generic.List<object>' does not contain a definition for 'entry1'

What you need to do is to put parenthesis around the assignment, like this:
        (entry1 = new EntryElement ("123", "123", null)),

(2) The MonoTouch compiler is correct about this too. Try this equivalent in Visual Studio:

    object entry1;
    var list = new List<object> {
        (entry1 = new string ('a', 20)),
    };

In general a trailing comma is allowed when you have a list of items in C#. The same thing is allowed for enums for instance:

enum Foo { one, two, three, }