| Author |
out parameter in ctor of struct vs. class ???
|
|
| test test 2006-01-17, 8:01 am |
| someone please explain why the following compiles:
struct MyStruct
{
public MyStruct(out Object anything)
{
this=new MyStruct();
}
}
but the following fails due to not definitely assigning the out
parameter???
class MyClass
{
public MyClass(out Object anything)
{
}
}
| |
|
|
| test test 2006-01-17, 8:01 am |
| that was what I thought. But if you actually try the following there is
not a problem during compilation:
struct MyStruct
{
public MyStruct(out Object anything)
{
this=new MyStruct();
}
}
| |
| Josh Twist 2006-01-17, 8:01 am |
| It doesn't compile under .NET 2.0, maybe it was a small 'bug' in 1.x
that has been fixed? I haven't tried it under 1.x btw :)
Josh
http://www.thejoyofcode.com/
| |
| Bruce Wood 2006-01-18, 7:02 pm |
| Yes, it does compile under .NET 1.1, which is weird for two reasons.
First of all, nothing is assigned to the parameter "anything", so the
"out" is not honoured.
Second, you can't assign to "this", so the line
this = new MyStruct();
is nonsense. Nonetheless, it compiles.
I didn't check to see what code it generated, but IMHO it's a bug in
the compiler.
| |
| test test 2006-01-18, 10:00 pm |
| The first one is the question.
The second one is actually as intented. You can do it with "struct".
There was something in MSDN that explain that.
| |
| Bruce Wood 2006-01-19, 7:05 pm |
| > The second one is actually as intented. You can do it with "struct".
Oh, yes, of course. Silly me. It's a value assignment so it's valid.
The "out" thing is weird, though. Obviously a compiler bug.
|
|
|
|