1

I want to understand what is the best way for declaring a string, to use var or string if I already know that I have a string. For example I have the following:

string str = "here is some text";

or

var str = "here is some text";

Which one is better for the compiler in order the analyzers at Roslyn to work better for switch expressions vs switch statements? Thanks

Eleni S.
  • 31
  • 3

1 Answers1

2

From the language and tooling perspective, these are identical. Go with which one you find is easier to maintain or read.

Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • From the compiler perspective? – Eleni S. Oct 21 '22 at 19:33
  • @EleniS.: I guess I'm not sure what you're asking about here: the compiler treats them the same. Sure the compiler has to do extra type inference to come up with var being string, but other than that it's the same. – Jason Malinowski Oct 22 '22 at 00:20
  • My question if from compiler's perspective. What kind of extra type interference does the compiler need to do with var instead of string if you know that you have a string? – Eleni S. Oct 22 '22 at 03:28
  • It'll just look at the right hand side of the =, see it's a string, and then it knows the var is a string. Nothing fancy. – Jason Malinowski Oct 25 '22 at 18:18