ARTICLE AD BOX
I'm writing a code generator. I have made a simple class to do indentation:
internal class Indentation { public int Indent { get => indent; set { indent = value; indentStr = new string(' ', Size * Indent); } } int Size { get; set; } string indentStr; private int indent; public Indentation(int Size = 4) { this.Size = Size; indentStr = new string(' ', Size * Indent); } public override string ToString() { return indentStr; } static public Indentation operator --(Indentation indentation) { indentation.Indent--; return indentation; } static public Indentation operator ++(Indentation indentation) { indentation.Indent++; return indentation; } }When I'm using it I'm expecting both of the print lines below to print the same
int i = 4; Indentation indent = new(); Debug.WriteLine(($"{i}|{indent}|public class ... {{")); Debug.WriteLine(($"{i++}|{indent++}|public class ... {{"));Instead, I get:
4||public class ... { 4| |public class ... {The integer has, as expected printed before the post-increment, whereas the indent has printed after the increment.
Is there a way I can force the indent to print before incrementing? Do I have something wrong in the operator overloading (I couldn't find anything helpful and the Microsoft documentation is sparse)?
1,7772 gold badges11 silver badges17 bronze badges
1
