Go to Definition Tip with the C# ‘var’ keyword

December 2nd, 2009

This may be totally obvious to the masses out there, and it isn’t much of a tip, other than to say it works

Did You Know?

F12 (Go To Definition) – works on the C# var keyword?

(That’s all there is to this post – the rest is just rambling)

I hit it on accident the other day (yes I know, F12 isn’t exactly in the usual path of accidental keystrokes, trust me it was on accident). It brought Visual Studio to a screeching halt. That is, while VS was trying to load the object browser, and satellites were linking up in outer space trying get some message sent through the Pony Express about a tweetup with the Add Reference Dialog. (Point being – loading the object browser is REALLY SLOW)

It dawned on me that the F12 (Go To Definition) keyboard shortcut works on the var keyword.

Usually I just use the tool tip window when I don’t have time to decipher why the variable’s naming isn’t clear. (good post on the subject)

image 

FYI: for those R# fans, who noticed after installing it, you lost the code metadata window in C# when F12ing it (Go To Definition). They’ve fixed it in the upcoming version http://www.jetbrains.net/jira/browse/RSRP-35547. So my satellites/pony express/tweetup/add ref dialog comment above won’t be an issue anymore. Yippee!

  • http://simpleprogrammer.com John Sonmez

    That is pretty useful. I did not know that. The question though is, if someone has to use F12 to find out what the var type is, isn’t that a good enough reason to not use var in that situation? Not accusing you of writing bad code, just making a broad statement.

  • http://elegantcode.com Jason Jarrett

    @John Sonmez
    I agree, if you have to use F12 to find out what the type is, then yes you either haven’t named your variable well enough, or you should just not use the var keyword.

    One case where this could be useful, is if your cursor is already sitting “near” the var keyword & you have to (Go to Definition). Say your project owns the class and you need to jump to and modify/view the type.(I know there are many other ways to get to the type within a project), it’s just a tiny use case trying to make it’s way in a big world…

  • Pingback: Arjan’s World » LINKBLOG for Dec 2, 2009

  • wes

    @Jason Jarrett
    Um sweet, this saves Ctrl T in r#! I vary rarely use anything but var in tests but constantly like to jump to classes from them. It helps with readability of the test as I give scenario specific names to variables.