Click here to Skip to main content
12,444,790 members (52,425 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 9-Jan-13 5:39am
Comments
   
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
   
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Short answer is different instances reference different memory locations.

Long answer: http://msdn.microsoft.com/en-us/magazine/cc301569.aspx[^]
  Permalink  
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
Marcus Kramer 9-Jan-13 12:31pm
   
+5.
   
That is right, too, my 5, but please see my comment so Solution 5.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
   
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
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
  }
}
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100