构造一个具有基于值语义的轻量级类型? [英] Struct a lightweight class type having value based semantics?

查看:57
本文介绍了构造一个具有基于值语义的轻量级类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过c#订购了'cli',同时我正在阅读'Pro C#

2005和.NET平台''(Andrew Troelson)。我只是在阅读CTS中定义的五种类型中的b $ b。特别是结构。现在Troelson

将结构类型描述为具有价值的轻量级类型

基于语义。


查看他的例子我看不出这里有什么区别

除了用struct关键字定义了吗?这两者之间的差价是多少?


另外,Troelson说类的类型是'基于价值的

语义''这是什么意思?


非常感谢你的帮助。


Gary-


// AC#结构类型。

struct Point

{

//结构可以包含字段。

public int xPos,yPos;

//结构可以包含参数化构造函数。

public Point(int x,int y)

{ xPos = x; yPos = y;}

//结构可以定义方法。

public void显示()

{

Console.WriteLine("({0},{1}",xPos,yPos);

}

}

I have ''cli via c# on order'', and in the mean time am reading ''Pro C#
2005 and the .NET platform'' (Andrew Troelson). I''m just reading about
the ''five types defined in the CTS''. Specifically Struct. Now Troelson
described the struct type as ''a lightweight class type having value
based semantics''.

Looking at his example I cant see any difference from a class here
other than it is defined with the struct keyword? what is the
difference between these two?

Also, Troelson speaks of the class type as having ''value based
semantics'' what does that mean?

Thanks very much for your help.

Gary-

// A C# structure type.
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain parameterized constructors.
public Point(int x, int y)
{ xPos = x; yPos = y;}
// Structures may define methods.
public void Display()
{
Console.WriteLine("({0}, {1}", xPos, yPos);
}
}

推荐答案

Noooo ....不再.......--


Jon在这里给出答案:
http://www.yoda.arachsys.com/ csharp / ... html #preamble2


C#结构的简短答案通常只能代表

简单,不可变的值 ;,像一个整数,一个复数等。你

永远不会改变它们;你只需指定一个新的。它们倾向于克隆

自己几乎随意(作为直接memcopy),并且可以存在于

堆栈(对于变量)或托管堆(如果是类中的字段)。它们

也应该很小(支持rt高效的memcopy)。


你需要在C#中创建一个结构,这是非常罕见的。


Marc
Noooo.... not again..... ;-p

Jon gives an answer here:
http://www.yoda.arachsys.com/csharp/...html#preamble2

The short answer it that C# structs should really only generally represent
simple, immutable "values", like "an integer", "a complex number" etc. You
never change them; you simply assign a new one. They tend to clone
themselves almost at will (as a direct memcopy), and can live either on the
stack (for a variable) or the managed heap (if a field inside a class). They
should also be small (to support efficient memcopy).

It is *very* rare that you need to create a struct in C#.

Marc


值类型实际上是内存区域的标签。引用类型

是指针。


因此,值类型中的值可以直接访问,并且很快就可以获得
;并且这样的数据结构可以在堆栈上分配内存。


引用类型总是必须通过间接层,并且始终在堆上分配内存
。因此,它们必须被垃圾收集。


显然,值类型不能为NULL,因为在它们定义的内存区域总会有一些东西,即使它是垃圾。

参考类型,同样显然,如果它们没有指向内存中的任何区域,那么它们可以是NULL。这可能会导致数据结构出现问题,例如日期

(无论如何都是.NET 1.1),它们是值类型,因此不能为null。

如果无效日期已被分配到日期类型,它将包含一些

默认值,其中,IIRC,类似11-11-1111,或者什么

同样无益 - 可以让很难在一个

数据库日期字段中存储一个NULL值。


比我更了解的人可以纠正并扩展

以上。


HTH

彼得


< ga ***** ***@myway.com写信息

新闻:11 ********************* @ f1g2000cwa.googlegrou ps.com .. 。
Value types are, effectively, labels for areas of memory. Reference types
are pointers.

