Jump to content
  • Advertisement
Sign in to follow this  
Tape_Worm

[.net] Boxing?

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I were to do the following:
private struct Vertex
{
   public float x;
   public float y;

   public Vertex(float vx,vy)
   {
      x = vx;
      y = vy;
   }
}

private Vertex[] vertices;
private Array anArray;
private Vertex[] verticesAgain;

public MyClass()
{
   vertices = new Vertex[1000];
   // Fill in vertices with data ...
   // ...
   // still filling
   // ....
   // And so forth.

   anArray = vertices;
   verticesAgain = (Vertex [])anArray;
}
This code compiles, runs fine and does exactly what I expect it to. What I'd like to know is would last line cause a boxing/unboxing issue? I'm guessing that it will, but I can't remember for sure if static arrays are a considered a ValueType/Object/Or something else. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
System.Array derives from System.Object and is thus not a value type, and all arrays (eg int[]) derive from System.Array. There won't be any boxing occuring with what you've shown.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tape_Worm
This code compiles, runs fine and does exactly what I expect it to. What I'd like to know is would last line cause a boxing/unboxing issue? I'm guessing that it will, but I can't remember for sure if static arrays are a considered a ValueType/Object/Or something else.

Thanks.

Whether a field is static or not has no bearing on whether it is a reference type or not. Only the type itself determines whether it is a reference or value type.

Some examples:

int i = 50; // Value type.
object o = i; // Boxed into o.
int j = (int)o; // Unboxed from o into j.

string s = "Hi there!"; // Reference type.
o = s; // Implicit upcasting, not boxing.
string t = (string)s; // Explicit downcasting, not unboxing.

Share this post


Link to post
Share on other sites
I don't know if this will help, but here's what I can tell you about boxing with managed extensions for c++...

int value = 32;
String *word = "program";
//these don't work
Console::WriteLine("'value' is equal to {0}",value);
Console::WriteLine("This {0} is now running.",__box(word));
//these do work
Console::WriteLine("'value' is equal to {0}",__box(value));
Console::WriteLine("This {0} is now running.",word);

The lines that don't work will complain about 1)WriteLine not being able to convert anything other than a String into a displayable type, and 2)boxing only being available for value types and not reference types. I'm using managed c++ here since I haven't run across any boxing in what little c# programming I've done.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!