C# 如何定义实例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2219566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:30:04  来源:igfitidea点击:

How to define an instance?

c#instance

提问by ACP

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#]?
  • 你将如何定义一个实例[c#]?

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

我的回答是它的另一个名字object......这个问题的正确答案是什么......

采纳答案by Adam Says - Reinstate Monica

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

实例之于课堂就像蛋糕之于食谱。任何时候使用构造函数创建对象时,都是在创建实例。

回答by Muad'Dib

MyObject obj = new MyObject( );

MyObject obj = new MyObject();

回答by Nick Craver

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.

我将实例描述为对象的单个副本。可能有一个,也可能有数千个,但一个实例是一个特定的副本,您可以参考它。

回答by Benny

Class is the blueprint, instance is the completed construction.

类是蓝图,实例是完成的构造。

回答by Steve Rathbone

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

是的,我的解释是提到只有可以“定义”实例。或者类似的东西,我可能会在代码中提到一个例子,或者寻求对问题的澄清。

回答by Athens Holloway

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.

类类似于蓝图,而实例是类/蓝图的具体实现。实例的特征还在于其身份、状态和行为。

回答by BillW

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.

“实例”是分配在内存中的对象,通常由编译器指令 'new等),或内置的 .NET 类(如 WinForm ?),或用户定义的类,或 .NET 中的结构;或者,甚至是一个枚举。

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

实例是从某种“某物的抽象计划”中创建的“真实的某物”。

回答by Manish Basantani

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.

声明“汽车”是一个类,所以如果我告诉你我有一辆车,你将不知道它是什么类型的汽车。但是如果告诉你我有福特嘉年华,1.6 EXI 2009 款银色,那么你就知道我的车了。所以,这就是一个实例。

回答by Sandeep Kumar

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

如果我们只考虑类,实例和对象是相同的,但如果我们考虑整个 C#,则不同。实例比对象更通用。

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();

  • 对象是类的实例。

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

我们通过为它分配 new Person() 来获得对象 John。这里 new Person() 首先保留存储其值类型属性及其引用所需的总内存,然后为其属性分配默认值。

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

所以这个“具有默认值的保留内存”被命名为“John”,它是一个类的实例,在 OOP 中称为 OBJECT。

They are different

它们是不同的

  • A variable is an instance of its type.

  • int x = 5;

  • 变量是其类型的实例。

  • 整数 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.

这里一切都一样。x 是内存位置的名称,它的容量正好是 4 个字节,可以存储一个整数。不同的是这里 x 是一个 int 的 INSTANCE 而不是一个对象。

回答by jp rathore

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

简单来说,实例意味着创建对象的引用(特定时间的对象副本),对象引用类的内存地址