Handling Excel column letter index as struct
Why is that the case?


You need to know what the string + operator does under the covers - it calls String.Concat.

So when you do this:

var someString = aString + anObject;


We get this:

var someString = string.Concat(aString, anObject);


Let's look at the IL for your example:

C#

void Main()
{
    int rowIndex = 1;
    ExcelColumn column = new ExcelColumn(1);
    string cellId = column + rowIndex.ToString(); 
}


IL

IL_0000:  nop         
IL_0001:  ldc.i4.1    
IL_0002:  stloc.0     // rowIndex
IL_0003:  ldloca.s    01 // column
IL_0005:  ldc.i4.1    
IL_0006:  call        UserQuery+ExcelColumn..ctor
IL_000B:  ldloc.1     // column
IL_000C:  box         UserQuery.ExcelColumn
IL_0011:  ldloca.s    00 // rowIndex
IL_0013:  call        System.Int32.ToString
IL_0018:  call        System.String.Concat
IL_001D:  stloc.2     // cellId
IL_001E:  ret        


You can see that your ExcelColumn (column) is being boxed to an object and passed to String.Concat.

So your question is really: why does this work?

string cellId = string.Concat(column, rowIndex.ToString()); 


Answer: because column.ToString() is being called in string.Concat.

Hmmm... I hope that makes sense.



Time to review your code.

Get rid of the regions. They're almost always pointless and you certainly don't need a region around a single constant.

Constants are named PascalCase in C#. MAX_COLUMN_COUNT should be MaxColumnCount.

I think it would be better to store the string representation at creation rather than computing it each time it's needed.

Structs always have a default constructor which means you have a bug.

new ExcelColumn().ToString() // @