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 8098331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:02:57+00:00 2026-06-05T22:02:57+00:00

object[] objArray = new object[]{blah, 4, whatever}; foreach(var value in objArray) vs. foreach(object value

  • 0
object[] objArray = new object[]{"blah", 4, "whatever"};

foreach(var value in objArray) vs. foreach(object value in objArray)

I’m curious as to what the difference is between those, other than var must remain its type after assigned. Is one better than the other? Thanks.

  • 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. Editorial Team
    Editorial Team
    2026-06-05T22:02:59+00:00Added an answer on June 5, 2026 at 10:02 pm

    From a purely functional perspective, var is just a shortcut for object here, since objArray‘s elements are of declared type object.

    Using object is a sign to whoever’s reading the code that the items in the array are not known to be any type more specific than object. The use of var does not connote this. In the minimal case you posted, it really doesn’t make any difference to the clarity of the program which one you use.

    If, on the other hand, it is not immediately clear from the context what type of object you are working with, then it may be advantageous to explicitly declare the type. On the other hand, if the type of the elements of the collection you’re iterating over is verbose and obtrusive, then it may be advantageous to use var, so that the reader’s eyes will be drawn to the logic of the code rather than a mess of generic parameters.

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

Sidebar

Related Questions

I designed the following test: var arrayLength=5000; object[] objArray=new object[arrayLength]; for(var x=0;x<arrayLength;x++) { objArray[x]=new
object ReassignTest extends App { class X(var i : Int) def x = new
object o; Is there any difference between o.ToString() and (string) o ?
Object initialization: var myObject = {}; and var myObject = new Object(); why the
Object.prototype.e = function() { [].forEach.call(this, function(e) { return e; }); }; var w =
Is it possible to do something like this in Java? Object[] objArray = {
Object.preventExtensions and Object.seal prevent unknown properties from being added to an object, but those
object[] objs = new object[]{one,two,three}; Are the strings stored in the array as references
object nullobject = null; object myobject = new myobject(); console.writeline(nullobject=+nullobject+myobject=+myobject);
Assign object literal properties var foo = { bar : 'hello'}; Ternary var cats

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.