alternative to structs?
-
What can I use instead of struct to store little info? I want create instances, put them in a dictionary, iterate and modify elements in the dictionary. I know every addition will involve a boxing operation, and every modification will involve an unboxing followed by a boxing operation. What is the best thing to do?
-
What can I use instead of struct to store little info? I want create instances, put them in a dictionary, iterate and modify elements in the dictionary. I know every addition will involve a boxing operation, and every modification will involve an unboxing followed by a boxing operation. What is the best thing to do?
Use .NET 2.0 with generics to allieviate the boxing involved. If you don't have .NET 2.0 ask yourself just how many elements you will be boxing an unboxing. In business apps the difference between 1ms and 3ms is very small. If you are modifying extremely large sets maybe C# is the wrong language. Also, consider your dictionary as a source of problems. If you are iterating a dictionary may be the wrong structure since lists are efficient in this respect.
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
-
Use .NET 2.0 with generics to allieviate the boxing involved. If you don't have .NET 2.0 ask yourself just how many elements you will be boxing an unboxing. In business apps the difference between 1ms and 3ms is very small. If you are modifying extremely large sets maybe C# is the wrong language. Also, consider your dictionary as a source of problems. If you are iterating a dictionary may be the wrong structure since lists are efficient in this respect.
A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane
Could you please explain what you mean by this - "Use .NET 2.0 with generics". What generics are you talking about?
-
What can I use instead of struct to store little info? I want create instances, put them in a dictionary, iterate and modify elements in the dictionary. I know every addition will involve a boxing operation, and every modification will involve an unboxing followed by a boxing operation. What is the best thing to do?
Instead of defining a struct you should define a class.
public class MySpecialData
{
private bool isDataDirty;
public bool IsDataDirty { get{ return isDataDirty; } }
private string someValue;
public string SomeValue
{
get { return someValue; }
set { someValue = value; }
}
public int GetHashcode(){}
}Now it was a known issue that collections of data (even strong typed collections) bring in boxing and downcasting/upcasting issues. Visual Studio 2005 and the 2.0 .NET Framework solve that by using what is called Generics. This moves your collection definition from runtime to compile time. As such your collection is now fully strong typed and no boxing occurs.
public class MySpecialDataCollection : Dictionary< int, MySpecialData> {}
The statement above uses generics....and that is all you need define for a fully functional collection. When you compile your program, the dictionary expects a key of type int and data of type MySpecialData. So at execute time, you do not have any boxing or downcasting occuring at all!!! Then to use it, it is as any other dictionary:
MySpecialDataCollection list = new MySpecialDataCollection(); foreach (Twizzle element in MyTwizzleCollection ) { list.Add( element.SpecialObject.GetHashcode(), element.SpecialObject); }