Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. alternative to structs?

alternative to structs?

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    honeyman_can
    wrote on last edited by
    #1

    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?

    E L 2 Replies Last reply
    0
    • H honeyman_can

      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?

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        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

        H Offline
        H Offline
        honeyman_can
        wrote on last edited by
        #3

        Could you please explain what you mean by this - "Use .NET 2.0 with generics". What generics are you talking about?

        1 Reply Last reply
        0
        • H honeyman_can

          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?

          L Offline
          L Offline
          LongRange Shooter
          wrote on last edited by
          #4

          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);
          }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups