Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how different instances can have different values for the same member of a class when a class is a reference type

Since class is a reference type the instances of class point only to the memory location of the members or variables in the class.Then how different instances of class can have different copies of class members or variables
Posted
Comments
Sergey Alexandrovich Kryukov 9-Jan-13 19:54pm    
Apparently, because you have no clue what a reference type is. It's absolutely not what you think. And I doubt that you understand well what a type is. Let's see...
—SA
Sergey Alexandrovich Kryukov 9-Jan-13 19:59pm    
OK, now I see that you also mix it up with boxing and unboxing, which are related to value types, not needed for reference type.
I don't know what to do. I looks like you are not ready for this yet. Could you start from the very beginning: variables, functions, parameters...
—SA

Short answer is different instances reference different memory locations.

Long answer: http://msdn.microsoft.com/en-us/magazine/cc301569.aspx[^]
 
Share this answer
 
Comments
sarathsprakash 9-Jan-13 11:47am    
could you just elaborate it
Adam R Harris 9-Jan-13 11:49am    
Sure, http://msdn.microsoft.com/en-us/magazine/cc301569.aspx
fjdiewornncalwe 9-Jan-13 12:31pm    
+5.
Sergey Alexandrovich Kryukov 9-Jan-13 19:57pm    
That is right, too, my 5, but please see my comment so Solution 5.
—SA
Since class is a reference type the instances of class point only to the memory location of the members or variables in the class.Then how different instances of class can have different copies of class members or variables

Because each instance points to its own block of memory allocated on the heap by the new statement.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Jan-13 19:56pm    
Should give an idea, even though it might not address the OP doubt. (I'm not saying you have to address it.) The problem is false understanding of reference types, but my imagination fails me when I try to follow OP's thinking. Anyway, you got my 5.
—SA
I misread your question here is my updated solution.

when you create an instance of the class with new keyword it creates a new instance and new memory block for that instance hence it can have different values for the same member of the class.

in detail
C#
class Student
{
 public string StudentName = string.Empty;
 public int Age = 0;
}

class StudentManager
{
  public void ManageStudents()
  {
    Student student1 = new Student(); // Creating Instance1;
    
    student1.Name = "Student 1"; 
    
    Student student2 = new Student(); // Creating Instance2;
    student2 .Name = "Student 2"; // Instance 2 created 

    //Instance1 and Instance2 are different because we create 2 different memory block for the student class using the new Keyword
    //Now see the magic of Reference type
    int age = 10;
    Console.WriteLine(student1.Age);    
    ChangeStudentName(student1,age);  
    Console.WriteLine(student1.Age);     
  }
 
  private ChangeStudentAge(Student student,int newAge)
  {
     student.Age= newAge;
     newAge = 100;
    //the variable student is local to this method but any changes made to the student instance will get effected on the original student1 instance inside the 'ManageStudents' method. but any changes made to the 'newAge' int variable does not change the age variable defined in the ManageStudents method. 

   //Because student is reference type object and int is a value type object
  }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900