title: C#: Sane alternative to optional parameter in property?
body: <p>Given: a class with properties like SomeNumber that, <em>internally</em> can be null (i.e. <code>int?</code>), but externally, you typically don't want to handle that special case, and pretend they start out as zero. So far, we're always returning a non-nullable version, i.e. <code>int</code>. I'd like to add an option to have a nullable version.</p>
  
  <p><strong>The easiest variant:</strong> Two properties. Works great, but unfortunately clutters the class with lots of largely redundant options. External code:</p>
  
  <pre><code>return myObject.SomeNumber; // Not nullable.
  return myObject.SomeNumber_Nullable; // Nullable.
  </code></pre>
  
  <p><strong>The internally dirty, but externally nice variant:</strong> A property with an optional parameter. I haven't yet found out how to do this in C#; .NET Reflector seems to be omitting incorrect code. In VB.NET, it looks like so:</p>
  
  <pre><code>Private _SomeNumber As Nullable(Of Integer)
  Public Property SomeNumber(Optional ByVal AllowNull As Boolean = False) As Nullable(Of Integer)
    Get
      If Not AllowNull AndAlso _SomeNumber Is Nothing
        Return 0
      End If
  
      Return _SomeNumber
    End Get
    Set(ByVal value As Boolean)
      _SomeNumber = value
    End Set
  End Property
  </code></pre>
  
  <p>External code:</p>
  
  <pre><code>return myObject.SomeNumber; // Not nullable.
  return myObject.SomeNumber(true); // Nullable.
  </code></pre>
  
  <p>Among the disadvantages:
  * C# implementation is so ugly .NET Reflector doesn't even seem to know how to do it,
  * I find the general notion of parameterized properties questionable,
  * In the non-nullable example, the return type doesn't match the actual behavior of the property — the compiler now thinks it <em>can</em> be <code>null</code>, but it really can't.</p>
  
  <p>Any other ideas?</p>