您好我不理解在C#中使用new关键字 [英] Hello I am not understanding the use of new keyword in C#

查看:75
本文介绍了您好我不理解在C#中使用new关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我不理解在C#中使用new关键字

推荐答案

C#中'new关键字的主要用途是为某种类型的结构分配内存,然后创建一个指针......一个地址的引用...使任何拥有该地址的人能够访问组成对象的有组织的内存部分。



因此,'new创建一个'对象的实例,从该对象的代码规范中可以描述一个Class,一个Struct和某些类型的基本对象。 ..参考值...存储在堆中的内存...像数组,通用列表等。 [1]



考虑类定义:
The major use of the 'new keyword in C# is to allocate memory for some type of structure, and then create a pointer ... a reference ... to the address that enables anyone with that "address" to access that organized section of memory that makes up an 'object.

So, 'new creates an instance of an 'object, from that object's specification in code which can be the description of a Class, a Struct, and certain kinds of fundamental objects ... reference values ... that are stored in memory in a Heap ... like Array, generic Lists, etc. [1]

Consider the Class definition:
public class AClass
{
    public int anInt;
    public string aString;
    
    public AClass(int i, string s)
    {
        anInt = i;
        aString = s;
    }
}

您可以将类定义视为模板,蓝图或配方;当您创建类的实例时:

You can consider the class definition a template, or a blue-print, or a recipe; when you create an instance of the class:

AClass aClass1 = new AClass(100, "hello");

.NET运行时引擎执行编译器从解析模板到CIL代码生成的CIL代码,分配内存以保存封装在类中的每种信息类型的每个实例...在这种情况下,一个'int,one'字符串。



使用对'变量'aClass1绑定的'AClass实例的引用,然后我们可以取消引用(访问)指针(引用)来获取或设置,变量'anInt指向的实例的整数值,以及变量'aString指向的字符串值。



评估结果'new是返回指针...引用...到创建的类'AClass的特定实例。



你可能想知道为什么某些类型的可以创建.NET对象,如Integer和String,直接分配给而不使用使用'new:那些类型被称为'值类型,它们以不同的方式存储在内部(在Stack)比参考类型。 '字符串是一种特殊情况,因为它是一个引用类型,但它的数据(字符)存储在一个堆中,并且,如您所知,可以创建赋值为一个值一个简单的声明。



关键字'new的一些其他特殊用途包括在一个类中用作特殊类型的访问成员,用信号通知编译器方法在Class继承的类中重写了一个定义。而且,在泛型中更为特殊的用法,其中'new可以用作类定义中的约束条件。



[1] 关于堆和堆栈存储的高级讨论,请参阅Eric Lippert:[ ^ ]。

The .NET run-time engine executes the CIL code produced by the compiler from its parsing of the template into CIL code, allocating memory to hold each instance of each Type of information encapsulated in the Class ... in this case one 'int, one 'string.

Using the reference to the instance of 'AClass bound to the variable 'aClass1, we can then dereference (access through) the pointer (reference) to get, or set, the integer value of the instance pointed-to by the variable 'anInt, and the string value pointed-to by the variable 'aString.

The result of the evaluation of 'new is to return a pointer ... a reference ... to the particular instance of the Class 'AClass created.

You may wonder why certain Types of .NET objects, like Integer and String can be created, and assigned to directly, without the use of 'new: those Types are called 'Value Types and they are stored in a different way internally (on a Stack) than are reference Types. 'String is kind of a special case since it is a reference Type, but its data (characters) are stored on a Heap, and, as you know, can be created and assigned-a-value in one simple declaration.

There are some other special uses of the keyword 'new including being used as a special type of access member in a Class signalling the compiler that a certain method over-rides a definition in a class the Class inherits from. And, an even more special use in generics, where 'new can be used as a constraint qualification in the definition of a Class.

[1] For advanced discussion of Heap and Stack storage see Eric Lippert: [^].


这篇关于您好我不理解在C#中使用new关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