locked
what is difference between an instance and object of a class?? RRS feed

  • Question

  • I have kind of confusion between object and instance of class. So pls help me out on this.
    Thanks
    Monday, August 20, 2012 6:36 PM

Answers

  • The two are mostly interchangable in C#, but I'd typically recommend using "instance" as the preferred terminology.  

    Since C# is based on creating instances of classes, it falls under this description on Wikipedia (here http://en.wikipedia.org/wiki/Object_(computer_science)#Mechanism) : "In a language where each object is created from a class, an object is called an instance of that class."

    Typically, using instance is done because it's very explicit - you create an instance of a class, and assign it to a variable.  "object" is often no the preferred terminology in C# (or other .NET languages) since "object" has a special meaning (System.Object is a specific class, and "object" aliases that directly in the language).


    By using "instance", you're clearly talking about a specific instance of a type.  When you use "object", you may be referring to a variable defined as object, and not the instance to which it points.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Norkk Monday, August 20, 2012 6:48 PM
    • Marked as answer by Bob Shen Thursday, August 30, 2012 10:06 AM
    Monday, August 20, 2012 6:42 PM
  • I can write words on a page, but words are not objects while a page is. The possibility of boxing values into objects doesn't make values "sort of objects".

    If I'm not wrong, the terminology is coherent with the C# Language Specification.

    But I understand why it can be confusing.

    The word "instance" is unambiguous and covers both reference-types and value-types. When you want to distinguish between objects and values, using "instance of a reference-type" and "instance of a value-type" are precise and unambiguous.

    • Marked as answer by Bob Shen Thursday, August 30, 2012 10:07 AM
    Wednesday, August 22, 2012 6:53 AM

All replies

  • The two are mostly interchangable in C#, but I'd typically recommend using "instance" as the preferred terminology.  

    Since C# is based on creating instances of classes, it falls under this description on Wikipedia (here http://en.wikipedia.org/wiki/Object_(computer_science)#Mechanism) : "In a language where each object is created from a class, an object is called an instance of that class."

    Typically, using instance is done because it's very explicit - you create an instance of a class, and assign it to a variable.  "object" is often no the preferred terminology in C# (or other .NET languages) since "object" has a special meaning (System.Object is a specific class, and "object" aliases that directly in the language).


    By using "instance", you're clearly talking about a specific instance of a type.  When you use "object", you may be referring to a variable defined as object, and not the instance to which it points.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Norkk Monday, August 20, 2012 6:48 PM
    • Marked as answer by Bob Shen Thursday, August 30, 2012 10:06 AM
    Monday, August 20, 2012 6:42 PM
  • Could your please check this stackoverflow post?

    Web Developer

    Monday, August 20, 2012 6:42 PM
  • Objects are instances of a class. An instance of a value-type is not an object unless you box it (assign it to a reference-type variable).

    • Edited by Louis.fr Tuesday, August 21, 2012 7:27 AM
    Tuesday, August 21, 2012 7:23 AM
  • Objects are instances of a class. An instance of a value-type is not an object unless you box it (assign it to a reference-type variable).

    Louis - this is exactly why I try to avoid using "object" to refer to anything related to concrete instances of types.  Value types are not objects in one sense, but they also do derive from System.Object, and can be boxed into an "object" - so they can sort of be an object.  It gets confusing, so I prefer to avoid using object as a term when dealing with .NET or C# for anything except "System.Object" or the "object" keyword.

    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, August 21, 2012 4:10 PM
  • I can write words on a page, but words are not objects while a page is. The possibility of boxing values into objects doesn't make values "sort of objects".

    If I'm not wrong, the terminology is coherent with the C# Language Specification.

    But I understand why it can be confusing.

    The word "instance" is unambiguous and covers both reference-types and value-types. When you want to distinguish between objects and values, using "instance of a reference-type" and "instance of a value-type" are precise and unambiguous.

    • Marked as answer by Bob Shen Thursday, August 30, 2012 10:07 AM
    Wednesday, August 22, 2012 6:53 AM
  • http://stackoverflow.com/questions/1215881/the-difference-between-classes-objects-and-instances
    Monday, April 1, 2013 3:11 PM