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

  • SEARCH
  • Home
  • 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 71659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:55:21+00:00 2026-05-10T19:55:21+00:00

this is a question that when programming I always wonder: What to use when

  • 0

this is a question that when programming I always wonder: What to use when we are writing code:

var myFiles = Directory.GetFiles(fullPath); 

or

string[] myFiles = Directory.GetFiles(fullPath); 

var is new and is a Implicitly Typed Local Variables, so we can only use locally and it has rules like can’t be null, etc., but I wonder if we get any advantage of using it ‘normally’.

The ‘normally’ part says, not in Anonymous Types, Object and Collection Initializers and Query Expressions where that was the intent to use the var anonymous object, so what I mean is… just like the example above.

what are your thoughts?

  • 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-10T19:55:22+00:00Added an answer on May 10, 2026 at 7:55 pm

    Beyond the obvious use of var with LINQ, I also use it to abbreviate hairy variable declarations for readability, e.g.:

    var d = new Dictionary<string, Dictionary<string, Queue<SomeClass>>>(); 

    In general, I get a kind of comfort (for want of a better word) from static typing that makes me reluctant to give it up. I like the feeling that I know what I’m doing when I’m declaring a variable. Declaring a variable isn’t just telling the compiler something, it’s telling the person reading your code something.

    Let me give you an example. Suppose I have a method that returns a List<string>. This code is certainly correct, and I think it’s how 90% of C# developers would probably write it:

    List<string> list = MyMethod(); 

    Obviously, right? In fact, here’s a place you could just as easily use var.

    True enough. But this version of the code isn’t just declaring a variable, it’s telling me what the person who wrote it is intending to do:

    IEnumerable<string> list = MyMethod(); 

    The developer who wrote that code is telling me ‘I’m not going to be changing this list, nor am I going to use an index to access its members. All I’m going to do is iterate across it.’ That’s a lot of information to get across in a single line of code. It’s something you give up if you use var.

    Of course, you’re not giving it up if you weren’t using it in the first place. If you’re the kind of developer who would write that line of code, you already know that you wouldn’t use var there.

    Edit:

    I just reread Jon Skeet’s post, and this quote from Eric Lippert jumped out at me:

    Implicitly typed locals are just one small way in which you can deemphasize the how and thereby emphasize the what.

    I think that actually in a lot of cases using implicit typing is leaving the what implicit. It’s just OK to not dwell on the what. For instance, I’ll casually write a LINQ query like:

    var rows = from DataRow r in parentRow.GetChildRows(myRelation)            where r.Field<bool>('Flag')            orderby r.Field<int>('SortKey')            select r; 

    When I read that code, one of the things I think when I’m reading it is ‘rows is an IEnumerable<DataRow>.’ Because I know that what LINQ queries return is IEnumerable<T>, and I can see the type of the object being selected right there.

    That’s a case where the what hasn’t been made explicit. It’s been left for me to infer.

    Now, in about 90% of the cases where I use LINQ, this doesn’t matter one tiny little bit. Because 90% of the time, the next line of code is:

    foreach (DataRow r in rows) 

    But it’s not hard to envision code in which it would be very useful to declare rows as IEnumerable<DataRow> – code where a lot of different kinds of objects were being queried, it wasn’t feasible to put the query declaration next to the iteration, and it would be useful to be able inspect rows with IntelliSense. And that’s a what thing, not a how thing.

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

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You may want to check out how S#arp Architecture has… May 11, 2026 at 6:10 pm
  • Editorial Team
    Editorial Team added an answer Something like this should work. public static class ApplicationState {… May 11, 2026 at 6:10 pm
  • Editorial Team
    Editorial Team added an answer Instead of myFunc() use myFunc. In javascript, a function with… May 11, 2026 at 6:10 pm

Related Questions

I use Delphi, but this is a question that I think is valid for
I've always been one to err on the side of preventing exception conditions by
I'm teaching/helping a student to program. I remember the following process always helped me
In the past I've worked with -Wall and other switches for gcc to eliminate

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.