Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime in VB.NET and C#

I have two questions:

  1. Date and DateTime : Are they different or same in VB ?

  2. DateTime can be assigned Nothing in VB, where as it cannot be assigned null in C#. Being a structure it cannot be null. So why is it being allowed in VB ?

---VB.NET-----

Module Module1

    Sub Main()
        Dim d As Date = Nothing
        Dim dt As DateTime = Nothing

        d = CType(MyDate, DateTime)


    End Sub

    Public ReadOnly Property MyDate As DateTime
        Get
            Return Nothing
        End Get
    End Property

End Module

---C#.NET-----

class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = null;//compile time error            
        }
    }
like image 364
Brij Avatar asked Jan 11 '13 15:01

Brij


2 Answers

Nothing in VB.NET is not the same as null in C#. It has also the function of default in C# and that is what happens when you use it on a structure like System.DateTime.

So both, Date and DateTime refer to the same struct System.DateTime and

Dim dt As Date = Nothing 

actually is the same as

Dim dt = Date.MinValue

or (in C#)

DateTime dt = default(DateTime);
like image 141
Tim Schmelter Avatar answered Sep 20 '22 13:09

Tim Schmelter


In c# You can use default keyword

DateTime dt = default(DateTime);

Date and DateTime are same in VB.NET. Date is just alias of DateTime

like image 34
Tilak Avatar answered Sep 19 '22 13:09

Tilak