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

#13 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 - 09:15 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..


In this case, yes. It's an overly simplified example to show how dynamic differs from var. Var is determined at compile time, dynamic at runtime.

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?


Why not check it yourself? Get the type from amount. It's really that simple.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#14 VNFox

VNFox

    CC Devotee

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

Posted 28 August 2012 - 10:00 AM

Why not check it yourself? Get the type from amount. It's really that simple.


When can you check this? At runtime ? What if I would like to know while I'm coding?
  • 0

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


#15 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 - 10:23 AM

If you're assigning it, you should already know.

var was really made for anonymous types from LinQ.

For example:

var custQuery = from cust in customers
where cust.City == "Phoenix"
select new { cust.Name, cust.Phone };
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#16 AceInfinity

AceInfinity

    CC Addict

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

Posted 28 August 2012 - 05:54 PM

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?


One thing you need to know here is that int & Int32 are the same thing. Same thing with Int64, and long. They are the same thing.

When can you check this? At runtime ? What if I would like to know while I'm coding?


Hover your mouse over the "var" keyword in your IDE, very simple. Otherwise:

var amount = 1000;
MessageBox.Show(amount.GetType().Name);

  • 0

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


#17 brokenkingpin

brokenkingpin

    CC Newcomer

  • Member
  • PipPip
  • 11 posts
  • Location:Canada
  • Programming Language:C++, C#, Others

Posted 02 September 2012 - 08:03 PM

It is a preference thing really. There are arguments both ways. I personally think it is fine for really long data types, but it does make code harder to understand... although in Visual Studio you can just hover over things to get the data types which helps. Most of the time I never think to use it.
  • 0

#18 AceInfinity

AceInfinity

    CC Addict

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

Posted 02 September 2012 - 08:16 PM

It is a preference thing really. There are arguments both ways. I personally think it is fine for really long data types, but it does make code harder to understand... although in Visual Studio you can just hover over things to get the data types which helps. Most of the time I never think to use it.


Preference related when you have the option not to use it, although in C# with anonymous types it's essentially required.
  • 0

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


#19 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 04 September 2012 - 05:10 AM

As CCR stated, you can't do anon types without them. I stay away from them whenever possible due to readability, etc. Nothing is more frustrating than seeing code you wrote years ago, and having to try to figure out what you were doing.
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#20 lobo521

lobo521

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 12 September 2012 - 01:08 PM

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?

If You know C#, You know that it's int.
  • 0

#21 lobo521

lobo521

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 12 September 2012 - 01:37 PM

Suppose you have method that returns List<YourType>. You use that method in all your application (like in 200 places). And you have to change the return type to IList<YourType>. If you use var, you don't have to do anything in most of places. When you use List<YourType> as variable type, you have to change all of them to IList<YourType>.

And about dynamic. If you use it, you loose all benefits of compilation type checking. I wouldn't use it unless i have good unit tests.
  • 0

#22 AceInfinity

AceInfinity

    CC Addict

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

Posted 12 September 2012 - 04:54 PM

And about dynamic. If you use it, you loose all benefits of compilation type checking. I wouldn't use it unless i have good unit tests.


No you don't
Posted Image

What does this intellisense tell you? :)

(IEnumerable<Int64[]> Or IEnumerable<long>)
  • 0

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


#23 lobo521

lobo521

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 12 September 2012 - 10:46 PM

You misunderstood me. By dynamic i mean "dynamic" c# keyword. Which is is totally different than "var" keyword. "var" is Just example of type inference. Type inference is also used to determine generic type parameters of generic methods. And all of you are using it in linq.
  • 0

#24 AceInfinity

AceInfinity

    CC Addict

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

Posted 13 September 2012 - 08:53 PM

You misunderstood me. By dynamic i mean "dynamic" c# keyword. Which is is totally different than "var" keyword. "var" is Just example of type inference. Type inference is also used to determine generic type parameters of generic methods. And all of you are using it in linq.


Not all of us, I only posted an example, I use it with anonymous types as well, but it's easier to post a line of code that others can test in their IDE instead of some bit of code that may require extra code for the person reading my post to put into their code before they can test. It's useful with LINQ as well though, who want's to type IEnumerable<something.....> all the time?

I know what dynamic and var are, you don't have to explain them to me, I just though "dynamic" in your context was the literal version of that word in the dictionary. In terms of code readability within the coder's IDE.

~Ace
  • 0

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






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