52

I am going over some code written by another developer and am not sure what long? means:

protected string AccountToLogin(long? id)
{
   string loginName = "";
   if (id.HasValue)
   {
      try
      {....
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ayush
  • 41,754
  • 51
  • 164
  • 239

6 Answers6

74

long is the same as Int64

long data type

The ? means it is nullable

A nullable type can represent the normal range of values for its underlying value type, plus an additional null value

Nullable Types

Nullable example:

int? num = null;
if (num.HasValue == true)
{
    System.Console.WriteLine("num = " + num.Value);
}
else
{
    System.Console.WriteLine("num = Null");
}

This allows you to actually check for a null value instead of trying to assign an arbitrary value to something to check to see if something failed.

I actually wrote a blog post about this here.

Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
15

long is an Int64, the ? makes it nullable.

Steve Guidi
  • 19,700
  • 9
  • 74
  • 90
Lucero
  • 59,176
  • 9
  • 122
  • 152
6

long? is a 64-bit, nullable integer.

To clarify, nullable means it can be null or an integer number ( 0, 1, etc.).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
automagic
  • 1,067
  • 8
  • 10
6

"long?" is a nullable 64-bit signed integer. It's equivalent to Nullable<Int64>.

Lucero
  • 59,176
  • 9
  • 122
  • 152
Peter
  • 12,541
  • 3
  • 34
  • 39
4

long? is a nullable type. This means that the id parameter can have a long value or be set to null. Have a look at the HasValue and Value properties of this parameter.

Bernard
  • 7,908
  • 2
  • 36
  • 33
3

It's a nullable type declaration.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104