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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:40:39+00:00 2026-05-26T20:40:39+00:00

As .NET doesn’t use C style null s to end a string how can

  • 0

As .NET doesn’t use C style nulls to end a string how can I keep the allocated string but change the length of it by using unsafe code?

As I understand .NET using a 20 bytes header for every string, presumably this is where the length of the string is stored, is there anyway to directly modify this length? So .NET will keep the string in memory but when I call .Length it’ll return the .Length I want.

if this is possible, also it would be interesting to hear all crazy possible side-effects of this

UPDATE

I’m trying accomplish this without using reflection.

  • 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-26T20:40:39+00:00Added an answer on May 26, 2026 at 8:40 pm

    From Strings UNDOCUMENTED

    public static unsafe void SetLength(string s, int length)
    {
        fixed(char *p = s)
        {
            int *pi = (int *)p;
            if (length<0 || length > pi[-2])
                throw( new ArgumentOutOfRangeException("length") );
            pi[-1] = length;
            p[length] = '\0';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

VB.NET doesn't have out parameters, but you can specify <Out()> ByRef on COM and
I have seen code like this: function($cfg) use ($connections) {} but php.net doesn't seem
SqlServerCe doesn't seem be supported in .net 4.0. But since I needed an embedded
In .Net (VB more specifically, but that doesn't really matter), is there a way
I'm aware that .NET doesn't use complete physical memory availabe. I've encountered a System.OutOfMemoryException
I'm using SIM. I perform a transaction, but Authorize.net doesn't return all the transaction
I got a Utility module since VB.NET doesn't have static class like C# and
To my endless disbelief I discovered the .NET stack doesn't have the answer for
I'd like to setup a WCF service to send emails. The System.Net.MailMessage doesn't seem
ASP.Net 3.5 running under IIS 7 doesn't seem to allow this out of the

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.