Reputation: 458
Situation: I recently came across an article where an employer was asking entry-level candidates to solve a complex reference problem (not shown), as well as questions pertaining to the stack and managed heap. I have devoted a bit of time to learning these concepts for personal understanding, but was not aware that these are considered 'entry-level' knowledge. Being a CIS major apposed to CS (huge difference I know) these concepts are not taught.
Question Why would a developer assign an object reference to another reference? More succinctly, why would a developer ever flip flop dozens of references around as described in interview scenarios? Is this simply a matter of testing subject matter comprehension, or is this 'reference flip-flopping' a practice used in "everyday" development?
using System;
namespace QuickConsoleTesting
{
class Program
{
static void Main()
{
//Instantiate two new Person objects on the heap
Person person1 = new Person() { Name = "Jim" };
Person person2 = new Person() { Name = "Todd" };
Person person3 = person1;
Person person4 = new Person() { Name = "Julie" };
//Flip-flop reference variables
person4 = person2;
person1 = person3;
//Display results
Console.WriteLine(person1.Name);
Console.WriteLine(person2.Name);
Console.WriteLine(person3.Name);
Console.WriteLine(person4.Name);
/* ======================================
* Results displayed: Jim, Todd, Jim, Todd
* ===================================== */
//Hold console window
Console.Read();
}//END OF MAIN
}//END OF PROGRAM
class Person
{
private string name = "";
public string Name
{
get { return name; }
set { name = value; }
}
}//END OF PERSON
}//END OF NAMESPACE
Upvotes: 0
Views: 6494
Reputation: 32807
imagine a class
class Person
{
pubic string Name;
}
With pass by value which is default
Person p=new Person();
//p contains the address of the newly allocated person
change(p);
//p is passed by value so the address within p would get copied into p1 which is a method parameter of change method
public void change(Person p1)
//p1 and p are separate variables containing address of person object
{
p1.Name="SO";
//changes name of p1 and p
p1=null;
//this makes p1 null not p since p and p1 are separate copies pointing to person object
}
With reference
change(ref p);
//p is now passed by reference so p1 is p
public void change(ref Person p1)//p1 and p are same
{
p1.Name="SO";
p1=null;//this makes p and p1 null
}
Upvotes: 2
Reputation: 16065
I am not sure exactly what you're asking, there are many reasons a developer might store the same reference in multiple variables, but that all depends on the code. Remember there is still only one place that the object lives on in the heap, but could have many references to it (and thats where Managed Garbage Collector comes in to clean up objects no longer referenced)
Obviously your example is simply bad code. These types of questions are akin to the C++ pointer questions. C# as a manged language has tried to stay away from pointers, but it is still important to understand the differences between value types and reference types. This is mostly what these questions are designed for.
Upvotes: 1
Reputation: 6270
These sort of questions are there to test if a candidate understands the difference between a reference variable and a value variable. The supplied scenario (as is) will almost never happen in Real World (tm) code. But, confusion resulting in not understanding why this code snippet does what it does can and does result in a lot of bugs, especially when there are various functions working on the same data.
Upvotes: 3