String Object with fixed length C# : <p>I have a class wherein I want to use Strings with a fixed size.
The reason for the fixed size is that the class "serializes" into a textfile
with values with a fixed length. I want to avoid to write foreach value a guard clause and instead have the class handle this.</p>

<p>So I have round about 30 properties which would look like this</p>

<pre><code>    public String CompanyNumber
    {
        get 
        {
            return m_CompanyNumber.PadLeft(5, ' ');
        }
        set 
        {
            if (value.Length &gt; 5) 
            {
                throw new StringToLongException("The CompanyNumber may only have 5 characters", "CompanyNumber");
            }
            m_CompanyNumber = value; 
        }
    } 
</code></pre>

<p>I would like to have a String that handles this by itself. Currently I have the following:</p>

<pre><code>public class FixedString
{
    String m_FixedString;

    public FixedString(String value)
    {
        if (value.Length &gt; 5) 
        {
            throw new StringToLongException("The FixedString value may consist of 5 characters", "value");
        }
        m_FixedString= value;
    }

    public static implicit operator FixedString(String value)
    {
        FixedString fsv = new FixedString(value);
        return fsv;
    }

    public override string ToString()
    {
         return m_FixedString.PadLeft(5,' ');
    }
}
</code></pre>

<p>The problem I have with this solution is that I can't set the String length at "compile time".</p>

<p>It would be ideal if it would look something like this in the end</p>

<pre><code>public FixedString&lt;5&gt; CompanyNumber { get; set; }
</code></pre>
