[Mono-dev] Nullable types and "as" operator

Michael Schurter michael at synthesyssolutions.com
Sun Sep 10 23:30:35 EDT 2006

The following code compiles and executes using MS.NET, but not Mono:


using System;

public class NullableInt
         public static void Main()
                 object x = null;

                 int? y = x as int?;  /* Causes CS0077 */

                 Console.WriteLine("y: '{0}'", y);
                 Console.WriteLine("y.HasValue: '{0}'", y.HasValue);

// END

Produces the following compiler error using gmcs (TRUNK, revision 65212):

test.cs(9,14): error CS0077: The as operator must be used with a 
reference type (`int?' is a value type)
Compilation failed: 1 error(s), 0 warnings

Runtime output in Windows using MS.NET:

y: ''
y.HasValue: 'False'

I found this Mono bug related to Nullable types, but it appears to be 

I first noticed this bug when trying to compile a large project which 
uses the syntax: "as int?" or "as bool?" in a few key places.

Michael Schurter
Synthesys Solutions

More information about the Mono-devel-list mailing list