2

Possible Duplicate:
C# - Basic question: What is '?' ?

What is the benefit of using int16? Instead of using int16 in .net variable declaration ?

dim i as int16?
dim i as int16
Community
  • 1
  • 1
alasif
  • 23
  • 2

3 Answers3

5

It allows you to have a "null" value. Generic example:

public int MyFunction(int? x)
{
    int m = 99;

    if(x == null)
        m = 0;
    else
        m /= x.Value;

    return m;
}

Otherwise, if try to pass a null value to MyFunction, the compiler will return an error.

Jess
  • 2,991
  • 3
  • 27
  • 40
  • +1 @Gats - good catch :-) http://stackoverflow.com/questions/676078/which-is-preferred-nullable-hasvalue-or-nullable-null – Jess Apr 30 '11 at 08:35
  • I wouldn't call me clever.. I wrote an answer about why you'd use Int16 over Int32 before I realised I'd missed the point :D:D – Gats Apr 30 '11 at 08:39
1

Question mark indicates Nullable Types: http://msdn.microsoft.com/en-us/library/1t3y8s4s%28v=VS.100%29.aspx

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
1

It isn't really about "benefit" - it is about representing something subtly different; an Int16? is really just shorthand for Nullable<Int16>, which means it is capable of representing a null/empty state in addition to the regular Int16 range. But the downside is that it takes more space. So if space efficiency is your aim (which mainly only makes sense in an array, etc) you may want to think carefully.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900