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

  • Home
  • SEARCH
  • 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 6940305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:44:06+00:00 2026-05-27T12:44:06+00:00

I have a System.Array of value struct types, something like this: public value struct

  • 0

I have a System.Array of value struct types, something like this:

public value struct Position
{
    int timestamp;
    float x;
    float y;
}

Position[] positions = new Position[1000 * 1000];

After I initialize the array with values, how can I get a byte[] of it’s content, without serializing one item at a time?

In C++/CLI I would use a pin_ptr to get the pointer to the array content and I would copy the data from there. Can I do something like this in C#?

EDIT: I need to write the raw data to disk, as if it were a C struct, without any kind of serialization.

I tagged this question as C# for broader exposure, but actually I’m trying to serialize the data from IronPython, so this means I can’t use any unsafe C# functionality.

  • 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-05-27T12:44:07+00:00Added an answer on May 27, 2026 at 12:44 pm

    Here’s a method that doesn’t require unsafe code:

    [Updated to remove the for loop and perform the copy in a single pass]

    private static byte[] StructureToByteArray(Position[] posArray)
    {
        if (posArray == null || posArray.Length == 0)
        {
            return new byte[0];
        }
    
        var lenOfObject = Marshal.SizeOf(typeof(Position));
        var len = lenOfObject * posArray.Length;
        var arr = new byte[len];
    
        var handle = GCHandle.Alloc(posArray, GCHandleType.Pinned);
        try
        {
            var ptr = handle.AddrOfPinnedObject();
            Marshal.Copy(ptr, arr, 0, len);
        }
        finally
        {
            handle.Free();
        }
    
        return arr;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somewhat academic question, but: How do value types like Int actually work ? I've
I have an array of strings. How can I convert it to System.Collections.ArrayList?
I have a situation where I could really benefit from having system like memcached,
import java.util.Random class Kostka { val rand = new Random(System.currentTimeMillis()) val value: List[Int] =
We have a system like gift cards balance usage in the checkout page.While customers
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
We have system here that uses Java JNI to call a function in a
I have System.Collections.Generic.Dictionary<A, B> dict where A and B are classes, and an instance
Let's say we have system A comprising a MySQL database, with several tables. After
I have a system that combines the best and worst of Java and PHP.

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.