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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:56:38+00:00 2026-06-18T01:56:38+00:00

When i Compiled the following both codes it gives same result (I think so).

  • 0

When i Compiled the following both codes it gives same result (I think so).

//ManagementObject :
SelectQuery query = new SelectQuery("Win32_Environment");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject envVar in searcher.Get())
  Console.WriteLine("Variable : {0}, Value = {1}",envVar["Name"], envVar["VariableValue"]);

//ManagementBaseObject :
SelectQuery query = new SelectQuery("Win32_Environment");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementBaseObject envVar in searcher.Get())
  Console.WriteLine("Variable : {0}, Value = {1}",envVar["Name"], envVar["VariableValue"]);

what is the difference between both code execution…?

  • 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-18T01:56:39+00:00Added an answer on June 18, 2026 at 1:56 am

    In this particular case, there is no difference.

    The ManagementObjectSearcher.Get() method returns a ManagementObjectCollection which is a collection of ManagementBaseObject. This means that the collection can contain instances of type ManagementBaseObject or of any type that is descended from ManagementBaseObject.

    However, the ManagementBaseObject is designed as a base class, which means that in reality, it won’t be instantiated, but instead, it’s descended classes will be instantiated. Note that this is a just a convention, and it is not enforced by the language or framework.

    Additionally, since the only class in the framework that (directly) inherits the ManagementBaseObject is ManagementObject, the Get() effectively returns a collection of ManagementObject instances. Note that this is just the current situation, and nothing prevents the creation of additional ManagementBaseObject inheritors.

    So, with all mentioned caveats, that means, that if you use only properties defined in the base class (and not overriden) you could iterate either way, and the code will behave exactly the same. In your code you use only the indexer, which is indeed defined, and not overriden, in the ManagementBaseObject class.

    If you want an example of code that will fail for one loop, and work in the other, you could try any of the properties defined on ManagementObject, like, for example, Path:

    foreach (ManagementObject envVar in searcher.Get())
      Console.WriteLine("Path : {0}, Value = {1}",envVar.Path.Path); //works
    
    foreach (ManagementBaseObject envVar in searcher.Get())
      Console.WriteLine("Path : {0}, Value = {1}",envVar.Path.Path); //compile error
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very strange problem. The following code gives different results when compiled
the following code can be compiled correctly on both VC or gcc: char *str
I compiled the following program with gcc -fprofile-arcs -ftest-coverage test.c: int main() { int
I compiled the following code as a shared library using g++ -shared ... :
I successfully compiled the following program simple.cc: #include <gtkmm.h> int main (int argc, char
I have the following code compiled by gcc: #include <iostream> using namespace std; class
The following code when compiled give this error I cannot understand why this is
the following piece of C++ code compiled two years ago in a suse 10.1
The following program, compiled with g++ 4.6, yields the error request for member ‘y’
The following code is generating warning C6284 when compiled with /analyze on MSVC 2008

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.