the advantage of "out" 
Author Message
 the advantage of "out"

Does anyone here talk about the advantages of "out"? Or
in which case, using "out" is the best solution?

IMHO I can do the same thing if I pass couple of
references to a method, change their values in the scope
and then in the caller, we still can get the changed
value.



Sat, 23 Oct 2004 11:48:20 GMT  
 the advantage of "out"
private void test1(  ref int someNumber1 )
{
    someNumber = 5;

Quote:
}

private void test2(  out int someNumber2 )
{
    someNumber = 5;

Quote:
}

private void button2_Click(object sender, System.EventArgs e)
{
    int numberOne;
    // Following give a compiler Error
    //    Use of unassigned local variable 'numberOne'
    test1( ref numberOne );

    int numberTwo;
    // There is no error, even though numberTwo is NOT initialized.
    test2( out numberTwo );

Quote:
}

Hope this helps,
Chris R.


Quote:
> Does anyone here talk about the advantages of "out"? Or
> in which case, using "out" is the best solution?

> IMHO I can do the same thing if I pass couple of
> references to a method, change their values in the scope
> and then in the caller, we still can get the changed
> value.



Sat, 23 Oct 2004 12:07:25 GMT  
 the advantage of "out"
I know the reference passed as "out" must be un-
initialized. But this does not mean we must design a new
keyword "out" to handle this case because passing an un-
initialized reference is not a must to a programming
language. Another word, many other programming languages
does not explicitly and particularly handle this case.

I think the reason why C# group designed this new keyword
is they must have some enough reasons to believe that
this keyword can do sth that any other keyword cannot do
or in some cases, using this keyword can beat others in
terms of easy-coding or the efficiency.

But, what are their reasons? Any idea? Thanks in advance.

Quote:
>-----Original Message-----
>private void test1(  ref int someNumber1 )
>{
>    someNumber = 5;
>}

>private void test2(  out int someNumber2 )
>{
>    someNumber = 5;
>}

>private void button2_Click(object sender,
System.EventArgs e)
>{
>    int numberOne;
>    // Following give a compiler Error
>    //    Use of unassigned local variable 'numberOne'
>    test1( ref numberOne );

>    int numberTwo;
>    // There is no error, even though numberTwo is NOT
initialized.
>    test2( out numberTwo );
>}

>Hope this helps,
>Chris R.



>> Does anyone here talk about the advantages of "out"? Or
>> in which case, using "out" is the best solution?

>> IMHO I can do the same thing if I pass couple of
>> references to a method, change their values in the
scope
>> and then in the caller, we still can get the changed
>> value.

>.



Sat, 23 Oct 2004 13:07:26 GMT  
 the advantage of "out"
Jason,

even if you can achieve the same with a REF parameter as with an OUT
parameter I think it is useful in a way to document properly what are the
return values for your function.So I see it mainly as a way to read and
understand the code more easily. And if you also look into IDL you see the
[out] keyword, so I would assume it comes from that background und supports
some automatic memory transportation as well.

Michael


Quote:
> I know the reference passed as "out" must be un-
> initialized. But this does not mean we must design a new
> keyword "out" to handle this case because passing an un-
> initialized reference is not a must to a programming
> language. Another word, many other programming languages
> does not explicitly and particularly handle this case.

> I think the reason why C# group designed this new keyword
> is they must have some enough reasons to believe that
> this keyword can do sth that any other keyword cannot do
> or in some cases, using this keyword can beat others in
> terms of easy-coding or the efficiency.

> But, what are their reasons? Any idea? Thanks in advance.

> >-----Original Message-----
> >private void test1(  ref int someNumber1 )
> >{
> >    someNumber = 5;
> >}

> >private void test2(  out int someNumber2 )
> >{
> >    someNumber = 5;
> >}

> >private void button2_Click(object sender,
> System.EventArgs e)
> >{
> >    int numberOne;
> >    // Following give a compiler Error
> >    //    Use of unassigned local variable 'numberOne'
> >    test1( ref numberOne );

> >    int numberTwo;
> >    // There is no error, even though numberTwo is NOT
> initialized.
> >    test2( out numberTwo );
> >}

> >Hope this helps,
> >Chris R.



> >> Does anyone here talk about the advantages of "out"? Or
> >> in which case, using "out" is the best solution?

> >> IMHO I can do the same thing if I pass couple of
> >> references to a method, change their values in the
> scope
> >> and then in the caller, we still can get the changed
> >> value.

> >.



Sat, 23 Oct 2004 14:25:20 GMT  
 the advantage of "out"

