5

One use for the var type in c# seems to be shortcuts/simplifying/save unnecessary typing. One thing that I've considered is this:

MyApp.Properties.Settings.Default.Value=1;

That's a ton of unnecessary code. An alternative to this is declaring:

using MyApp.Properties;

-- or --

using appsettings = MyAppp.Properties.Settings;

leading to: appsettings.Default.Value=1 or Settings.Default.Value=1

Abit better, but I'd like it shorter still:

var appsettings = MyFirstCSharpApp.Properties.Settings.Default;
appsettings.Value=1;

Finally, it's short enough, and it could be used for other annoyingly long calls too but is this an accepted way of doing it? I'm considering whether the "shortcut var" will always be pointing to the existing instance of whatever I'm making a shortcut too? (obviously not just the settings as in this example)

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • possible duplicate of [Use of var keyword in C#](http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c) – BrunoLM Dec 20 '10 at 15:34
  • I looked at the plugin, could you be more specific in how this helps? I assume that I could add snippets for the things I want shortcuts for, but this would not be an "in the moment" thing in the same way var's help. – Kristian Erik Tigersjäl Dec 20 '10 at 15:34
  • @BrunoLM: can't find much about whether the "shortcut" use for subclasses is safe or not. – Kristian Erik Tigersjäl Dec 20 '10 at 15:37
  • Resharper highlights areas of code with the potential to replace types with the var keyword. Resharper give recommendations for var such as "use var keyword when initializer explicitly declares type". In your case I would be hesitant to use var because the type is not clear from the initializer. – P.Brian.Mackey Dec 20 '10 at 15:43
  • @brunolm did you read the question? It's got nothing todo with the var keyword as much as variables – Rune FS Dec 20 '10 at 15:47
  • @Rune I guess I misunderstood the question as it was written before the edit. – BrunoLM Dec 20 '10 at 15:52

5 Answers5

7

It's acceptable code in that the compiler will take it and know what to do with it. It's acceptable code logically in that it shortens code later. It's really not any different than actually defining the variable type (int/bool/whatever) rather than saying var. When you're in the studio, putting your mouse of the variable gives you its compiled type, so there shouldn't be any real issue with it. Some might call it lazy, but laziness is the mother of invention. As long as your code doesn't become unreadable, I can't see how it would be much of a problem.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • 2
    And if your methods and classes follow the SRP (Single Responsibility Principle) and are short and to the point, and your variables are clearly named and declared close to the one or two lines of code that use them, there should never be any ambiguity about what a variable is for or what type it is... – Charles Bretana Dec 20 '10 at 15:41
1

There is nothing wrong with that, as long as the code is clear.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

In Fact, var is more and more used exactly for that : shortening the code.

Specially in the case of

MyClass myClass = new MyClass();

Which is very clear enough using

var myClass = new MyClass();

And btw, ReSharper helps you enforce that var is used everywhere it can be !

VdesmedT
  • 9,037
  • 3
  • 34
  • 50
1

Seems fine to me, it can enhance readability especially if you're using that long .notated syntax many times.

As a side, if you're using an indexed property or an autogenerated property (which does work on the fly for the GETTER) multiple times, then there can be a performance hit for each access of this property. That's microoptimisation though, so probably shouldn't worry too much about that.

Tom
  • 3,354
  • 1
  • 22
  • 25
1

Just be careful to know that the static variables you are referencing do not change from underneath you. For instance, the following would break your "shortcut":

var appsettings = MyFirstCSharpApp.Properties.Settings.Default;
MyFirstCSharpApp.Properties.Settings.Default = new DefaultSettings(); // new reference
appsettings.Value=1;

Of course, I am not suggesting that you would ever write code that does this, but we are talking about global variables here... any code anywhere can change out this reference. Caching the reference in appsettings CAN be dangerous in cases like these... one of the many pitfalls of being coupled to static variables, IMO.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167