Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# "var" bad or benefits?

C#

  • Please log in to reply
31 replies to this topic

#1 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 27 August 2012 - 03:43 PM

I see a lot of programmers are starting to use var as their variable name.

such as:

var test = new MyObject();

instead of MyObject test = new MyObject();

What's your opinions on this? Do you think it's a good idea or bad practice? I'm coming from a JAVA programmer so I like everything clean that means no "var" for me, but tell me what do you think?
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#2 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts
  • Programming Language:C, C++, C#, Perl, Visual Basic .NET, Assembly, VBScript, Others

Posted 27 August 2012 - 06:58 PM

var isn't necessarily bad, in fact, everything is properly and strongly typed when you use the var keyword anyways. It's much easier to use var than it is to use something like...

IEnumerable<IGrouping<string, infoClass[]>> t = ______

(IEnumerable<IGrouping<string, infoClass[]>> t = ______ vs. var t = ______)

It's GOOD to know what kind of variable you have though, so if you like seeing that in your code (for easier debugging, or whatever) then don't use var as often.

I see people that ONLY use var. That is bad.
  • 0

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image


#3 Nimrod

Nimrod

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL
  • Learning:Java

Posted 28 August 2012 - 02:25 AM

Var isn't a bad at all.. Sometimes its even necessary to use it. E.g.

var results = context.People.Select(p => new {p.PersonID, p.Name});

The linq query selects an object of an anonymous type. C# offers you 2 possibilities to store the object in a variable. Either use Object or var. In this example var gives you the advantage. Because var will force the compiler to remember the properties.

Console.WriteLine(results.Name);

The second reason why var isnt that bad at all is because the compiler doesnt make a difference between strong typed variables and var. More information about that subject is found here:

http://www.interact-...3/varisntobject

http://stackoverflow...ype-performance
  • 0

#4 CodeTiger

CodeTiger

    CC Regular

  • Member
  • PipPipPip
  • 25 posts
  • Programming Language:Java, C++, C#, JavaScript, Lua
  • Learning:Objective-C

Posted 28 August 2012 - 03:29 AM

I think var, is only sometimes useful but you shouldn't need it too often. And you really shouldn't replace all class names by "var". I especially don't use it, because I learned to write MyObjects myObjects; and I go without "var".
What do you think of "dynmic"?
Since C# 4.0, you can write
dynamic test = 10; //test is int
test = "Hallo"; //test is now string

  • 0

#5 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 28 August 2012 - 03:34 AM

I think var, is only sometimes useful but you shouldn't need it too often. And you really shouldn't replace all class names by "var". I especially don't use it, because I learned to write MyObjects myObjects; and I go without "var".
What do you think of "dynmic"?
Since C# 4.0, you can write

dynamic test = 10; //test is int
test = "Hallo"; //test is now string


You want to say that with dynamic you can write like in Python your variables. You can set a number value to it and later change it to some string or character value and reversed?
  • 0

Microsoft Student Partner, Microsoft Certified Professional


#6 CodeTiger

CodeTiger

    CC Regular

  • Member
  • PipPipPip
  • 25 posts
  • Programming Language:Java, C++, C#, JavaScript, Lua
  • Learning:Objective-C

Posted 28 August 2012 - 03:35 AM

yes, this works.. with C# 4.0.
But what's about performance when you use dynamic?
  • 0

#7 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 28 August 2012 - 03:44 AM

I think it would be the same if you use converting in this example:

int number = 9;
string converted = Convert.ToString(number);

  • -1

Microsoft Student Partner, Microsoft Certified Professional


#8 Nimrod

Nimrod

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL
  • Learning:Java

Posted 28 August 2012 - 03:48 AM

Dynamic and var are way different.

Var is used as a syntax feature for storing implicit strong typed variables. Dynamics on the other hand bypass all type checking by the compiler. So in general, using var is just like using strong types such as int, char, string and so on. The only diffrence is the syntax. Using dynamics is a whole other story. Dynamics are more like a big piece of memory with no type checking at all. When you throw an int and a string in it it will figure out how to combine those 2 to and make something of it.

Var:
http://msdn.microsof...y/bb383973.aspx
Dynamic:
http://msdn.microsof...y/dd264741.aspx
  • 0

#9 CodeTiger

CodeTiger

    CC Regular

  • Member
  • PipPipPip
  • 25 posts
  • Programming Language:Java, C++, C#, JavaScript, Lua
  • Learning:Objective-C

Posted 28 August 2012 - 04:29 AM

why does Microsoft implements dynamic, I think there is no case where you have to use "dynamic"
var is useful for anounymous classes and for short type names but dynamic?
  • 0

#10 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 28 August 2012 - 05:05 AM

It's rather simple, var is an implicitly typed variable that is inferred by the compiler, but it is just as strongly typed as if you had explicitly typed it yourself using “int test2 = 2;”.

A dynamic variable bypasses all compile-time type checking and resolves everything at runtime.

An example:

dynamic test = 1;
test = "i'm a string now"; // compiles and runs just fine
var test2 = 2;
test2 = "i'm a string now"; // will give compile error
  • 1
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#11 CodeTiger

CodeTiger

    CC Regular

  • Member
  • PipPipPip
  • 25 posts
  • Programming Language:Java, C++, C#, JavaScript, Lua
  • Learning:Objective-C

Posted 28 August 2012 - 05:53 AM

but you could do this also with an extra variable. I'm not sure about dynamic. The source code with dynamic variables is a bit confusing. You don't know exactly which type a variable is. And you have to look where you have initalized it..
  • 0

#12 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 28 August 2012 - 08:09 AM

One thing that I'm puzzling about var is how can you tell this:

var amount = 1000;

is amount double int int32 int64 long? Which one is it?
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download