3

I was asked a question in an interview and i wasn't able to answer it... Here is the question

  • How will you define an instance[c#]?

My answer was it is an other name of an object... what is the right answer for this question...

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ACP
  • 34,682
  • 100
  • 231
  • 371

10 Answers10

26

Instance is to class as cake is to recipe. Any time you use a constructor to create an object, you are creating an instance.

Adam Ruth
  • 3,575
  • 1
  • 21
  • 20
8

MyObject obj = new MyObject( );

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • 5
    That is how I "define an instance" – Muad'Dib Feb 08 '10 at 05:09
  • 4
    No, that is how you **construct** an instance. That's not a definition. Interview questions like this are generally intended to test a programmer's ability to communicate effectively. Is this how you would explain it to Doris in Accounts Receivable? – Aaronaught Feb 08 '10 at 14:14
  • 6
    If Doris in Accounts Receivable was interviewing me, I wouldn't want the job. – Muad'Dib Feb 08 '10 at 14:45
  • This is the best convo I've ever read. +1 for making me laugh at work. –  May 19 '16 at 15:53
6

I would describe instance as a single copy of an object. There might be one, there might be thousands, but an instance is a specific copy, to which you can have a reference.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
4

Class is the blueprint, instance is the completed construction.

Benny
  • 8,547
  • 9
  • 60
  • 93
1

An "instance" is an object allocated in memory, usually initialized by the compiler directive 'new, rendered according to the structure of a template which is most often a built-in language-feature (like a native data structure : a Dictionary, List, etc.), or a built-in .NET class (like a WinForm ?), or a user-defined class, or struct in .NET; or, even an Enum.

While an "instance" of a "class," for example, will embody, or contain, all the properties, fields, and methods of the class, the fields and/or properties may, or may not, have values allocated to them when the "instance" is created. The class template will also constrain the accessibility of the properties, fields, and methods inside any instance of the class.

The instance is "the real something" created from some "abstract plan for the something."

BillW
  • 3,415
  • 4
  • 27
  • 46
1

Instances and objects are same if we consider only classes but different if we consider the whole C#. Instance is more general than object.

Anything which occupy space or memory and build by following some blue print is an instance of that blue print.

An object is denotes the reference to a memory location assigned by following memory requirements of a class;

Example:

They are same

  • An object is an instance of a class.

  • var John = new Person();

We get object John by assigning it new Person(). Here new Person() first reserves total memory required for storing its value type properties & its references and then assign default values to its properties.

So this 'reserved memory with default value' is named 'John' which is an INSTANCE of a class and in OOPs is called OBJECT.

They are different

  • A variable is an instance of its type.

  • int x = 5;

Here everything is same. x is a name of memory location which is exactly 4 byte in capacity to store an integer. What is different is here x is an INSTANCE of an int but not an object.

Sandeep Kumar
  • 1,505
  • 1
  • 15
  • 24
  • May be I'm wrong but my knowledge and these resources points me right. :) http://en.wikipedia.org/wiki/Object-oriented_programming http://stackoverflow.com/questions/1682231/how-do-valuetypes-derive-from-object-referencetype-and-still-be-valuetypes http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx http://en.wikipedia.org/wiki/Object_type_(object-oriented_programming) http://en.wikipedia.org/wiki/Value_type These resources are from SO, Wiki and Microsoft which itself justify that these are reviewed and followed by millions of developers like me so they should be correct. – Sandeep Kumar Mar 10 '14 at 19:33
  • @Servy As if you treat value types as object then OOPS would be meaning less and languages like assembly and c should be called Object oriented languages because everything is object even 5. – Sandeep Kumar Mar 10 '14 at 19:36
  • To your first link, I see nothing there to indicate that value types aren't objects, the same is true of the third and fifth links. The second link is explaining how value types *are* objects. It refutes your point. The fourth link is using `object` in the sense of the class, `System.Object`, not the computer science sense of the word. – Servy Mar 10 '14 at 19:57
  • The fact that value types are objects, and that languages such as assembly and C use them, does not make them OO languages. The ability to create custom types is necessary for a language to be considered OO. That doesn't meant that ints aren't objects, just that using a language limited to a infinite set of objects isn't OO. Having a finite set is not the same at all as having none. The key point, and Eric mentions it in his answer, is that the storage mechanism for how data is stored has no impact on whether or not it is an object (in the CS sense of the word). – Servy Mar 10 '14 at 19:59
  • First Link: Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Third Link: I think you could not clearly understand this "Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself." >>: http://msdn.microsoft.com/en-us/library/3ewxz6et.aspx – Sandeep Kumar Mar 10 '14 at 20:46
  • Fifth Link: A type that does determine constraints for storage in random-access memory is often called an object type. Second Link: This is a somewhat artificial construct maintained by the CLR. For further details on this >>: http://msdn.microsoft.com/en-us/library/3ewxz6et.aspx Forth Link: Boxing, otherwise known as wrapping, is the process of placing a primitive type within an object so that the primitive can be used as a reference object For your computer science related stuff http://en.wikipedia.org/wiki/Object_(computer_science) – Sandeep Kumar Mar 10 '14 at 20:47
  • For custom types in C, you too may be know about struct are custom types And at last Eric Conclusion: "How is it possible that every red box (value types) is inside (derives from) box O (System.Object), which is a blue box (a reference Type) and still be a red box (a value type)?" A value type is value type not an reference type but is inside an reference type. And being inside an refrence type does not make it refrence type. And from Reed Copsey: This is a somewhat artificial construct maintained by the CLR in order to allow all types to be treated as a System.Object. – Sandeep Kumar Mar 10 '14 at 20:49
  • None of the things that you just mentioned in any way exclude value types from being objects. They are a bunch of related facts about value/reference types, neither is defining what an `object` is or stating whether a value/reference type is or is not an object. The only things that *appear* to touch on the issue are talking about the `System.Object` class, not the general concept of an "object" in abstract terms. That something isn't an instance of `System.Object` doesn't make it not an object in the abstract sense of the word. – Servy Mar 10 '14 at 20:52
  • When looking at whether a language is object oriented another key point is polymorphism. That C doesn't support this is probably one of the key reasons that it isn't considered an OO language. – Servy Mar 10 '14 at 20:55
  • http://msdn.microsoft.com/en-us/library/3ewxz6et.aspx Variables that are value types store data, and those that are reference types store references to the actual data. Reference types are also referred to as objects. http://en.wikipedia.org/wiki/Object_(computer_science) In computer science, an object is a location in memory having a value and referenced by an identifier objects are those which points to an address of some memory but value types are those which points to data in some memory. – Sandeep Kumar Mar 10 '14 at 21:00
  • Yes, I'm well aware about the difference between reference/value types. You don't need to keep defining them over and over or linking to sources that do. You've linked to several sources that state that instance of reference types are objects. You have linked to nothing that states that instances of value types are not objects, nor have you provided a definition of "object" that would specifically exclude value types for any reason. None of what you are quoting is actually proving your point. – Servy Mar 10 '14 at 21:02
  • Page 114: Line 7 I thought you have good knowledge and understanding but you want everything in written so there you go :) "instances of a value type are not objects" http://books.google.co.in/books?id=Kl1DVZ8wTqcC&pg=PA114&lpg=PA114&dq=why+value+types+are+not+object&source=bl&ots=5b7PDGVNPL&sig=5Xu4fZ1lXjasu5ij7fxMsOvpOi8&hl=en&sa=X&ei=2ikeU93ZK8KHrQfI_YGIDg&ved=0CGQQ6AEwCA#v=onepage&q=but%20instances%20of%20a%20value%20type%20are%20not%20objects&f=false – Sandeep Kumar Mar 10 '14 at 21:19
  • That looks like it's referring to the `object` type, rather than the abstract CS concept. The "Object (CS)" wikipedia article, that you have linked to yourself, provides a definition of an object that value types most certainly meet. They are locations in memory, and they are referenced by an identifier. The definition matches. – Servy Mar 10 '14 at 21:25
  • Value types are data in memory they do not have locations or references in memory. Please go through this link it may help you. http://www.tutorialspoint.com/csharp/csharp_data_types.htm – Sandeep Kumar Mar 10 '14 at 21:51
  • Check this one too and verify yourself before down voting someone. http://stackoverflow.com/questions/9748882/why-data-type-in-java-are-not-object – Sandeep Kumar Mar 10 '14 at 22:47
  • Of course value types have locations in memory. Where else do you expect the data to be stored if not in memory? The fact that values that **reference** them contain the values themselves, and do not reference a reference to some other location in memory that contains the actual data most certainly doesn't mean that they do not have a location in memory, nor does it mean that that memory cannot be referenced by an identifier. Those are both *absolutely* the case for value types. Neither of your links are quality references with it comes to understanding these concepts. – Servy Mar 10 '14 at 23:01
1

Instance is synonymous of object and when we create an object of class then we say that we are creating instance of class

in simple word instance means creating reference of object(copy of object at particular time) and object refer to memory address of class

jp rathore
  • 35
  • 10
1

I would have rather taken a real life example...

stating that "car" is a class, so if i tell you i have a car you will have no clue what kind of car it is. But if tell you that i have Ford Fiesta, 1.6 EXI 2009 model of silver color, then you exactly know my car. So, this is what an instance is.

Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
0

yup, my interpreteation would be to mention that only classes can 'define' instances. or something along those lines, I might mention an example in code, or seek clarification of the question.

Steve Rathbone
  • 458
  • 1
  • 5
  • 14
0

a class is akin to a blueprint while an instance is a concrete implementation of the class/blueprint. An instance is also characterized by its identity, state and behavior.

Athens Holloway
  • 2,183
  • 3
  • 17
  • 26