-2

Possible Duplicate:
Performance of anonymous types in C#

Are anonymous types slower than declarative types?

For example, does

Dim score as Double = CalculateScore()

run slower than

Dim score = CalculateScore()

?

Why use explicit typing if it's not slower?

Community
  • 1
  • 1
Moderator71
  • 385
  • 7
  • 16
  • 1
    What are you talking about? I don't see any anonymous type in your question. In the first example it looks like you are calling a function while in the second you are just assigning a delegate variable pointing to a function. You are comparing apples to oranges. So what is your question? – Darin Dimitrov Dec 30 '10 at 20:32
  • 9
    This is not what "anonymous type" means. – Jay Dec 30 '10 at 20:33
  • 1
    If Option Infer is turned off this will produce a variant object which could lead to run time performance problems under the right circumstances. – asawyer Dec 30 '10 at 20:36
  • @Darin the second example could eithe be an inferred type, or vb's implicet object conversion, it depends on the project settings. – asawyer Dec 30 '10 at 20:37
  • 2
    @asawyer: I'd call them the "wrong" circumstances. :) – cHao Dec 30 '10 at 20:37
  • @asawayer Yes, and if that were the case then the reasons to avoid variants are numerous. Using variants will prevent the compiler from warning you about numerous scenarios where you might make a mistake. Thus, they should only be used where you really need them. – AaronLS Dec 30 '10 at 20:39
  • 1
    @AaronLS I know I have a monster vb.net project that was done with infer and stric turned off, it's a nightmare :( – asawyer Dec 30 '10 at 20:41
  • 2
    I'm not sure downvoting or voting-to-close a question because of misplaced nomenclature is in order. Enough of the intent of the question is apparent that we can at least try to be helpful. – Jay Dec 30 '10 at 20:47
  • @Darin: The second line does in fact call the `CalculateScore` method; in VB the `()` symbols are optional (to refer to the method for a delegate the way you would by omitting the parentheses in C#, you need to include `AddressOf`). I hate it but it's true. – Dan Tao Dec 30 '10 at 20:48
  • @Dan Tao, thanks for the good explanation. I guess I need some VB.NET skill refresh :-) It's been a long time since I wrote VB code. – Darin Dimitrov Dec 30 '10 at 20:49
  • VS2010 compiler will infer Double just like "var" does for C#. So I don't think there is any difference. It won't use "object" in this case. – turtlepick Dec 30 '10 at 21:07

5 Answers5

4

An implicitly typed variable is "syntactic sugar". It gets compiled into exactly the same code either way, so runtime performance is unaffected.

Why use an explicit declaration?

It is a matter of style/preference/standard of a developer or organization. Some find the code to be more clear or intention-revealing.

For the record, an anonymous type (notice no double-n in there) is one that is declared inline, or "on-the-fly," depending on how you look at it. It is anonymous because it has no name. It is a temporary object that can be useful for holding related data.

In VB, it is declared by calling Dim anon = New With { .Name = "Moderator71", .Id = 19 } You can optionally declare some properties as Key.

Jay
  • 56,361
  • 10
  • 99
  • 123
1

No, there is no difference in speed for anonymous or inferred types.

But maybe you are talking about dynamic types, they would be a little slower.

Your (2nd) code sample suggests an inferred type, meaning score would be at compile-time determined to be a double

H H
  • 263,252
  • 30
  • 330
  • 514
1

why use explicit typing?

Readability might be one.

AaronLS
  • 37,329
  • 20
  • 143
  • 202
1

Assuming that the CalculateScore Function returns a double.

No, it's the other way around. Declaring it As Double will always be as fast as is possible. Omitting the declaration will only be as fast when Option Infer On is in effect. If it is not then it will be a variable of type Object. The double will be boxed.

Unboxing is pretty expensive in VB.NET because it allows unboxing to any compatible type, not just Double. Unlike C# which throws an exception if there's a mismatch.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

The reason to use explicit typing vs infered is because it moves a lot of bugs from run time to compile time. I can fix a compile time bug in seconds, thanks to the IDE. If a bug occurs in run time it might be months or even years before someone runs that particular line of code, or users might be hitting it every day but it might be a long time before it gets back to me and I can fix it. I call those time bombs. You don't know where or when they will go off.

My number 1 goal is to write code that doesn't blow up, so compile time bugs are awesome.

dwidel
  • 1,264
  • 1
  • 12
  • 22