What does public DateTime? DateOfBirth {get; set;} mean?

  • Thread starter Thread starter RayLopez99
  • Start date Start date
R

RayLopez99

I saw this code recently:

public class MyClass
{
public string MyString;
public DateTime? DateOfBirth {get; set;}

public MyClass (string S, DateTime? dateOfBirth)
{
MyString = S;
DateOfBirth = dateOfBirth;
}
}

What does the "?" parameter stand for? Does this mean assignment of a
DateTime is optional? I thought you can't do that in C#

RL
 
RayLopez99 said:
I saw this code recently:

public class MyClass
{
public string MyString;
public DateTime? DateOfBirth {get; set;}

public MyClass (string S, DateTime? dateOfBirth)
{
MyString = S;
DateOfBirth = dateOfBirth;
}
}

What does the "?" parameter stand for? Does this mean assignment of a
DateTime is optional? I thought you can't do that in C#

RL

It means DateOfBirth is of type Nullable<DateTime> rather than DateTime.
DateOfBirth can take on a value of null, in otherwords. DateTime as a
structure would not normally allow this.

Mike
 
It means DateOfBirth is of type Nullable<DateTime> rather than DateTime.
  DateOfBirth can take on a value of null, in otherwords.  DateTime as a
structure would not normally allow this.

Mike

I did not know this, thanks for the info. Does that mean that upon
initialization the value defaults to null, or does it still default to
MinValue or 0?
 
I did not know this, thanks for the info. Does that mean that upon
initialization the value defaults to null, or does it still default to
MinValue or 0?

Write a quick program to test it.
 
It means DateOfBirth is of type Nullable<DateTime> rather than DateTime.
  DateOfBirth can take on a value of null, in otherwords.  DateTime as a
structure would not normally allow this.

Mike

Thanks FTM! I have dealt with nullable types in C# and this makes
sense.

RL
 
Back
Top