Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:43:48+00:00 2026-06-09T17:43:48+00:00

I want to create a Rectangle struct which has byte s for x, y,

  • 0

I want to create a Rectangle struct which has bytes for x, y, width and height fields instead of ints. It should be compatible with the usual Microsoft.Xna.Framework.Rectangle (casting as (bytesRectangle) should be ok). How do I do that?

Upd: using this struct helped me save 12 bytes per tile.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:43:49+00:00Added an answer on June 9, 2026 at 5:43 pm

    I’m interested to know the design choice to implement this struct. Looking at Microsoft.Xna.Framework.Rectangle we see there are more than 30 properties and members. This is a lot to reproduce to save a few bytes. If your only goal is to save some bytes there might be a better way. For example if you want to save the data in a smaller space you could define an object and a cast to store it. Something like this:

    class SmallRectangle
    {
        public byte x { get; set; }
        public byte y { get; set; }
    
        public SmallRectangle(byte inx, byte iny)
        {
           x = inx;
           y = iny;
        }
    
        public static explicit operator SmallRectangle(Microsoft.Xna.Framework.Rectangle big)
        {
            SmallRectangle small = new SmallRectangle((byte)big.x, (byte)big.y);
    
            return small;
        }
    
        public WriteToFile(FileStream out)
        {
           //....
        }
    }
    

    Of course a complete implementation would include testing to make sure there is no data loss on the casting etc.

    Based on your comment below: This is not the direction to go. If you really need to save memory over XNA while using XNA, then you should make an Small rectangle manager class that does not store them the same way with the same attributes. But this class is good at creating the XNA ones when you need them. Don’t make the small rectangle class (or the manager) “compatable”.

    The BEST way to store the data you need in memory is to just store the four BYTES with no overhead. The best way to store two bytes with no overhead is with the Tuple class.

    So I suggest you make a rectangle manager class that internally stores the rectangles as Tuples and then “boxes” and “unboxes” them as needed into Xna objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With JavaFX, I want to create some post-it which are composed by a rectangle
I want create wordpress website into which I want create user management... That means
I want create module which update list of usb devices automatically (not only mass
I want to create a blue rectangle image and see it in my view,
I want to create a UIImageView object and initialize it with a rectangle image
I want to create a rectangle glass and a menu exact like that is
I want to create layout out with rounded rectangle border with gradient filling color
I want to create 4 rectangles stacked to one row. |----|--|------|------------| The width of
I want to create a bitmap from a byte array of pixel in metro
I want to create a class library which is able to draw pie or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.