DateTime in VB.NET and C #

I have two questions:

  • Date and DateTime : are they different from VB?

  • DateTime can be set to Nothing in VB, where since it cannot be set to null in C #. Being a structure, it cannot be null. So why is this 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 } } 
+6
source share
3 answers

Nothing in VB.NET is not null in C #. It also has a default function in C #, and this happens when you use it in a structure like System.DateTime .

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

 Dim dt As Date = Nothing 

actually matches

 Dim dt = Date.MinValue 

or (in c #)

 DateTime dt = default(DateTime); 
+13
source

In C # you can use default keyword

 DateTime dt = default(DateTime); 

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

+3
source

There is simply an alias for DateTime in vb.net Date .

Some of the aliases that exist in VB exist for legacy purposes to help with conversions from vb6 applications.

+3
source

All Articles