Quote:
> But, what are their reasons? Any idea? Thanks in advance.

My understanding is that using "out" allows the optimisation of marshalling
when making remote calls---either acorss a process boundary or a machine
one.

Cheers,

Dave


Quote:
> I know the reference passed as "out" must be un-
> initialized. But this does not mean we must design a new
> keyword "out" to handle this case because passing an un-
> initialized reference is not a must to a programming
> language. Another word, many other programming languages
> does not explicitly and particularly handle this case.

> I think the reason why C# group designed this new keyword
> is they must have some enough reasons to believe that
> this keyword can do sth that any other keyword cannot do
> or in some cases, using this keyword can beat others in
> terms of easy-coding or the efficiency.

> But, what are their reasons? Any idea? Thanks in advance.

> >-----Original Message-----
> >private void test1(  ref int someNumber1 )
> >{
> >    someNumber = 5;
> >}

> >private void test2(  out int someNumber2 )
> >{
> >    someNumber = 5;
> >}

> >private void button2_Click(object sender,
> System.EventArgs e)
> >{
> >    int numberOne;
> >    // Following give a compiler Error
> >    //    Use of unassigned local variable 'numberOne'
> >    test1( ref numberOne );

> >    int numberTwo;
> >    // There is no error, even though numberTwo is NOT
> initialized.
> >    test2( out numberTwo );
> >}

> >Hope this helps,
> >Chris R.



> >> Does anyone here talk about the advantages of "out"? Or
> >> in which case, using "out" is the best solution?

> >> IMHO I can do the same thing if I pass couple of
> >> references to a method, change their values in the
> scope
> >> and then in the caller, we still can get the changed
> >> value.

> >.



Sat, 23 Oct 2004 16:14:04 GMT  
 the advantage of "out"
I would say they go for security.

as out needs to be uninitialised, I can hardly pass in the wrong variable if
it already has been initialised.

In comparing to a ref variable, changes are thus not completly possible.

This could also be done with serious preconditions (or a Debug.Assert in the
function), but I doubt too many people here use this extensively.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsft MVP C#/.NET)


Quote:
> > But, what are their reasons? Any idea? Thanks in advance.

> My understanding is that using "out" allows the optimisation of
marshalling
> when making remote calls---either acorss a process boundary or a machine
> one.

> Cheers,

> Dave



> > I know the reference passed as "out" must be un-
> > initialized. But this does not mean we must design a new
> > keyword "out" to handle this case because passing an un-
> > initialized reference is not a must to a programming
> > language. Another word, many other programming languages
> > does not explicitly and particularly handle this case.

> > I think the reason why C# group designed this new keyword
> > is they must have some enough reasons to believe that
> > this keyword can do sth that any other keyword cannot do
> > or in some cases, using this keyword can beat others in
> > terms of easy-coding or the efficiency.

> > But, what are their reasons? Any idea? Thanks in advance.

> > >-----Original Message-----
> > >private void test1(  ref int someNumber1 )
> > >{
> > >    someNumber = 5;
> > >}

> > >private void test2(  out int someNumber2 )
> > >{
> > >    someNumber = 5;
> > >}

> > >private void button2_Click(object sender,
> > System.EventArgs e)
> > >{
> > >    int numberOne;
> > >    // Following give a compiler Error
> > >    //    Use of unassigned local variable 'numberOne'
> > >    test1( ref numberOne );

> > >    int numberTwo;
> > >    // There is no error, even though numberTwo is NOT
> > initialized.
> > >    test2( out numberTwo );
> > >}

> > >Hope this helps,
> > >Chris R.



> > >> Does anyone here talk about the advantages of "out"? Or
> > >> in which case, using "out" is the best solution?

> > >> IMHO I can do the same thing if I pass couple of
> > >> references to a method, change their values in the
> > scope
> > >> and then in the caller, we still can get the changed
> > >> value.

> > >.



Sat, 23 Oct 2004 16:34:23 GMT  
 the advantage of "out"
Out is useful when you're writing a function that's always going to return a
new instance (or value, for value types), and you don't need to pass a value
it. It saves the caller from having to initialize the parameter before it's
used.

--
Visit the C# product team at http://www.gotdotnet.com/team/csharp

This posting is provided "AS IS" with no warranties, and confers no rights.


Quote:
> Does anyone here talk about the advantages of "out"? Or
> in which case, using "out" is the best solution?

> IMHO I can do the same thing if I pass couple of
> references to a method, change their values in the scope
> and then in the caller, we still can get the changed
> value.



Sun, 24 Oct 2004 00:07:37 GMT  
 the advantage of "out"
   As Eric notes, you don't have to create an instance... and if you are
