Top  Previous  Next

What is partially translated > Pointers > PChar


The PChar class is used to simulate Delphi's PChar type or other character pointer types.


PChar can store a character array either a a string or as a StringBuilder and has a second member variable for the current index.


public class PChar : IPointer<char>


   private StringBuilder m_StringBuilder = null;

   private string m_String = null;

   private int FPosition = -1;



Whether the string or the StringBuilder is used depends on the construction and on the use of PChar. When PChar is constructed from a string, m_String becomes a reference of the original string. That means, there are minimal costs to represents the character array of a Delphi string.


   public PChar(string S, int Index = 0)


     m_String = S;

     FPosition = Index;



As long as there is read only access to the pointer nothing but the index variable is changed. But as soon as there is a writing access to PChar, m_String will be copied into m_StringBuilder and all further operations will be done on it. For example the the following code is executed if a character is assigned to the deferenced pointer:



   public void Assign(char c)


     if(m_StringBuilder == null && m_String != null)


        m_StringBuilder = new StringBuilder();




     m_StringBuilder[FPosition] = c;




But the real purpose of the modifications is the modification of the original string. Therefore the changes at PChar finally have to be copied back. For this there is the Synchronize method.


public void Synchronize(ref string s)



Delphi2C# automatically adds a call of this method at the and of the code block, where the pointer is defined.



The StringBuilder member is used also in cases that PChar is not constructed from a string:



    public PChar(int Size)

    public PChar(char c)

    public PChar(char[] arr, int Index = 0)






This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content