Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.VisualBasic.Now vs System.DateTime.Now

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.

like image 508
chris Avatar asked Oct 08 '10 14:10

chris


2 Answers

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
like image 154
GvS Avatar answered Nov 15 '22 08:11

GvS


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.

like image 30
egrunin Avatar answered Nov 15 '22 06:11

egrunin