whatever123456
whatever123456

Reputation: 31

Difference between object and instance

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?

Upvotes: 1

Views: 4023

Answers (2)

Christos
Christos

Reputation: 53958

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.

Upvotes: 8

Charles Bretana
Charles Bretana

Reputation: 146603

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.

Upvotes: 2

Related Questions