Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 182501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:57:02+00:00 2026-05-11T14:57:02+00:00

Duplicate Use of var keyword in C# C# – Do you use var? Should

  • 0

Duplicate

Use of var keyword in C#
C# – Do you use ‘var’?
Should I always favour implictly typed local variables in C# 3.0?

Would you prefer to write your code like this:

var myVar = new MyClass(); 

or

MyClass myVar = new MyClass(); 

I don’t object to use the var keyword in example above; however, in the code below I don’t like the usage of var.

var files = Files.GetFiles(); 

In this example it’s not obvious what type variable files is going to be.

What’s your preference for using the var keyword?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:57:03+00:00Added an answer on May 11, 2026 at 2:57 pm

    I tend to use ‘var’ in places where I, reading the sourcecode, can infer the type of the variable without having to navigate to other places in the sourcecode.

    For example, any time I new up an object:

    var c = new Customer(); 

    Or when calling a method in which the return type is clear:

    var c = Customers.GetCustomerByID(5); 

    Also, ‘var’ must be used in the case of anonymous types:

    var t = new { Key = 'One', Value = 1 }; 

    And I also use it with LINQ of course:

    var q = from c in Customers         where c.CustomerID == 5         select c; 

    In the case above ‘q’ is of type IQueryable (assuming Customers is an enumeration of Customer objects).

    Also ‘foreach’ statements as well – GREAT please to use ‘var’:

    foreach (var i in intArray) { /* use i */ } 

    Other examples abound I’m sure – but these cover my most frequent use cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Use of var keyword in C# Which declaration should be accepted ?
Possible Duplicate: When should one use final? When should Java programmers prefer to use
Possible Duplicate: Use of var keyword in C# I don't really understand why you
Possible Duplicate: Use of var keyword in C# Being relatively new to C# I
Possible Duplicate: C# ‘var’ vs specific type performance I use the 'var' keyword extensively.
Possible Duplicate: Use of var keyword in C# Which one is better? using var
Possible Duplicate: Use of var keyword in C# Hi, I am pretty new in
Possible Duplicate: Use of var keyword in C# Hi, Just moved job and I
Possible Duplicate: Use of var keyword in C# What is the benefit of using
Possible Duplicate: Why use document.write? Considering the negative effects of document.write(), why are most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.