[Mono-list] new color.cs for inclusion in CVS

Brian Crowell brian@fluggo.com
Wed, 6 Mar 2002 18:06:39 -0600


> The above may be really slow since it could require a box/unbox
> operation to the struct when using the new operator (i.e. remember that
> we are not using classes).  Although, I find that I am sometimes wrong
> about this stuff so maybe someone should check my facts. ;-)

I don't think so... The C# new operator does not return pointers like the
C++ new operator, any more than the C# "this" operator in a structure
returns a pointer to the structure. You need to stop thinking C++  :P

According to the C# spec, when new creates a value type, it allocates a
temporary variable and calls a constructor on it. No boxing or unboxing or
references are specified at all; new just creates a new instance of the
type.

So, the sequence:

> 		public static Color FromArgb (int a,int r, int g, int b)
> 		{
>			Color color;
>			color.a = a;
>			color.r = r;
>			color.g = g;
>			color.b = b;
>			return	color;
> 		}

...could actually be slower than:

> 		public static Color FromArgb (int a,int r, int g, int b)
> 		{
> 			return new Color (a,r,g,b);
> 		}

...since the first one assigns the structure's value twice: once in the
implicit call to Color's default constructor, and the second explicitly.

--Brian