Saturday 15 September 2012

c# - Convert instances of objects to be able to loop through -


I have something that contains strings, int, and doubles. I have three examples of the item: item 1, item 2, item 3

I can print the price by saying item 1.name and so on. But how can I get through the object and print the values? I do not think I can convert an object to a list or array because the object has different types - right?

itemprop = "text">

If you mean that they are all examples of the same class, then it is very basic that what you do in this situation can do. If this is the case, you can make a list of that category type. Then try the objects again in any way, and you will be able to print any public property included in the class.

  var example = new list & lt; ExampleObject & gt; () {New instance object {id = 1, name = "display"}, new instance object {id = 2, name = "display2"}}}; Examples.ForEach (x = & amp; Console.WriteLine ("id = {0}, name = {1}", x.id, x.Name));    

No comments:

Post a Comment