If a object can have multiple references which can have their own state, does this mean that references are also objects themselves?

I only ask this because because references have their own individual definitions of the objects fields that are not shared between other references.

I only ask this because because references have their own individual definitions of the objects fields that are not shared between other references.

What exactly do you mean by that?

I mean objects have fields for example:

public void myHuman()
{
    string name;
    int age;
}

public void makeReference()
{
    myHuman person1 = new myHuman();
    person1.name = John Key;
    person1.age = 31;

    myHuman person2 = new myHuman();
    person2.name = Keil Carpenter;
    person2.age = 24;
}

makeRefence() creates two references to the myHuman object and each reference sets and keeps its own state by defining its own variables name and age. person1 variables are seperate to that of person2

You're creating two references to two different myHuman objects. If you had two references to the same object (as you would if you did myHuman person2 = person1 instead of myHuman person2 = new myHuman()), the fields would always have the same values, no matter which reference you access them through.

Example:

myHuman ref1 = new myHuman();
ref1.name "John Key";

ref2 = ref1;
Console.WriteLine(ref1.name);
Console.WriteLine(ref2.name); // Both will print "John Key"

ref2.name = "Keil Carpenter";
Console.WriteLine(ref1.name);
Console.WriteLine(ref2.name); // Now both will print "Keil Carpenter"

Basically every time you use new, you get a new object. And if you just assign one variable to another, you get two references to the same object.

Unless human1 and human2 will never be used outside of MakeReference() you should define them outside of the method:

    public class myHuman
    {
        string name;
        int age;
    }

    public class myClass
    {
        public myHuman person1;
        public myHuman person2;

        public void MakeReference()
        {
            person1 = new myHuman();
            person1.name = John Key;
            person1.age = 31;

            person2 = new myHuman();
            person2.name = Keil Carpenter;
            person2.age = 24;       
        }
    }

Like sepp2k said the new keyword mean you're creating a new instance of what ever is to the right of it.

Ok thankyou, I thought that for this instance myHuman() was the object and person1 and person2 are two seperate references to the one myHuman Object

@ChrisHunter
Ahh gotcha, the new keyword creates a new object, so creating two references to one object would be as sepp2k explained.

Dammit, still learning :)

Thankyou

Always will be too but the more you learn the better it gets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.