Default parameter to a struct
SPONSORED LINKS
I am currently moving from C++ to C# and there are a few thing that confuse me about structures. I read a lot but it still feel like I am missing a key paradigm. I understand that struct are really meant to by type in C# but still some things do not make sense to me yet.
I will ask my two questions via a simple example
I have a color class which can be accessed via the color (an int) or it’s component (A,R,G,B). Hence I created a union. Here is the class which illustrates my 2 problems:
struct CColor
{
public CColor(byte _A, byte _R, byte _G, byte _B)
{
A = _A;
R = _R;
G = _G;
B = _B;
}
[System.Runtime.InteropServices.FieldOffset(0)]
public int Color;
[System.Runtime.InteropServices.FieldOffset(0)]
public byte A;
[System.Runtime.InteropServices.FieldOffset(1)]
public byte R;
[System.Runtime.InteropServices.FieldOffset(2)]
public byte G;
[System.Runtime.InteropServices.FieldOffset(3)]
public byte B;
}
1st question : Is their any way I can get my alpha (A) initialized to 255? No parameterless constructor I can accept but are we really forced to do everything explicitly ?
Second thing, the nice constructor above does not compile :
Error 1 Field ‘CColor.Color’ must be fully assigned before control is returned to the caller.
It is fully initialized…
Read more:
Default parameter to a struct