ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is the dis advantage in using the var type variable?

user-image
تم إضافة السؤال من قبل Yameen Mohammed Khan , IT Manager , Al Faisal Medical Center
تاريخ النشر: 2013/10/30
Daanish Rumani
من قبل Daanish Rumani , Product Manager , Publicis Sapient

In my opinion using var forces me to use good names for the variables and ultimately lead to more readable code. I always use var for temporary variables. On the other hand I strive to keep temporary variables to minimum in accordance with good coding practices.

 

The obvious disadvantage is that you cannot write bad code when using var or the reader would invariably be confused.

Azmat Jadoon
من قبل Azmat Jadoon , Software Engineer , NADRA

It reduces the readability of the code, when someone else is reading your code especially when some person is new to an existing project.

Noor Khan
من قبل Noor Khan , Technical Team Leader , Insightz360

There has been a lot of dispute over the use of var. My general rules are the following.

  • When the type is obvious such as when the right hand of the assignment is a constructor, use var.
  • When the type is complex to write, such as a LINQ query (the reason for var in the first place) use var.
  • For ambivalent types (your Decimal being an example) where you want to make sure that your variable is correctly typed, spell it out.
  • Anonymous types have to use var.
  • In all other cases spell out the type.

Basically, the goal is to make it easier to read the code. If you feel that var suffices because the assignment is obvious, use var. Use the full type name as a hint for the reader when you feel it's necessary.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