Over a million developers have joined DZone.

Collections of Objects in Windows Phone

DZone's Guide to

Collections of Objects in Windows Phone

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

A Collection is simply an object that knows how to manage references to multiple instances of other classes. It keeps track of other objects. There are different collections to choose from. Each of them with their own “powers”. Some are created to keep objects sorted, some are created to allow for easy retrieval. The use of collections is quite common in the .NET Framework Class Library.

For now I am just focusing on the list collection, because it is the most simple one. To have an example to look at, I created a little project with a button called “myButton” and a textBox called “myTextBox”. First I add a class that I can work with:


Visual Studio nicely adds my class in a new page to my project:

    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    namespace Collections
        public class Car

I want to add two properties to my class:

    public class Car
        public string Make { get; set; }
        public string Model { get; set; }

So, let’s go back to my MainPage.xaml.cs file and add three instances of the car class inside the click event handler:

    private void myButton_Click(object sender, RoutedEventArgs e)
        Car car1 = new Car();
        car1.Make = "Oldsmobile";
        car1.Model = "Cutles Supreme";
        Car car2 = new Car();
        car2.Make = "Geo";
        car2.Model = "Prizm";
        Car car3 = new Car();
        car3.Make = "Nissan";
        car3.Model = "Altima";

Each instance has different properties.

Now I want to add each of these instances to a new collection, a list. Once I have them in the list I want to loop through each item in the list.

I am going to inspect the properties of each instance of a car class, adding it to a string that I then display in the textblock on the phone’s user interface:

    List<Car> myList = new List<Car>();
    string myCars = "";
    foreach (Car car in myList)
        myCars += car.Make + "-" + car.Model + Environment.NewLine;
    myTextBlock.Text = myCars;

First I created a new instance of a new collection, a list collection. This new List<Car>(), which is a Generic List, can only work with my Car class. The List Object has a number of methods for managing the objects that it holds a reference to. The “Add” method allows me to add a reference to the list. The “foreach” statement works with collections. It iterates through my list and for each car in that list it displays the Make and the Model:



To be continued…

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.


Published at DZone with permission of Andrea Haubner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}