8

Greeting for the day!

I have a question in my mind and looking for answer from some days. If my understanding is correct then only diff between Instance and object is :-

instance means just creating a reference(copy) .

object :means when memory location is associated with the object( is a runtime entity of the class) by using the new operator

Now i want to know how to create an instance of an object. Please give explanation with sample code

Any help will be appreciated. Thanks

1
  • 1
    Am I missing something, surely you just mean var obj = new Object()? Commented Aug 17, 2011 at 9:12

6 Answers 6

10

By your explanation it's not called an instance, but a reference of an object. An instance of a class is called an object. I think your question is: "What is the difference of an object and a reference variable?" I'll try to explain it with some examples:

Foo f;

I just declared a reference variable. This is not an object but only a reference that refers to an object.

f = new Foo();

Now I created a new object and assigned it to the f reference variable so every time I do something to f I refer to the Foo object. Like when I call f.Name = "MyFoo"; I refer to the foo object.

Foo otherFoo;

Now I declare another reference variable.

otherFoo = f;

What we have here now is having ONE object in the memory but TWO reference variables refering to the same object.

f.IsFoo = true;
bool isotherFooFoo = otherFoo.IsFoo;

This last line will return true because we changed the IsFoo property to true and f and otherFoo reffer to the same object.

I hope that explains you everything. :)

Sign up to request clarification or add additional context in comments.

5 Comments

I just have one word "WONDERFUL" explanation. Thanks for sharing the information Bosak :)
No thank you for asking and making me explain it right so i can understand it even more and practice my knolage about this:)
@Bosak, where does this picture come from? Do you have the right to post it here? You should at least mention the source...
@Thomas They look like they come from one of the Head First books. I flagged the post for a moderator to review and make a decision if they should be allowed here.
If you're going to copy that much from a book, you need to cite where it came from.
5

You don't create "an instance of an object", you create an instance of a class (or struct). An object is an instance of a class.

If you do:

Foo f = new Foo();

You create an instance of the Foo class.

2 Comments

Thanks for revert thomas.I have read it in many article and books that "an object is an instance of a class". So what the exact diff between object and instance. As per my knowledge, instance just create a reference.Let's talk about your above line of code. You have created an instance or object in above code? Sorry for my lacking knowledge :-|
There is no difference, it's the same thing... It's just a matter of terminology: usually you say "instance" when you refer to an instance of a specific class; the word "object" is more generic.
1

In the phrase "an object is an instance of a class", the word "instance" does not really have a technical meaning that is different to the word "object", it is just a way of defining, in English, what the word "object" means. The meaning of "instance" is really meant to be the same as the meaning of "object". We can break this down as follows:

an object is an instance of a class
an object = instance of a class
an object = instance

Comments

1

In C# 9.0 there is a new way to initialize a class by Target-typed new expressions.

You can initialize the class like this:

Foo f = new();

Note, f is a reference to the class Foo.

1 Comment

Great, thanks for sharing the information @Misha Zaslavsky
0

We have a class ABC

Class ABC 
    {
        string name="";

        public ABC()
        {
            this.name = "A1";
        }
        public ABC(name)
        {
            this.name = name;
        }

    }

An Instance of a class can be created as:

ABC a1 = new ABC();

or

ABC a1 = new ABC("James");

Comments

-1

You create instance of a class and not the object.

1 Comment

The wording is the same as in the question, so I guess the OP has already seen this page... Anyway, this explanation is wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.