Should we use 'ref' keyword while passing objects also...
-
Hi , I've read that objects are reference types and also that 'ref' keyword is used to pass the reference of a value ... Now plz look into the following examples...
namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { man.name = "Grade1Employee"; } } public class Manager { public string name = string.Empty; } } The program gives the output as "Grade1Employee" .... Example2 namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); emp.Name="Grade1Employee"; changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { Manager emp2 = new Manager(); emp2.name = "TopLevelEmployee"; man = emp2; } } public class Manager { public string name = string.Empty; } }
..The above still gives the output as "Grade1Employee" why is it so? When object is a reference type then the value should be changed right?...when use a ref keyword for the parameter name in the method like "changevalue(ref Manager man)" then the output is "TopLevelEmployee"... Why is it behaving differently in two cases..?Koushik
-
Hi , I've read that objects are reference types and also that 'ref' keyword is used to pass the reference of a value ... Now plz look into the following examples...
namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { man.name = "Grade1Employee"; } } public class Manager { public string name = string.Empty; } } The program gives the output as "Grade1Employee" .... Example2 namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); emp.Name="Grade1Employee"; changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { Manager emp2 = new Manager(); emp2.name = "TopLevelEmployee"; man = emp2; } } public class Manager { public string name = string.Empty; } }
..The above still gives the output as "Grade1Employee" why is it so? When object is a reference type then the value should be changed right?...when use a ref keyword for the parameter name in the method like "changevalue(ref Manager man)" then the output is "TopLevelEmployee"... Why is it behaving differently in two cases..?Koushik
Spunky Coder wrote:
public static void changevalue(Manager man) { Manager emp2 = new Manager(); emp2.name = "TopLevelEmployee"; man = emp2; }
This is correct. Objects are passed as reference, but that means that instead of copying whole object, reference to object is copied.
changevalue(Manager man) <- copy of "pointer" to instance Manager { Manager emp2 = new Manager(); <- local variable emp2.name = "TopLevelEmployee"; <- you do changes to local variable man = emp2; <- you assign the copy of pointer to instance to local instance } <- all is forgotten when method returns
Why is that called passing by reference? If it was passed by value, following code:
changevalue(Manager man) <- copy of instance of Manager { man.name = "TopLevelEmployee"; <- you do changes to copy }
would NOT change name of man outside the method. As you can try, it indeed does change the name, because objects are passed by reference. If you really want to change the reference to man, use keyword ref. (think about pointer to pointer)
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
Hi , I've read that objects are reference types and also that 'ref' keyword is used to pass the reference of a value ... Now plz look into the following examples...
namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { man.name = "Grade1Employee"; } } public class Manager { public string name = string.Empty; } } The program gives the output as "Grade1Employee" .... Example2 namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); emp.Name="Grade1Employee"; changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { Manager emp2 = new Manager(); emp2.name = "TopLevelEmployee"; man = emp2; } } public class Manager { public string name = string.Empty; } }
..The above still gives the output as "Grade1Employee" why is it so? When object is a reference type then the value should be changed right?...when use a ref keyword for the parameter name in the method like "changevalue(ref Manager man)" then the output is "TopLevelEmployee"... Why is it behaving differently in two cases..?Koushik
Spunky Coder wrote:
The above still gives the output as "Grade1Employee" why is it so?
You're passing a reference to a Manager object to the method. If you change the object via this reference the changes reflect outside the method (first example). If you create a new object and assign the reference to it to the method parameter, then this change does not reflect outside the method (second example), because changes to parameters (values or references) do not reflect outside a method unless you use the ref or out keyword.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hi , I've read that objects are reference types and also that 'ref' keyword is used to pass the reference of a value ... Now plz look into the following examples...
namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { man.name = "Grade1Employee"; } } public class Manager { public string name = string.Empty; } } The program gives the output as "Grade1Employee" .... Example2 namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); emp.Name="Grade1Employee"; changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { Manager emp2 = new Manager(); emp2.name = "TopLevelEmployee"; man = emp2; } } public class Manager { public string name = string.Empty; } }
..The above still gives the output as "Grade1Employee" why is it so? When object is a reference type then the value should be changed right?...when use a ref keyword for the parameter name in the method like "changevalue(ref Manager man)" then the output is "TopLevelEmployee"... Why is it behaving differently in two cases..?Koushik
When you take (Manager man) as input, you are actually taking a copy of the pointer to the actual manager. that means it points to the same object, but you cannot change the pointer adress declared in main, to do that you need the ref.. dunno if you get what i mean tho, im not good at explaining ^^ Manager man <- pointer to a manager object = new Manager() <- returns adress to the new object
fafafa, ringakta icke sådant som bringa ack så naggande högönsklig välmåga å baronens ära.
-
Spunky Coder wrote:
public static void changevalue(Manager man) { Manager emp2 = new Manager(); emp2.name = "TopLevelEmployee"; man = emp2; }
This is correct. Objects are passed as reference, but that means that instead of copying whole object, reference to object is copied.
changevalue(Manager man) <- copy of "pointer" to instance Manager { Manager emp2 = new Manager(); <- local variable emp2.name = "TopLevelEmployee"; <- you do changes to local variable man = emp2; <- you assign the copy of pointer to instance to local instance } <- all is forgotten when method returns
Why is that called passing by reference? If it was passed by value, following code:
changevalue(Manager man) <- copy of instance of Manager { man.name = "TopLevelEmployee"; <- you do changes to copy }
would NOT change name of man outside the method. As you can try, it indeed does change the name, because objects are passed by reference. If you really want to change the reference to man, use keyword ref. (think about pointer to pointer)
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne MetcalfeHi.. changevalue(Manager man) <- copy of "pointer" to instance Manager { Manager emp2 = new Manager(); <- local variable emp2.name = "TopLevelEmployee"; <- you do changes to local variable man = emp2; <- you assign the copy of pointer to instance to local instance } <- all is forgotten when method returns ...Now so what's the difference between passing an int datatype and object when its scope is just limited to the called function ?...
Koushik
-
Hi.. changevalue(Manager man) <- copy of "pointer" to instance Manager { Manager emp2 = new Manager(); <- local variable emp2.name = "TopLevelEmployee"; <- you do changes to local variable man = emp2; <- you assign the copy of pointer to instance to local instance } <- all is forgotten when method returns ...Now so what's the difference between passing an int datatype and object when its scope is just limited to the called function ?...
Koushik
Spunky Coder wrote:
...Now so what's the difference between passing an int datatype and object when its scope is just limited to the called function ?...
No, scope of the reference is limited to called function. Actual instance of object you change via the reference will keep the changes outside that function. Is this what you mean?
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
Hi , I've read that objects are reference types and also that 'ref' keyword is used to pass the reference of a value ... Now plz look into the following examples...
namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { man.name = "Grade1Employee"; } } public class Manager { public string name = string.Empty; } } The program gives the output as "Grade1Employee" .... Example2 namespace ExampleConsoleApp { public class Program { static void Main(string[] args) { Manager emp = new Manager(); emp.Name="Grade1Employee"; changevalue(emp); Console.Out.WriteLine(" " + emp.name); Console.Read(); } public static void changevalue(Manager man) { Manager emp2 = new Manager(); emp2.name = "TopLevelEmployee"; man = emp2; } } public class Manager { public string name = string.Empty; } }
..The above still gives the output as "Grade1Employee" why is it so? When object is a reference type then the value should be changed right?...when use a ref keyword for the parameter name in the method like "changevalue(ref Manager man)" then the output is "TopLevelEmployee"... Why is it behaving differently in two cases..?Koushik
Suppose we alter the first example as following... public static void changevalue(Manager man) { man.name = "Grade1Employee"; man = null; } still the emp.name in the main function gives the value "Grade1Employee"....Now when we are assigning a value to the member of the object (to the reference copy of the object) , the value is retained and when are setting the reference copy to null the value is not retained.... :(
Koushik
-
Spunky Coder wrote:
...Now so what's the difference between passing an int datatype and object when its scope is just limited to the called function ?...
No, scope of the reference is limited to called function. Actual instance of object you change via the reference will keep the changes outside that function. Is this what you mean?
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne MetcalfeI'm totally confused...:^) ...i was taught that both in java and c# objects are referenced types so when we do any operations on the object they get reflected to entire object wherever you access it ..but now its behaving differently...
Koushik
-
I'm totally confused...:^) ...i was taught that both in java and c# objects are referenced types so when we do any operations on the object they get reflected to entire object wherever you access it ..but now its behaving differently...
Koushik
Spunky Coder wrote:
i was taught that both in java and c# objects are referenced types so when we do any operations on the object they get reflected to entire object wherever you access it
But you are not doing any operation on object, you are changing the reference itself.
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe