Discussion: What is the default type (ref or value) for parameters passed to function??
-
Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!
-
Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!
By default, parameters are passed by value. Click me[^]
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!
Depends on whether the parameter is a reference type or a value type?
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
Hi all, Let’s discuss and share your knowledge on this question!! I am sure most of the member having this question in their mind and as many as members knows the answer for this!! Mean while, you can also ask your queries to other on same topic!!!
By default, by value (a copy). If the parameter is a value type, it's a "copy" of that value. If the parameter is a reference type, it's a "copy" of that reference (by reference). If you use ref or out: If the parameter is a value type, you pass a reference (address) to it. If the parameter is a reference type, you pass a reference to this original reference which refers to the object. As an example of this, write a function that takes 2 arrays and swaps them (without copying the elements).
Eslam Afifi
-
By default, parameters are passed by value. Click me[^]
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Groan.. I know where you're coming from - for reference types, references to objects are passed by value by default. "Passed by reference" is not entirely accurate, but it describes the object sharing aspect well.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
By default, by value (a copy). If the parameter is a value type, it's a "copy" of that value. If the parameter is a reference type, it's a "copy" of that reference (by reference). If you use ref or out: If the parameter is a value type, you pass a reference (address) to it. If the parameter is a reference type, you pass a reference to this original reference which refers to the object. As an example of this, write a function that takes 2 arrays and swaps them (without copying the elements).
Eslam Afifi
If reference(from stack) to the reference type(e.g object)is passed then, whatever changes done inside the functions are reflected to the object (as another copy of reference is created on stack which also point to original location e.g object)..........isn't it?? then what is the use of ref and out keyword here??? anybody like share??
-
If reference(from stack) to the reference type(e.g object)is passed then, whatever changes done inside the functions are reflected to the object (as another copy of reference is created on stack which also point to original location e.g object)..........isn't it?? then what is the use of ref and out keyword here??? anybody like share??
class A
{
public Guid Guid;
public A()
{
Guid = Guid.NewGuid();
}
}static void Main(string[] args)
{
var a = new A();
Console.WriteLine(a.Guid);
Foo(a);
Console.WriteLine(a.Guid);
}static void Foo(A par)
{
Console.WriteLine(par.Guid);
par = new A();
Console.WriteLine(par.Guid);
}static void Main(string[] args)
{
var a = new A();
Console.WriteLine(a.Guid);
Foo(ref a);
Console.WriteLine(a.Guid);
}static void Foo(ref A par)
{
Console.WriteLine(par.Guid);
par = new A();
// btw, you still can do something like par.Guid = Guid.NewGuid();
Console.WriteLine(par.Guid);
}Eslam Afifi
modified on Thursday, March 19, 2009 10:48 AM
-
If reference(from stack) to the reference type(e.g object)is passed then, whatever changes done inside the functions are reflected to the object (as another copy of reference is created on stack which also point to original location e.g object)..........isn't it?? then what is the use of ref and out keyword here??? anybody like share??
deep@Pune wrote:
then what is the use of ref and out keyword here???
ref and out pass the reference to the reference by value :)
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro