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 6577675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:40:04+00:00 2026-05-25T15:40:04+00:00

I would like to store a struct into a MemoryMappedFile, but this struct contains

  • 0

I would like to store a struct into a MemoryMappedFile, but this struct contains strings (with a max size which I know) that are reference type and therefore rejected by the MemoryMappedViewAccessor.

Do you know a good fixed-length string struct (value type) implementation? I can hard code the length if that is necessary (as arrays are reference types).

For example, I could instantiate it as:

TenCharsString myString = new TenCharsString("1234567890");

Decorating with the MarshalAs attribute

<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> 

should work, but it does not.

  • 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-25T15:40:05+00:00Added an answer on May 25, 2026 at 3:40 pm

    Here’s a simple 10-character value type:

    struct TenChars
    {
        public char A;
        public char B;
        public char C;
        public char D;
        public char E;
        public char F;
        public char G;
        public char H;
        public char I;
        public char J;
    }
    

    If unsafe code is an option, this works as well:

    unsafe struct TenChars
    {
        public fixed char Chars[10];
    }
    

    Full code:

    struct TenChars1
    {
        public const int Capacity = 10;
    
        private char A;
        private char B;
        private char C;
        private char D;
        private char E;
        private char F;
        private char G;
        private char H;
        private char I;
        private char J;
    
        public TenChars1(string value)
        {
            if (value == null) throw new ArgumentNullException("value");
            if (value.Length > Capacity) throw new ArgumentException();
    
            A = (value.Length > 0) ? value[0] : '\0';
            B = (value.Length > 1) ? value[1] : '\0';
            C = (value.Length > 2) ? value[2] : '\0';
            D = (value.Length > 3) ? value[3] : '\0';
            E = (value.Length > 4) ? value[4] : '\0';
            F = (value.Length > 5) ? value[5] : '\0';
            G = (value.Length > 6) ? value[6] : '\0';
            H = (value.Length > 7) ? value[7] : '\0';
            I = (value.Length > 8) ? value[8] : '\0';
            J = (value.Length > 9) ? value[9] : '\0';
        }
    
        public override string ToString()
        {
            return new string(new char[] { A, B, C, D, E, F, G, H, I, J });
        }
    }
    
    unsafe struct TenChars2
    {
        public const int Capacity = 10;
    
        private fixed char buffer[Capacity];
    
        public TenChars2(string value)
        {
            if (value == null) throw new ArgumentNullException("value");
            if (value.Length > Capacity) throw new ArgumentException();
    
            fixed (char* ptr = this.buffer)
            fixed (char* chars = value)
            {
                for (int i = 0; i < value.Length; i++)
                {
                    *(ptr + i) = *(chars + i);
                }
            }
        }
    
        public override string ToString()
        {
            fixed (char* ptr = this.buffer)
            {
                return new string(ptr);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a twenty byte hex hash that I would like to store in
I have a single user java program that I would like to have store
I would like to store log4net config data in my application.config file. Based on
I would like to store an object FOO in a database. Lets say FOO
In my database, I would like to store a decimal score. A score can
I would like to sign a device, and I have 64 bits to store
I use a byte to store some flag like 10101010 , and I would
I would like to create a stored procedure in MySQL that took a list
I have a structure that has an array of pointers. I would like to
This may be the wrong approach, but as I dig deeper into developing my

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.