[Mono-list] Class Property Initializer Question...

Ryan Kirkpatrick ryan.kirkpatrick at inovonics.com
Tue Jul 21 12:44:18 EDT 2009

Ryan Kirkpatrick wrote:
> When I compile and run the following code with Visual Studio, the output 
> is 'Blue/Green' and 'Red/Red'. When I compile and run the same code 
> with Mono (2.4.2.x), the output is 'Blue/Green' and 'Blue/Green'.

Looked at this again with a fresh mind this morning and realized this 
only happens if the initial values set inline with 'new' are the 
variable type's default value. In other words, if I change the below 
code such that 'Red = 3' in the enum, then this problem all goes away!

Therefore, these 'inline initializers' are unable to set a class's 
variable/property to the default value of its data type if the class 
provides a value in its declaration or constructor. This looks like a 
compiler bug to me. :(

> ---------------------PROGRAM.CS-------------------------------------
> using System;
> using System.Collections.Generic;
> namespace ConsoleApplication2 {
>      enum Colors { Red = 0, Green = 1, Blue = 2 };
>      class Screen {
>          public Colors fore = Colors.Blue;
>          public Colors back;
>          public Screen() { back = Colors.Green; }
>      }
>      class Program {
>          static void Main(string[] args) {
>              Screen a = new Screen();
>              Console.WriteLine("{0} / {1}", a.fore, a.back);
>              a = new Screen() { back = Colors.Red, fore = Colors.Red };
>              Console.WriteLine("{0} / {1}", a.fore, a.back);
>          }
>      }
> }
> -----------------------------------------------------------------

Ryan Kirkpatrick <ryan.kirkpatrick at inovonics.com>, 303-209-7190
Senior Design Engineer
Inovonics Wireless Corporation

This email transmission and any documents, files or previous email
messages attached to it contains information that is confidential
or legally privileged. If you are not the intended recipient or a
person responsible for delivering this transmission to the intended
recipient, you are hereby notified that you must not read this
transmission and that any disclosure, copying, printing,
distribution or use of this transmission is strictly prohibited. If
you have received this transmission in error, please immediately
notify the sender by telephone or return email and delete the
original transmission and its attachments without reading or saving
in any manner. Thank you.

More information about the Mono-list mailing list