0

I have been working very closely with csx lately and something that I noticed was that whenever we specify a variable it always used var name = "Hello World"; instead of string name = "Hello World"; . Is this due to a certain convention that developpers are expected when writting scripts or is there a more technical reason why var is used more often that specifying the actual type of the variable

Even in the scriptcs wiki (https://github.com/scriptcs/scriptcs/wiki/Writing-a-script ) which has some good content on csx , this nature of defining variables with var can be seen

So my question stands why do people always stick with var rather than specifying the variables such as string , int etc .

Devss
  • 67
  • 7
  • 3
    Because it’s easier to write and the type can be inferred. There’s no requirement for it, people are just used to it. – Sami Kuhmonen Jan 08 '20 at 17:23
  • I see ... So there is no such thing as performance of script would be better when using var over explicitly defining the variables and so on right ? – Devss Jan 08 '20 at 17:28
  • var should be used when the type of the variable is obvious from looking at the right hand side of the assignment. e.g. `var a = "a string"` If it isn't obvious a type should be used. e.g. `var score = game.score` vs `decimal score = game.score` The latter in this example is more explicit. As per [Microsoft Programming Guide](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions#implicitly-typed-local-variables) – Sjolfr Jan 08 '20 at 17:28
  • wow ! Thats some intersting information – Devss Jan 08 '20 at 17:30

0 Answers0