planning to return a large/complex object, then this becomes extremely
important, because if you HAD to create it in the calling method and send it
as a reference, you could easily end up with a much slower, labor intensive
method.

    For example, what if you are returning a dataset based on a large set of
tables?  That would also dicatate how you'd write the class where you
received the object from, so that, by passing a ref, it would fill an
existing dataset, rather than creating one on its own and passing it *out*,
as a new instance.

    In remoting, this would be an even more critical issue.  That's why COM
has the [out] option as well.

Chris R.



Quote:
> Out is useful when you're writing a function that's always going to return
a
> new instance (or value, for value types), and you don't need to pass a
value
> it. It saves the caller from having to initialize the parameter before
it's
> used.

> --
> Visit the C# product team at http://www.gotdotnet.com/team/csharp

> This posting is provided "AS IS" with no warranties, and confers no
rights.



> > Does anyone here talk about the advantages of "out"? Or
> > in which case, using "out" is the best solution?

> > IMHO I can do the same thing if I pass couple of
> > references to a method, change their values in the scope
> > and then in the caller, we still can get the changed
> > value.



Sun, 24 Oct 2004 00:44:05 GMT  
 the advantage of "out"


Quote:
> I know the reference passed as "out" must be un-
> initialized.

That's not quite right. out parameters can indeed be initialized and even
manipulated prior to being used in a method call as an out parameter. out
simply means that the parameter must have a value written to it by the
method and the parameter will always end up with a new value in the caller
no matter what value it had prior to using it in the method.

Quote:
> But this does not mean we must design a new
> keyword "out" to handle this case because passing an un-
> initialized reference is not a must to a programming
> language. Another word, many other programming languages
> does not explicitly and particularly handle this case.

Again, nowhere does out say that the variable you use in an out parameter
*must* be uninitialized. It *can* be, but there is certainly no reason it
must be. To wit:

  public static void OutParam(out string x)
  {
   x = "abc";
  }

   string x = "123";
   x = x + "456";
   Console.WriteLine("x=[" + x + "]");
   OutParam(out x);
   Console.WriteLine("x=[" + x + "]");

Quote:

> I think the reason why C# group designed this new keyword
> is they must have some enough reasons to believe that
> this keyword can do sth that any other keyword cannot do
> or in some cases, using this keyword can beat others in
> terms of easy-coding or the efficiency.

This is not new idea. Ada had this way back when. It may have been the first
to have this, but I'm not positive.

In conjuntion with ref and "by value" (the default) these three keywords
build in to the language what a method does with its parameters. In
programming languages where you can't do this you oftentimes but this
information ([in], [out], [in/out]) in the comments describing the method. I
think it's really great to have these three keywords built in to the
language. They catch a lot of bugs.

-glenn-



Sun, 24 Oct 2004 01:06:40 GMT  
 the advantage of "out"



Quote:
> I would say they go for security.

> as out needs to be uninitialised, I can hardly pass in the wrong variable
if
> it already has been initialised.

out parameters do not have to be uninitialized. It would be quite a
limitation if they did have to be.

Quote:

> In comparing to a ref variable, changes are thus not completly possible.

> This could also be done with serious preconditions (or a Debug.Assert in
the
> function), but I doubt too many people here use this extensively.

I like to describe out, ref, and "by value" as follows:

- out: From the callee's point of view, the variable is write-only (and any
current contents, if any, cannot be accessed); from the caller's POV, the
variable is always written to (not matter what it already contains).
- ref: From the callee's POV, the variable is read/write (any current
contents can be used and the varaiable can be completely over-written or
modified); from the caller's POV, the variable may or may not be modified by
the callee.
- "by value": From the callee's POV, the variable is read-only (a copy of
the variable is placed on the stack and can be modified as required but the
callee but the value the caller sees never changes); from the caller's POV,
the variable will be exactly the same after the method runs.

-glenn-



Sun, 24 Oct 2004 01:15:24 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. remove() vrs fopen("""w")

2. Displaying binary data as ascii "1"'s and "0"'s

3. Looking for "Shroud"/"Obfus"

4. ""help with TSR""

5. Parse trees and "("")"

6. Error "free"-ing "malloc"-ed memory

7. Displaying binary data as ascii "1"'s and "0"'s

8. Attention "C"/"C++" gurus and "C" newbees

9. merits of "#define", "const", and "enum" ???

10. why not "cout", "<<" , and "endl"??

11. "#define" versus "con

12. '.', '::", "->"

 

 
Powered by phpBB® Forum Software