1

I am studying C# and I am a little confused with the terminology... I saw that there are still many same questions like this but i am still confused... Can someone explain me the difference between an object and an instance?

4
  • It's just a matter of semantics, really. Depends on the context of what you're talking about. Without context, the terms are pretty much interchangeable. Is there some specific text or example in which they're meant to be different? One might use "object" in a slightly more generic way when saying, for example, "x is an instance of a Widget object." Commented Jan 6, 2017 at 15:30
  • 1
    Possible duplicate of Difference between object and instance Commented Jan 6, 2017 at 15:32
  • Not exactly... I need it as a general meaning, since they are used frequently in programming terminology Commented Jan 6, 2017 at 15:33
  • @whatever123456 And their meaning is contextual. There is no one single definition, so you either need to provide a context, or you can't get a meaningful (correct) answer. Commented Jan 6, 2017 at 15:34

2 Answers 2

8

The words object and instance basically refer to the same thing.

In object oriented programming there is the concept of classes. You can basically do nothing without a class.

A class is the blueprint for creating an object (classes marked as static, abstract etc. are excluded from this statement.), with specific characteristics and behaviour defined in the class. An object can be also called an instance of a class.

For example:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

The above is a class. It just defines a type called Person with two properties Name and Age.

When you need a person object (or an instance of this class), you can create it using the new operator and an appropriate constructor (below I use an object initializer).

var person = new Person 
{ 
    Name = "Foo",
    Age = 30
};

Now the person is an object or equivalently an instance of the class Person.

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

4 Comments

so basically an object and an instance of a class is the same thing?
Exactly, they are the same thing !
static classes still inherit from System.Object. That's where I'm still not sold
@Jonesopolis Every type in .NET has as it's base type the System.Object. Actually, you can think of a static class as an object which has been created automatically by the runtime on the loading of your application, but no other type can be created based on the class used to create this object. There would be only one object with the specifed behaviour in your app. Beware this is an analogy I tried to use to explain it. Regarding the term instance in this context, I think it's meaningless. You can't say I have an instance of a static class...What's the point?
2

This is only terminology, and common usage. The word object is used, generally to refer to the thing that is created in memory, and referenced or represented by a variable in your code, when you create a data structure based on a class. The word instance means exactly the same thing, but is used when the emphasis is on the fact that a specific object is but one of many objects that may have been or could be created from that class. i.e., an object created from class MyClass is but one instance of myClass.

2 Comments

This description falls down a bit in the case where you have several instances of the class object as here instance and object are not interchangeable. Objects are "things" like "people", where as an in this analogy an instance would be an individual "person". As all "things" derive from object, people can still be objects, but a person is an instance of an object, not an object.
I'm not clear as to what your point is. Are you discussing the scenario where someone creates a custom class and names it object ? I'm not sure that is even possible. I think not. Or are you referring to the built-in .Net class object that is the root of the class hierarchy? If that is the case, then, technically, every instance (of anything, not just of a person) is indeed an object, just as every moose is indeed an animal.

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.