Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #70 – Data Collection Support in Silverlight

Silverlight provides a number of options for data collection, storage and retrieval. In this Tip I will be walking you through various ways of doing this. You will need to add a using statement in your page to reference System.Collections;

BitArray. The BitArray is a class that you can instantiate that manages a collection of bit values which are represented as either true (1 = bit is on) or false (0 =bit is off). The following example below is an implementation of BitArray. Note that there are many different constructor options for the BitArray, this is just one.

BitArray bitArray = new BitArray(4);
 
// Add
bitArray[0] = true;
bitArray[1] = false;
bitArray[2] = false;
bitArray[3] = true;
 
// Retreive
if (true == bitArray[1])
{
    //...
}

List. The List collection allows you to store any type of data that can than be accessed by a index value. In the case below we use a string but you can replace string with any type of object you want.

List<string> test = new List<string>();
 
// Add data
test.Add("Hello");
test.Add("There");
test.Add("Goodbye");
 
// Retrieve "Goodbye"
string data = test[2];

Stack. A stack is always last in first out.  That is, the data that was inserted on the stack via a call to Push() will be the first retrieved when the call to Pop() is made. Like the List collection above, you can store any type of object in it. For our example below we are using an integer.

Stack<int> stack = new Stack<int>();
 
stack.Push(5);
stack.Push(3);
stack.Push(4);
 
// Pop the 4 off the stack.
int test = stack.Pop();

Queue. A queue is first in first out. Objects are added via the Enqueue() call and retrieved and removed via the Dequeue() call. In the example below, we enqueue three strings. Then, a call to Dequeue() removes the first string insert which was “First”. If we were to make another call to Dequeue() we would retrieve the string “Second”.

// Add data
Queue<string> queue = new Queue<string>();
queue.Enqueue("First");
queue.Enqueue("Second");
queue.Enqueue("Third");
 
// Retrieve the string "First"
string test = queue.Dequeue();

LinkedList. A LinkedList is a linked list of nodes. You have direct access to the head and tail of the list and can enumerate through the list from beginning to end. . In the example below we declare a class called Node that we store in the linked list.

class Node
{
    private int _ID;
 
    public Node(int ID)
    {
        _ID = ID;
    }
 
    public int ID
    {
        get { return _ID; }
        set { ID = value; }
    }
 
};
 
public partial class Page : UserControl
{
    public Page()
    {
        InitializeComponent();
 
 
        // Add Nodes
        LinkedList<Node> _list = new LinkedList<Node>();            
        LinkedListNode<Node> headNode = _list.AddFirst(new Node(432));
        LinkedListNode<Node> lastNode = _list.AddAfter(headNode, new Node(200));
        lastNode = _list.AddAfter(lastNode, new Node(451));
        lastNode = _list.AddAfter(lastNode, new Node(14));
        lastNode = _list.AddAfter(lastNode, new Node(55));
 
        // Retrieve Nodes
        foreach (Node node in _list)
        {
            int ID = node.ID;
        }
        
    }
}

Dictionary. Dictionaries store data via a key / value pair where the key is used to to identify and retrieve the item being stored and the value is the actual data being stored.

Dictionary<int, string> myData = new Dictionary<int, string>();
 
// Store Data
myData.Add(4324, "Cat");
myData.Add(331, "Dog");
myData.Add(442, "Lion");
myData.Add(3444, "Tiger");
 
// Retrieve "Lion" by passing the key 3444
string animal = myData[442];

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Silverlight provides a number of options for data collection, storage and retrieval. In this Tip I will

# November 14, 2008 3:58 PM

spo81rty said:

This is kinda weird stuff!

# November 14, 2008 10:04 PM

Mike Snow Tip of the Day #70 - Data Collection Support in Silverlight said:

Pingback from  Mike Snow Tip of the Day #70 - Data Collection Support in Silverlight

# November 15, 2008 4:55 AM

mike.snow said:

What's weird about it? :)

# November 15, 2008 8:37 PM

Dew Drop - Weekend Edition - November 15-16, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - Weekend Edition - November 15-16, 2008 | Alvin Ashcraft's Morning Dew

# November 15, 2008 9:42 PM

Swedish WRC rally 2005, Toyota Corolla said:

Pingback from  Swedish WRC rally 2005, Toyota Corolla

# November 15, 2008 11:09 PM

Community Blogs said:

In this issue: Boyan Nikolov, Ning Zhang, Jeff Paries, Chris Hay, Jeff Handley, and Mike Snow. Terence

# November 16, 2008 1:04 AM

syntax42 said:

I thank you for including these colletions in silverlight, but there is one i really miss: Priority Queue. Will it come later? :)

# November 16, 2008 5:36 AM

unruledboy2 said:

is there a "Collection" that combines Dictionary<> and List<>?

I mean, you could specify key(string), and you could also use for/next to iterate, for example:

FooCollection c = new FooCollection<SomeObject>();

c.Add("key1", SomeObject1);

c.Add("key2", SomeObject2);

c.Add("key3", SomeObject2);

for(int i; i< c.Count; i++)

{

 Console.WriteLine(cIdea.SomeProperty);

}

Console.WriteLine(c["key2"].SomeProperty)

# November 16, 2008 9:43 PM

2008 November 17 - Links for today « My (almost) Daily Links said:

Pingback from  2008 November 17 - Links for today &laquo; My (almost) Daily Links

# November 17, 2008 3:52 AM

mike.snow said:

unruledboy2 - Sorry, not that I am aware of.

# November 17, 2008 12:51 PM

VixMVP said:

Mike, This is cool !! ,but would like to know practical scenarios where one can implement this, is it good to use such collection for large data?

# November 18, 2008 1:29 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

Visual Web Developer Team Blog said:

This link provides a complete Tips of the Day Summary Outline - silverlight.net/.../silverlight-tips-of-the-day-summary-outline.aspx

# January 8, 2009 6:37 PM

cleanpro said:

looking for a objectcollection view for a datagrid query.

# August 16, 2011 1:15 PM

Silverlight collections | My2shoppe said:

Pingback from  Silverlight collections | My2shoppe

# September 29, 2011 7:24 AM