469,355 Members | 2,507 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,355 developers. It's quick & easy.

FYI: Using ArrayList.Synchronized on Derived Classes

I've seen a few messages on this, but no responses. Here's what I
found out

PROBLEM:
How do you use ArrayList.Synchronized to create thread-safe objects
derived from ArrayList

public class MyList : ArrayList
{
}

// This throws an exception
MyList list = (MyList)ArrayList.Synchronized(new MyList)
ANSWER:
You can't. ArrayList.Synchronized returns a new ArrayList object.
Sorry.

THE (not so great) SOLUTION:
Instead of deriving from ArrayList, Wrap it.

public class MyList : IList
{
private ArrayList inner;
public MyList()
{
inner = ArrayList.Synchronized(new ArrayList);
}

// Implement the IList members using the inner ArrayList
public object this[int index]
{
get
{
return inner[index];
}
set
{
inner[index] = value;
}
}

You get the rest...
If there is a better solution, post it!
Nov 15 '05 #1
0 1029

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Iulian Ionescu | last post: by
10 posts views Thread by Eric | last post: by
3 posts views Thread by Igor | last post: by
1 post views Thread by KK | last post: by
28 posts views Thread by wanwan | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.