Skip to main content
Added anonymous types to the list, as it is mentioned in the comments anyhow.
Source Link

There has been a lot of dispute over the use of var. My general rules are the following.

  • When the type is obvious such as when the right hand of the assignment is a constructor, use var.
  • When the type is complex to write, such as a LINQ query (the reason for var in the first place) use var.
  • For ambivalent types (your Decimal being an example) where you want to make sure that your variable is correctly typed, spell it out.
  • Anonymous types have to use var.
  • In all other cases spell out the type.

Basically, the goal is to make it easier to read the code. If you feel that var suffices because the assignment is obvious, use var. Use the full type name as a hint for the reader when you feel it's necessary.

There has been a lot of dispute over the use of var. My general rules are the following.

  • When the type is obvious such as when the right hand of the assignment is a constructor, use var.
  • When the type is complex to write, such as a LINQ query (the reason for var in the first place) use var.
  • For ambivalent types (your Decimal being an example) where you want to make sure that your variable is correctly typed, spell it out.
  • In all other cases spell out the type.

Basically, the goal is to make it easier to read the code. If you feel that var suffices because the assignment is obvious, use var. Use the full type name as a hint for the reader when you feel it's necessary.

There has been a lot of dispute over the use of var. My general rules are the following.

  • When the type is obvious such as when the right hand of the assignment is a constructor, use var.
  • When the type is complex to write, such as a LINQ query (the reason for var in the first place) use var.
  • For ambivalent types (your Decimal being an example) where you want to make sure that your variable is correctly typed, spell it out.
  • Anonymous types have to use var.
  • In all other cases spell out the type.

Basically, the goal is to make it easier to read the code. If you feel that var suffices because the assignment is obvious, use var. Use the full type name as a hint for the reader when you feel it's necessary.

Source Link
Michael Brown
  • 21.8k
  • 3
  • 48
  • 83

There has been a lot of dispute over the use of var. My general rules are the following.

  • When the type is obvious such as when the right hand of the assignment is a constructor, use var.
  • When the type is complex to write, such as a LINQ query (the reason for var in the first place) use var.
  • For ambivalent types (your Decimal being an example) where you want to make sure that your variable is correctly typed, spell it out.
  • In all other cases spell out the type.

Basically, the goal is to make it easier to read the code. If you feel that var suffices because the assignment is obvious, use var. Use the full type name as a hint for the reader when you feel it's necessary.