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

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
 
F

Family Tree Mike

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
 
H

henry.lee.jr

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?
 
J

Jeff Johnson

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.
 
R

RayLopez99

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top