views:

213

answers:

5

I'm sure this information is available in the C# spec, but I thought I'd give y'all a chance to set me straight. Why won't this code compile?

namespace DataObjects
{
    class QuestionMark
    {
        public bool Possible? {get; set;}
    }
}

Is there any way I can make my boolean properties end in a question mark? I think it would add to readability and be nifty.

Let's hear it!

A: 

? is a special character in C#. If it follows a type, it means the type is nullable.

Matthew Jones
It has to follow a type not a variable name.
helium
+15  A: 

Why don't you just name it IsPossible ? That is clear, consice and no subject for confusion.

The question mark is a special character, and is used to indicate that a value type is 'nullable'.

Frederik Gheysels
I'm aware of nullable<T> types, like int?. Can't the compiler tell the difference between a type and a name?
Chris McCall
foo?bar:baz. Ternary operator or label with a question mark in it followed by some expression baz?
helium
+1 Don't fight the compiler
Richard Szalay
A: 

It's not valid to do so.

Is this a Rubyism? :-)

frou
+9  A: 

I actually don't think it has much to do with nullable types as that is easy to determine base on a type declaration. It probably has more to do with the ternary operator. If you had a variable with a ? at the end you wouldn't be able to determine the difference between a normal reference to a variable and a ternery call. For example:

int a;
bool b?;
a = b? ? 15 : 22; 
messenger
Good point. Add the null coalescing operator and you could run into "b???0" if your spacing was bad. It's the same reason you can't have a variable named "b+".
Ian Henry
This looks like the most likely reason it was in the spec. Thanks for the informative answer!
Chris McCall
+2  A: 

From the standard:

identifier-start-character::
    letter-character
    _ (the underscore character U+005F)

identifier-part-characters::
    identifier-part-character
    identifier-part-characters identifier-part-character

identifier-part-character::
    letter-character
    decimal-digit-character
    connecting-character
    combining-character
    formatting-character

letter-character::
    A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
    A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl

combining-character::
    A Unicode character of classes Mn or Mc
    A unicode-escape-sequence representing a character of classes Mn or Mc

decimal-digit-character::
    A Unicode character of the class Nd
    A unicode-escape-sequence representing a character of the class Nd

connecting-character::
    A Unicode character of the class Pc
    A unicode-escape-sequence representing a character of the class Pc

formatting-character::
    A Unicode character of the class Cf
    A unicode-escape-sequence representing a character of the class Cf

So identifiers are limited to containing only characters that are considered "letters" by the Unicode standard. "?" is a punctuation character (class Po).

Dean Harding