C#, ASP.NET and out...

liunx

Guest
Hi Everyone,


I'd just like to ask everyone's views on the reasoning behind using an out parameter over using a returned value of the function.

:confused:Why use out at all, when you can use either a standard casted function or define a struct to hold anything more complex?:confused:


With regards,
Paul.ref is another keyword.

ref is a reference to the original object. out is identicial to ref except that it requires that you assign a value to "out" required to compile the code.

The use of those keywords are good for several reasons. If you have a function which returns saying you have a good state of your data or whatever, and need a value from the function that can be modified by the function.

ref and out work so that it passes the "pointer" to the method much like C/C++ languages but with additional rules.

By default in C# all classes are passed by reference.

References are used to reduce the amount of copying a variable around. By default an int, string and the rest of intrinsic types are passed by copy. which just passes the value but can not modify the original value.

This is useful for things like Images and such which have a good some of data, which can be modified by the calling function, but do not want to create an exponent of the size of memory for the stack call that any data being modified.

Hopefully this explains the proper use of these keywords.Oh - I agree; I can see the benefit of ByRef and ByVal, as I have used them frequently in VB. I can't really, though, see any advantage or benefit in the out qualifier...

Maybe it's just me?... }:-)

Paul.Yes I can. It insures that the value is a "ref" and also that the function assigns a value to the variable.

Ref and out are like that of VB. However VB's memory state is protected thats why if you import libraries from Win32 the values must be passed to it and any functions in your code as ByVal. So that it can not add it to the stack making it another pointer. its like Pointer to a Pointer.

In VB you can not de-refernce a pointer.Hmmm... I'm afraid out still seems a little grey to me. I'll have to use it, perhaps, to appreciate it more.

Thanks, Afterburner!


Paul. ;)
 
Back
Top