Archive

Archive for the ‘General’ Category

C# cloning objects and lists

March 26th, 2010 gerbrand No comments

Okay I had a situation where I needed to make a copy of my generic list (as a temporary backup). What I did was in my class I added a method ‘Clone’.

public object Clone()
{
var ms = new MemoryStream();
var bf = new BinaryFormatter();

bf.Serialize(ms, this);
ms.Position = 0;

var obj = bf.Deserialize(ms);
ms.Close();

return obj;
}

With this I could clone each object in my generic list by doing it in a foreach loop.

var oldData = new List<myObject>();

foreach(var data in myObjectList){

oldData.Add((myObject)data.Clone();

}

This is going “fast” with a small list of objects, but once my list had around 1000 items, it didn’t go fast enough. So after googling the web I found a better solutions. Why not cloning the whole list at once.
To do this I need to let my class inherit from ICloneable interface. Once this is done, you can do the following instead of using the foreach.

var oldData = new List<myObject>();

oldData = (List<myObject>) myObjectList.Clone();

This line of code had a elapsed time around 12seconds instead of 5-6minutes with the foreach loop. Which is away better performance.
Now my program has a smoother way of working. Hope that y’all can also benefit from this.

Greetzz and until next post …

Categories: General Tags:

On holiday

September 1st, 2009 gerbrand No comments

Finally some well deserved rest… Going to the Dominican Republic for 2 weeks… resting, diving, swimming, …

Just me and my wife…

We are staying at the region of Bayahibe. Not far of a national park.

I’ll upload some pictures later on this week…

Greetzz and untill next post

Categories: General Tags:

My First Weblog

April 13th, 2009 gerbrand No comments

Well I never thought that I would start with a Weblog, but since almost everybody is doing it, I was wondering how it would be to add from time to time some information on the web. So I started to think on what kind of subjects I could write. Because why have a weblog if you don’t know what to write about.

So these are some of the subject I will talk about or share with you: Programming c#, jigsaw puzzles (my collection), my Lego collection, djing, vacation talks, etc …

In the programming part I will add from time to time sample code on issues I had with some solutions. Also I’m going to talk about the Telerik controls here, my own experience with the controls, the pro’s and con’s.

Well I hope you will appreciate this weblog and all the posts I’ll be adding.

Greetzz and until the next post.

Categories: General Tags: