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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:09:58+00:00 2026-05-11T15:09:58+00:00

In an answer to his own controversial question , Mash has illustrated that you

  • 0

In an answer to his own controversial question, Mash has illustrated that you don’t need the ‘unsafe’ keyword to read and write directly to the bytes of any .NET object instance. You can declare the following types:

   [StructLayout(LayoutKind.Explicit)]    struct MemoryAccess    {        [FieldOffset(0)]       public object Object;        [FieldOffset(0)]       public TopBytes Bytes;    }     class TopBytes    {       public byte b0;       public byte b1;       public byte b2;       public byte b3;       public byte b4;       public byte b5;       public byte b6;       public byte b7;       public byte b8;       public byte b9;       public byte b10;       public byte b11;       public byte b12;       public byte b13;       public byte b14;       public byte b15;    } 

And then you can do things like change an ‘immutable’ string. The following code prints ‘bar’ on my machine:

 string foo = 'foo';  MemoryAccess mem = new MemoryAccess();  mem.Object = foo;  mem.Bytes.b8 = (byte)'b';  mem.Bytes.b10 = (byte)'a';  mem.Bytes.b12 = (byte)'r';  Console.WriteLine(foo); 

You can also trigger an AccessViolationException by corrupting object references with the same technique.

Question: I thought that (in pure managed C# code) the unsafe keyword was necessary to do things like this. Why is it not necessary here? Does this mean that pure managed ‘safe’ code is not really safe at all?

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

    OK, that is nasty… the dangers of using a union. That may work, but isn’t a very good idea – I guess I’d compare it to reflection (where you can do most things). I’d be interested to see if this works in a constrained access environment – if so, it may represent a bigger problem…


    I’ve just tested it without the ‘Full Trust’ flag, and the runtime rejects it:

    Could not load type ‘MemoryAccess’ from assembly ‘ConsoleApplication4, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ because objects overlapped at offset 0 and the assembly must be verifiable.

    And to have this flag, you already need high trust – so you can already do more nasty things. Strings are a slightly different case, because they aren’t normal .NET objects – but there are other examples of ways to mutate them – the ‘union’ approach is an interesting one, though. For another hacky way (with enough trust):

    string orig = 'abc   ', copy = orig; typeof(string).GetMethod('AppendInPlace',     BindingFlags.NonPublic | BindingFlags.Instance,     null, new Type[] { typeof(string), typeof(int) }, null)     .Invoke(orig, new object[] { 'def', 3 }); Console.WriteLine(copy); // note we didn't touch 'copy', so we have                          // mutated the same reference 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

@swatkins gave a fantastic answer to his own question about using jQuery templates with
In his answer to this question , BlackBear suggested replacing string y = Session[key]
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
I've got a question that most likely doesn't have a unique and clear answer.
EDIT @mootinator has done awesome work with this. See his answer below. Since I
In my answer to my own question here I posted some code and @Dave
Problem solved klaustopher his answer fixed the problem. The problem is that i can't
Edit: Building off of aL3xa's answer below, I've modified his syntax below. Not perfect,
ANSWER: You cannot write an int directly to a file. Convert it to a
THE ANSWER THAT WORKED FOR ME BASED ON AN ANSWER GIVEN while($post = mysql_fetch_array($tags))

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.