What is 'var' and when should I use it?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1382 Views - Last Post: 30 August 2011 - 01:38 PM Rate Topic: -----

#1 Rhokk  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 17-June 11

What is 'var' and when should I use it?

Posted 23 August 2011 - 07:12 PM

Hello,

I've recently started a new job working with a project written in mostly C#. There are several instances which I've noticed in the project where variables are declared with the type var. An example:

 var myObject = new ChartObject(); 


To me this looks like loose typing (like in Javascript). If this is the case, why would I pick this over explicit typing? Is this all a matter of coding style and preference? Am I reading too much into this? :)

Thanks!

This post has been edited by Rhokk: 23 August 2011 - 07:19 PM


Is This A Good Question/Topic? 1
  • +

Replies To: What is 'var' and when should I use it?

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,688
  • Joined: 13-March 10

Re: What is 'var' and when should I use it?

Posted 23 August 2011 - 07:19 PM

'var' is simply put used for implicit type declarations. It can only be used as local variables.
So if you have a field, it has to be explicitly declared. It cannot be used as return type and it cannot be used as parameters.

public static void PrintString(string s)
{
   System.Console.WriteLine(s);
}

static void Main()
{
    var s = "String";
    PrintString(s);
}



Compiler will know at compile time that s is a string. If you change it to a number, it wont compile.

Edit: 'var' is mostly used in LINQ.

This post has been edited by Curtis Rutland: 23 August 2011 - 07:28 PM

Was This Post Helpful? 3
  • +
  • -

#3 Rhokk  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 26
  • Joined: 17-June 11

Re: What is 'var' and when should I use it?

Posted 23 August 2011 - 07:20 PM

Ah, thanks for explaining! This just made my life a whole lot easier! xD
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4462
  • View blog
  • Posts: 7,776
  • Joined: 08-June 10

Re: What is 'var' and when should I use it?

Posted 23 August 2011 - 07:33 PM

*
POPULAR

To expand, or rather to confirm, what darek has stated, var is absolutely still static and strong typing. If you did need a late-bound (loose) type, you can use dynamic, though that's usually best to avoid if possible.

The reason that var is often used with LINQ is because LINQ frequently uses anonymous types, and the only way to declare a variable of an anonymous type is to use var (since the type itself cannot be written out, since it doesn't exist until the compiler reads the anonymous type itself).

I use it all the time, since it's simpler to write, and just as accurate as an explicit declaration.
Was This Post Helpful? 5
  • +
  • -

#5 Bloosdhed  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 22
  • Joined: 18-August 11

Re: What is 'var' and when should I use it?

Posted 23 August 2011 - 11:24 PM

I find myself only using var in cases like this where its really easy to see what the var is going to be and saves me half a line of text:

var lParams = new List<SqlParameter>();

var sqlConn = new SqlConnection("bla");



for types like string, int, bool and such i still just use

int count = 0;

string newLine = "bla";

Was This Post Helpful? 2
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: What is 'var' and when should I use it?

Posted 24 August 2011 - 05:56 AM

We only use var like the MSDN documentation does: However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required.
Was This Post Helpful? 2
  • +
  • -

#7 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: What is 'var' and when should I use it?

Posted 26 August 2011 - 06:45 AM

The Gods of Resharper will punish those who DON'T use var, SEVERELY... they are always watching... waiting... :ninja:

Seriously though, like others have stated, I use it primarily when it is easy to determine the data type by looking on the other side of the "=", whether from the assignment or a cast or whatever...
Was This Post Helpful? 1
  • +
  • -

#8 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: What is 'var' and when should I use it?

Posted 26 August 2011 - 04:00 PM

View Posth4nnib4l, on 26 August 2011 - 07:45 AM, said:

The Gods of Resharper will punish those who DON'T use var, SEVERELY... they are always watching... waiting... :ninja:

Seriously though, like others have stated, I use it primarily when it is easy to determine the data type by looking on the other side of the "=", whether from the assignment or a cast or whatever...


Haha your post made me laugh. You've been +1ed for that :)
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4462
  • View blog
  • Posts: 7,776
  • Joined: 08-June 10

Re: What is 'var' and when should I use it?

Posted 26 August 2011 - 04:12 PM

Yeah, it's interesting that Resharper considers that a warning, rather than a suggestion. You should be able to change that though, since I know a lot of people don't like using var.
Was This Post Helpful? 0
  • +
  • -

#10 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: What is 'var' and when should I use it?

Posted 29 August 2011 - 08:32 AM

View PostCurtis Rutland, on 26 August 2011 - 04:12 PM, said:

Yeah, it's interesting that Resharper considers that a warning, rather than a suggestion. You should be able to change that though, since I know a lot of people don't like using var.


Yeah, I have changed the way that Resharper responds to that now (to suggestion), but I'm honestly glad that it said something, because I didn't know that C# offered var. The first time I saw var, I had the same reaction as the OP (relating it to the loosely typed var in Javascript), so I researched it and learned something new.
Was This Post Helpful? 0
  • +
  • -

#11 cdpace  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 52
  • Joined: 24-July 10

Re: What is 'var' and when should I use it?

Posted 30 August 2011 - 07:25 AM

Correct me if im wrong, but var will get the type assigned during compile time, and can be used when the return type is not know immediatly...
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4462
  • View blog
  • Posts: 7,776
  • Joined: 08-June 10

Re: What is 'var' and when should I use it?

Posted 30 August 2011 - 07:29 AM

That's essentially what we've all been saying, yes. var causes a variable to be implicitly typed. This type is fixed at compile time, not runtime, and is static. The compiler uses the best match it can make for the type, which nearly all the time is a perfect match. Occasionally you have to be explicit when looping through an old-style collection, since they're full of objects, but for the most part, you can use var the same way you could declare the actual type.
Was This Post Helpful? 0
  • +
  • -

#13 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: What is 'var' and when should I use it?

Posted 30 August 2011 - 08:14 AM

Posted Image

We should compile a good answer and paste it to the C# FAQ. This question is brough up every couple of weeks and the same answers are given every couple of weeks.

We should just copy paste the link to these threads to avoid repeating the same thing over and over again.
Was This Post Helpful? 1
  • +
  • -

#14 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: What is 'var' and when should I use it?

Posted 30 August 2011 - 11:29 AM

This is really how I explain what 'var' really is.

So, their are a whole bunch of types of variables, i.e. int, string, bool, ect. Now, var I think is more of a broad way of declaring a variable. Basically, their isn't really a type defined the the declared 'var' because it can be used mostly as all types of variables. For example:

var type = 10;
var type2 = 0.1;
var type3 = "Hello";
var type4 = true/false;
ect.



So, I think more of it as a way to be able to be more broad about what types of variables you declare, which I hope you understand. In understanding, 'var' is a way to declare a variable without having to give it a specific type.

Hope this is understandable.

EDIT: Though, only use it if you don't want to type long lines of code. 'var' is more of a fixed variable.

This post has been edited by CreaturGames: 30 August 2011 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

#15 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: What is 'var' and when should I use it?

Posted 30 August 2011 - 11:38 AM

Quote

In understanding, 'var' is a way to declare a variable without having to give it a specific type.


I wouldn't do that if I were you, kind of iffy. If you are using basic types, just type them out. It helps with legibility and prevents the issue of having to look at the right side of the assignment, on left right assigments, to see the type.

I only use var when working with Linq as I don't want to type out IEnumerable<Yadayadayada> every time I access data or collections.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2