Question

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...?

Was it helpful?

Solution

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

OTHER TIPS

You should preferably use ManagementBaseObject here. The Get() method returns a (non-generic) ManagementObjectCollection which contains ManagementBaseObject-derived types, including ManagementObject and ManagementClass.

The first version may work for your particular query, but in general you might get an invalid cast exception.

If you only need to consider objects of type ManagementObject you might consider the following version:

var query = new SelectQuery("Win32_Environment");
var searcher = new ManagementObjectSearcher(query);
foreach (var envVar in searcher.Get().OfType<ManagementObject>())
{
    Console.WriteLine("Variable: {0}, Value = {1}", 
        envVar["Name"], envVar["VariableValue"]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top