Tech Off Thread

5 posts

Video series C# Fundamentals: Declaring Variables and Assigning Values Duration - 05

Back to Forum: Tech Off
  • User profile image
    udRising

    Hi there,

    I started learning c# and found Bob Tabors c# Fundamental series which i think is great. props to bob and a very big thank you.

    Anyways i have a question regarding something he says in ep 5 around 11:00 and further.

    he explains what var's are and what they do. which is basically letting the pc figure out what kind of variable i write down on that line.

    My question is, if we were to write everything with var's would that eventually strain the pc and hog alot of memory? since it has to figure out what the variable is? as to writing string, or int etc etc?

    just curious,

     

    thanks!

  • User profile image
    evildictait​or

    @udRising: No. The C# var keyword is resolved to a type at compile time, so there is no difference between the runtime speed of 

    var i = 1;

    and 

    int i = 1;

    Note that putting var everywhere can adversely affect the readability of your code, but it will never affect the speed of your program.

  • User profile image
    udRising

    Ah. i see ! Thanks for the reply Smiley

  • User profile image
    spivonious

    There are some languages that do type checking at runtime (JavaScript and Python come to mind), and there is a small performance penalty, but today's computers are more than powerful enough to handle it.

  • User profile image
    cheong

    Note that in some case you may have to explicitly cast a "var" variable to some type or the runtime will fail to find out which method / operator to use.

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.