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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:40:03+00:00 2026-05-28T23:40:03+00:00

If one loops through an XmlNodeList like this foreach (XmlNode foo in xmlNodeList) {string

  • 0

If one loops through an XmlNodeList like this

foreach (XmlNode foo in xmlNodeList) {string baa = foo.Attributes["baa"].Value;}

everything works as expected – foo is clearly of type XmlNode and the VS.NET IDE shows methods and fields.

On the other hand

foreach (var foo in xmlNodeList) { string baa = foo.Attributes["baa"].Value; }

is not compiling because here foo is of type object. Type inference sort of works but infers object.

Apparently, the elements of XmlNodeList are not of one defined type, but assigning them to XmlNode instead of var does something implicitly (casting or unboxing).

First question: what’s the mechanism behind that?

Second (related) question: how to find the types one can use in this kind of loop? Does the VS.NET IDE help?

  • 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-05-28T23:40:04+00:00Added an answer on May 28, 2026 at 11:40 pm

    XmlNodeList implements only the non-generic IEnumerable interface, and not something like IEnumerable<XmlNode> with generics. This prevents strong typing of its elements until you cast appropriately, so the compiler has no choice but to map the implicit type declaration to object in your foreach.

    If you insist on using the var keyword, you can cast the elements of xmlNodeList like so:

    foreach (var foo in xmlNodeList.Cast<XmlNode>())
    {
        string baa = foo.Attributes["baa"].Value;
    }
    

    But that’s ugly, and requires more keystrokes anyway. You may as well just explicitly declare XmlNode foo, and let the foreach cast it for you on the fly.

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

Sidebar

Related Questions

I've got a method similar to this, which loops through one set of data
I have a for loop that loops over one array... for i=1:length(myArray) In this
I know at first glance (due to the title) this looks like one of
Is there any themes for emacs which looks like this one? http://www.lnbogen.com/VisualStudioNet2005Colors.aspx
I'm looking to implement a fairly basic jQuery slideshow. One that automatically loops through
I've got this code which basically loops through a set of folders and subfolders
i have this code below that loops through a data structure builds up a
I'm querying a mysql table which then loops through the results. One of the
Hit refresh several times and see sometimes I get null. This script loops through
I have a timer that loops through following code every second. this code reads

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.