开发者

How to define an instance?

开发者 https://www.devze.com 2022-12-19 06:06 出处:网络
I was asked a question in an 开发者_Go百科interview and i wasn\'t able to answer it... Here is the question

I was asked a question in an 开发者_Go百科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...


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


MyObject obj = new MyObject( );


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.


Class is the blueprint, instance is the completed construction.


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."


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.


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消