For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2006 > out parameter in ctor of struct vs. class ???









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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)
{
}
}

Josh Twist

2006-01-17, 8:01 am

If you use the out keyword you have to set it to something (even if its
just null).

struct MyStruct
{
public MyStruct(out Object anything)
{
anything = null;
}
}

http://msdn.microsoft.com/library/d...spec_10_5_1.asp

Josh
http://www.thejoyofcode.com/

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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com