Therefore the values in value types can be accessed directly and very
quickly; and such data structure can be allocated memory on the stack.

Reference types always have to go through an indirection layer, and always
allocate memory on the heap. They therefore have to be garbage collected.

Obviously, value types cannot be NULL, because there will always be
something in the area of memory they define, even if it''s garbage.
Reference types, equally obviously, can be NULL, if they aren''t pointing to
any area in memory. This can cause problems with data structures like dates
(in .NET 1.1 anyway), which are value types, and therefore can''t be null.
If no valid date has been assigned to a Date type, it will contain some
default value which, IIRC, is something like 11-11-1111, or something
equally unhelpful - which can make it hard to store a NULL value in a
database Date field.

People more knowledgable than I am can probably correct and expand on the
above.

HTH
Peter

<ga********@myway.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...

>我通过c#订购了''cli',同时我正在阅读''Pro C#

2005年和.NET平台''(Andrew Troelson)。我只是在阅读CTS中定义的五种类型中的b $ b。特别是结构。现在Troelson

将结构类型描述为具有价值的轻量级类型

基于语义。


查看他的例子我看不出这里有什么区别

除了用struct关键字定义了吗?这两者之间的差价是多少?


另外,Troelson说类的类型是'基于价值的

语义''这是什么意思?


非常感谢你的帮助。


Gary-


// AC#结构类型。

struct Point

{

//结构可以包含字段。

public int xPos,yPos;

//结构可以包含参数化构造函数。

public Point(int x,int y)

{ xPos = x; yPos = y;}

//结构可以定义方法。

public void显示()

{

Console.WriteLine("({0},{1}",xPos,yPos);

}

}
>I have ''cli via c# on order'', and in the mean time am reading ''Pro C#
2005 and the .NET platform'' (Andrew Troelson). I''m just reading about
the ''five types defined in the CTS''. Specifically Struct. Now Troelson
described the struct type as ''a lightweight class type having value
based semantics''.

Looking at his example I cant see any difference from a class here
other than it is defined with the struct keyword? what is the
difference between these two?

Also, Troelson speaks of the class type as having ''value based
semantics'' what does that mean?

Thanks very much for your help.

Gary-

// A C# structure type.
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain parameterized constructors.
public Point(int x, int y)
{ xPos = x; yPos = y;}
// Structures may define methods.
public void Display()
{
Console.WriteLine("({0}, {1}", xPos, yPos);
}
}



ga ******** @ myway.com 写道:

我已经通过c#订购了'cli',同时我正在阅读''Pro C#

2005和.NET平台''(Andrew Troelson)。我只是在阅读CTS中定义的五种类型中的

。特别是Struct.Now Troelson

将结构类型描述为具有值的轻量级类型

基于语义''。
I have ''cli via c# on order'', and in the mean time am reading ''Pro C#
2005 and the .NET platform'' (Andrew Troelson). I''m just reading about
the ''five types defined in the CTS''. Specifically Struct. Now Troelson
described the struct type as ''a lightweight class type having value
based semantics''.



那是''不幸的是,因为它不是一个班级...我不确定

轻量级是否也是一个很好的描述(完全取决于

关于所涉及的类型)。


结构和类之间的主要区别在于结构是

值类型和类是引用类型。我意识到这并没有单独提供帮助,但希望这些文章将有助于进一步解释

的事情:
http://www.pobox.com/~skeet/csharp/parameters.html


http: //www.pobox.com/~skeet/csharp/memory.html


(我还在撰写一篇关于* * *此事的文章.. 。)


Jon

That''s unfortunate, as it''s not a class... I''m not sure that
"lightweight" is really a good description either (it entirely depends
on the types involved).

The primary difference between a struct and a class is that structs are
value types and classes are reference types. I realise that doesn''t
help on its own, but hopefully these articles will help to explain
things further:
http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

(I''m still working on an article about *just* this matter...)

Jon


这篇关于构造一个具有基于值语义的轻量级类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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