none
Var implicit type or explicit type is better to work with for performance RRS feed

  • Question

  • i often declare explicit  typed variable like String s="test" or Person p= new Person() etc but i often some people use var instead like var x="test" or var p=new Person();

    which one is better in terms of performance and memory usage & why ? please guide me in detail. thanks



    Monday, April 8, 2019 8:10 PM

Answers

  • There is no difference in performance. Many analyzers such as internal to Visual Studio and ReSharper suggest using var. Personally I use var where it makes sense and use explicit declarations when viewing code it's not apparent what the data type is from a) naming a variable b) what is on the right side of the variable assignment.  

    Side note: Naming is very important e.g. never use one letter variable names with implicit or explicit operations even in for-next, always have meaningful names.



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 8, 2019 8:21 PM
    Moderator
  • It makes no difference if var is used to implicitly define a local variable at the method level, which is done by the compiler and strong typed,  or the local variable is explicitly defined and strong typed at the local scope at the method level in concerning speed of execution.

    I use var exclusively at the method level as a convenience much like I use auto property over the traditional property get/set with using a private backing variable for a property, which now the compiler  takes care of doing this by using auto property that is a welcomed convenience too.

    The speed comes in not having to type it all in is where the speed comes. :)

    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:24 PM
    Monday, April 8, 2019 9:26 PM
  • Hi Studip_inn,

    Thank you for posting here.

    The link below gives a good explanation for performance.

    https://stackoverflow.com/questions/356846/will-using-var-affect-performance

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:24 PM
    Tuesday, April 9, 2019 6:42 AM
    Moderator

All replies

  • There is no difference in performance. Many analyzers such as internal to Visual Studio and ReSharper suggest using var. Personally I use var where it makes sense and use explicit declarations when viewing code it's not apparent what the data type is from a) naming a variable b) what is on the right side of the variable assignment.  

    Side note: Naming is very important e.g. never use one letter variable names with implicit or explicit operations even in for-next, always have meaningful names.



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 8, 2019 8:21 PM
    Moderator
  • It makes no difference if var is used to implicitly define a local variable at the method level, which is done by the compiler and strong typed,  or the local variable is explicitly defined and strong typed at the local scope at the method level in concerning speed of execution.

    I use var exclusively at the method level as a convenience much like I use auto property over the traditional property get/set with using a private backing variable for a property, which now the compiler  takes care of doing this by using auto property that is a welcomed convenience too.

    The speed comes in not having to type it all in is where the speed comes. :)

    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:24 PM
    Monday, April 8, 2019 9:26 PM
  • Hi Studip_inn,

    Thank you for posting here.

    The link below gives a good explanation for performance.

    https://stackoverflow.com/questions/356846/will-using-var-affect-performance

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:24 PM
    Tuesday, April 9, 2019 6:42 AM
    Moderator
  • why  some one use var ? what is advantage of var instead of explicit type ?
    Tuesday, April 9, 2019 7:23 PM
  • why  some one use var ? what is advantage of var instead of explicit type ? please discuss Madam. thanks
    Tuesday, April 9, 2019 7:24 PM