struct member initialization - 成员成员?! [英] struct member initialization - member by member?!

查看:79
本文介绍了struct member initialization - 成员成员?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我发现如果你声明一个结构(而不​​是''new()''你)

然后可以单独初始化它成员和编译器将这些单独的语句计为

作为完全初始化。这让我印象深刻,因为我认为它只会让人感到困扰

关于''追踪'''原子''/''完整' '(通过''new()''初始化

没有做到''咬它''!!!任何意见?似乎有点开销

跟踪成员初始化不是吗?任何C#编译人员

在那里?


希望得到一些评论告诉我这不是一件奇怪的事情

please。


Emma Middlebrook
em**************@fastmail.fm


struct Value

{

public int x;

public int y;

}


价值v2;

v2.x = 6; // v2不能用作''未分配''...

v2.y = 7; // v2现在可用于其所有部件都已初始化。

解决方案

emma middlebrook< em ********** ****@fastmail.fm>写道:

我发现如果你声明一个结构(而不​​是''new()''它)你可以单独初始化它的成员和编译器计数
那些将单独的语句作为完整初始化。这让我感到有点奇怪,因为我原本以为它只会打扰'跟踪'''原子''/''完全''(通过''新的'( )'')初始化
不做它''咬它''!!!任何意见?似乎有点开销跟踪成员初始化不是吗?任何C#编译人员都在那里?




为什么它会成为开销?或者更确切地说,为什么你关心仅在* compile * time存在的

开销?我宁愿编译器

跟踪一些额外的东西以防止错误,如果它在运行时没有花费额外的时间。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet/

如果回复小组,请做不要给我发邮件


如果你查看文档和规格..你会看到它说的是

''结构未初始化无法访问或使用''。这个

并不意味着运行时会进行任何类型的跟踪,实际上,编译器会明确地给你一个消息,说明有错误并且

不会编译(直到你在使用之前初始化结构数据)。

所以..它只检查访问结构数据时的有效性。请看下面的示例程序




public struct Point

{

public int x,y;


public Point(int p1,int p2)

{

x = p1;

y = p2;

}

}


class MyClass {

static void Main(string args []){

Point p;

Console.WriteLine(" X is:{0}",px);


//编译器将执行检查并将中止..


px;

Console.WriteLine(" X is :{0}",px);


/ *

如果您注释掉前面的语句,程序将会运行并且

编译。请注意,我没有初始化y,它仍然有效。这显示

编译器没有检查''每个''字段'未初始化

但仅在访问字段时* / < br $>

}

}

-Andre


emma middlebrook写道:



我发现如果你声明一个结构(而不​​是''new()''它)你可以单独初始化它的成员和编译器计数
那些单独的语句作为完全初始化。这让我感到有点奇怪,因为我原本以为它只会打扰'跟踪'''原子''/''完全''(通过''新的'( )'')初始化
不做它''咬它''!!!任何意见?似乎有点开销跟踪成员初始化不是吗?有没有C#编译人员在那里?

希望有一些评论告诉我这不是一件奇怪的事情
请。

Emma Middlebrook
em ************** @ fastmail.fm

结构价值
{
public int x;
public int y;
}

>价值v2;
v2.x = 6; // v2不能用作''未分配''...
v2.y = 7; //现在可以使用// v2,因为它的所有部分都已初始化。




正好 - 另一个好点:)


-Andre


Jon Skeet写道:

emma middlebrook< em ************ **@fastmail.fm>写道:

我发现如果你声明一个结构(而不​​是''new()''它)你可以单独初始化它的成员和编译器计数
那些单独的语句作为完全初始化。这让我感到有点奇怪,因为我原本以为它只会打扰'跟踪'''原子''/''完全''(通过''新的'( )'')初始化
不做它''咬它''!!!任何意见?似乎有点开销跟踪成员初始化不是吗?任何C#编译人员都在那里?



为什么会是开销?或者更确切地说,为什么你关心仅在* compile *时存在的开销?我宁愿编译器跟踪一些额外的东西来防止错误,如果它在运行时没有花费任何额外的时间。



Hi

I discovered that if you declare a structure (and not ''new()'' it) you
can then separately initialize its members and the compiler counts
those separate statements as a full initialization. That struck me as
a bit odd really as I would have thought it would have only bothered
about ''tracking'' an ''atomic''/''complete'' (via ''new()'') initialization
not doing it ''bit by it''!!! Any comments? Seems a bit of an overhead
tracking memberwise initialization doesn''t it? Any C# compiler people
out there?

Hoping for some comments to show me that this isn''t such a weird thing
please.

Emma Middlebrook
em**************@fastmail.fm

struct Value
{
public int x;
public int y;
}

Value v2;
v2.x = 6; // v2 can not be used yet as ''unassigned'' ...
v2.y = 7; // v2 can now be used as all its parts are initialized.

解决方案

emma middlebrook <em**************@fastmail.fm> wrote:

I discovered that if you declare a structure (and not ''new()'' it) you
can then separately initialize its members and the compiler counts
those separate statements as a full initialization. That struck me as
a bit odd really as I would have thought it would have only bothered
about ''tracking'' an ''atomic''/''complete'' (via ''new()'') initialization
not doing it ''bit by it''!!! Any comments? Seems a bit of an overhead
tracking memberwise initialization doesn''t it? Any C# compiler people
out there?



Why would it be an overhead? Or rather, why do you care about the
overhead which only exists at *compile* time? I''d rather the compiler
kept track of a few extra things to prevent bugs, if it doesn''t take
any extra time at runtime.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


If you go through the documentation and specs.. you''ll see that it says
''structs that are not initilized cannot be accessed or used''. This
doesn''t imply that the runtime would do any sort of tracking, in fact,
the compiler will plainly give you a message that there''s an error and
won''t compile (till you''ve initialized struct data before using it).
So.. it only checks for validity upon struct data being accessed. Have a
look at the following sample program:

public struct Point
{
public int x, y;

public Point(int p1, int p2)
{
x = p1;
y = p2;
}
}

class MyClass {
static void Main(string args[]) {
Point p;
Console.WriteLine("X is: {0}", p.x);

// Compiler will perform a check hede and will abort..

p.x;
Console.WriteLine("X is: {0}", p.x);

/*
if you comment out the preceding statement, the program will work and
compile. Notice I didn''t initialize y and it still worked. This shows
that the compiler doesn''t check for ''every'' field that''s not initialized
but only when a field is being accessed */

}
}
-Andre

emma middlebrook wrote:

Hi

I discovered that if you declare a structure (and not ''new()'' it) you
can then separately initialize its members and the compiler counts
those separate statements as a full initialization. That struck me as
a bit odd really as I would have thought it would have only bothered
about ''tracking'' an ''atomic''/''complete'' (via ''new()'') initialization
not doing it ''bit by it''!!! Any comments? Seems a bit of an overhead
tracking memberwise initialization doesn''t it? Any C# compiler people
out there?

Hoping for some comments to show me that this isn''t such a weird thing
please.

Emma Middlebrook
em**************@fastmail.fm

struct Value
{
public int x;
public int y;
}

Value v2;
v2.x = 6; // v2 can not be used yet as ''unassigned'' ...
v2.y = 7; // v2 can now be used as all its parts are initialized.




Exactly - another good point made :)

-Andre

Jon Skeet wrote:

emma middlebrook <em**************@fastmail.fm> wrote:

I discovered that if you declare a structure (and not ''new()'' it) you
can then separately initialize its members and the compiler counts
those separate statements as a full initialization. That struck me as
a bit odd really as I would have thought it would have only bothered
about ''tracking'' an ''atomic''/''complete'' (via ''new()'') initialization
not doing it ''bit by it''!!! Any comments? Seems a bit of an overhead
tracking memberwise initialization doesn''t it? Any C# compiler people
out there?


Why would it be an overhead? Or rather, why do you care about the
overhead which only exists at *compile* time? I''d rather the compiler
kept track of a few extra things to prevent bugs, if it doesn''t take
any extra time at runtime.




这篇关于struct member initialization - 成员成员?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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