views:

71

answers:

5

Is there any difference between using the Microsoft.VisualBasic.DateAndTime.Now compared to the System.DateTime.Now?

I seem to remember reading something that said the MS.VB namespace was for legacy use only, and was much slower than using the native .net classes.

A: 

The VB namespace is there to make VB 6 developers feel at home.

Which is similar to legacy, but not quite.

awrigley
+2  A: 

I don't know if it's slower (and doubt it's enough to matter); but I've done enough porting between VB and C# and back to avoid the VisualBasic namespace when a System equivalent is available. It's just one more thing to worry about.

egrunin
+5  A: 

There might be some cases, where using the VB namespace might be slower (because of the added functionality and/or flexibility, not because MS wants VB to be slower).

But in the case of DateAndTime.Now, this is not the case. This is simply a wrapper around a call to DateTime.Now.

I suspect the call to be inlined by the JIT-compiler, so it will not make your code any slower (when compile for Release).

Prove: This is the IL from DateAndTime.Now:

.method public specialname static valuetype [mscorlib]System.DateTime get_Now() cil managed
{
    .maxstack 1
    .locals init (
        [0] valuetype [mscorlib]System.DateTime time)
    L_0000: call valuetype [mscorlib]System.DateTime [mscorlib]System.DateTime::get_Now()
    L_0005: ret 
}

this is exactly the same as:

Return DateTime.Now
GvS
+1 for the proof
egrunin
A: 

The VB6 function returned the fractional number of days since some reference point; the .net function returns a DateTime struct.

supercat
+1  A: 

You have to watch out for this. Not a problem with the Now property, it returns DateTime.Now. But there are subtle differences in other ones, like the Year() method. It uses the current calendar's GetYear() method which doesn't have to be the same as the DateTime.Now.Year property. Depending where your program is running, that might for example give the number of years since the temple in Jerusalem got destroyed. Use these VB6 compat functions only if you really want to emulate behavior of a legacy VB6 program.

Hans Passant