Difference between int and int?, DateTime and DateTime?

Last Reply on May 06, 2016 03:57 AM By Shashikant

Posted on May 06, 2016 03:47 AM

Please tell me the basic difference to write ?

public class Accessor
    {
        public int? Salary { get; set; }
        public int Sal { get; set; }
        public DateTime? Date { get; set; }
        public DateTime Time { get; set; }

    }

Thanks

You are viewing reply posted by: Shashikant on May 06, 2016 03:57 AM.
Posted on May 06, 2016 03:57 AM Modified on on May 06, 2016 04:27 AM

Hi,

As we know Int32 and DateTime is struct,struct is value type, hence we cannot assign null value to int and DateTime. where if you consider string,it is sealed class String, hence we can assign null to string.

so if you want to make value type to nullable, so you can put ? after datatype.

C#

int sal = null; //It will give syntex error
int? sal = null; // will work
DateTime date = null; //will give syntex error
DateTime? date = null; // will work
string name = null; // It is fine because String is class i.e. reference type

I hope this will help you out.