结构的新东西 [英] new for struct

查看:47
本文介绍了结构的新东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我想知道什么时候必须使用new用于结构创建。


// ---------案例1


struct S

{

public int I;

}

// ...

S s;

sI = 3; //好的


// ---------案例2


struct S

{

int i;

public int I

{

get {return i;}

set {i = value;}

}

}

// ...

S s;

sI = 3; //错误。使用未分配的局部变量''s''

// ...

S s = new S();

sI = 3 ; //好吧

所以我假设如果一个结构有一个属性我必须使用new。

但这是错误的。

点p;

pX = 3; // OK


X是Point struct的属性。


我何时必须使用new什么时候不需要?


TIA。

Sam

解决方案

< blockquote>在调用任何方法之前,必须显式初始化结构的所有字段。在封面下,属性只是方法(查看IL)。现在问题是大多数结构将保持他们的国家私人(或至少其中一些)。除了运行构造函数之外,您无法显式初始化结构的数据 - 这是结构体的新功能。所以,实际上,如果你正在创建自定义值类型(结构),你几乎总是必须使用new来选择一个构造函数,然后再调用它们的任何方法。


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


您好!


我想知道何时使用"新"用于结构创建。


// ---------案例1


struct S

{

public int I;

}

// ...

S s;

sI = 3; //好的


// ---------案例2


struct S

{

int i;

public int I

{

get {return i;}

set {i = value;}

}

}

// ...

S s;

sI = 3; //错误。使用未分配的局部变量''s''

// ...

S s = new S();

sI = 3 ; //好吧

所以我假设如果一个结构有一个属性我必须使用new。

但这是错误的。

点p;

pX = 3; // OK


X是Point struct的属性。


我何时必须使用new什么时候不需要?


TIA。

Sam





你可以使用new,因为使用struct运算符new只是将

字段值初始化为零/ null。所以你可以调用new或手动初始化

字段。


-

问候,

Peter Jausovec

http://blog.jausovec.net


Sam Sungshik Kong < ss*@chol.nospam.net>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...

你好!

我想知道何时必须使用new用于结构创建。

// ---------案例1

struct S
{
public int I;
}
// ...
S s;
sI = 3; //好的

// ---------案例2

struct S
{
int i;
public int I
{
获得{return i;}
设置{i = value;}
}
}
//...
S s;
sI = 3; //错误。使用未分配的局部变量'''
// ...
S s = new S();
s.I = 3; //好吧

所以我假设如果一个结构有一个属性我必须使用new。
但这是错误的。

点p;
pX = 3; // OK

X是Point struct的一个属性。

我什么时候需要使用new什么时候我不需要?

TIA。
Sam



感谢您的回复。


我想我明白你说的话。但是,我仍然不明白为什么我要点b $ b不要在Point案例中出错。


我应该用它如下所示。


点p =新点();

pX = 3;


但是如果我使用它如下,似乎没问题。


点p;

pX = 3;


我正在读一本书 - 用C#编程Windows应用程序 by Charles

Petzold。

他说新在上述情况下是必要的。

否则,会发生错误。

但是我的测试反对它。


有什么想法吗?


谢谢。

Sam


" Richard Blewett [DevelopMentor]" < RI ****** @ NOSPAMdevelop.com>写在

消息新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...

你必须在调用任何方法之前显式初始化结构的所有字段。在幕后,属性只是方法(看看
IL)。现在问题是大多数结构将保持其状态私有(或至少其中一些)。除了运行构造函数之外,您无法显式初始化
结构的数据 - 这是
结构的新功能。所以,实际上,如果你正在创建自定义值类型(结构)
在调用任何方法之前,你几乎总是必须使用new来选择构造函数来运行




Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

您好!

我想知道何时必须使用新用于结构创建。

// ---------案例1

struct S
{
public int I;
}
// ...
S s;
sI = 3; //好的

// ---------案例2

struct S
{
int i;
public int I
{
获得{return i;}
设置{i = value;}
}
}
//...
S s;
sI = 3; //错误。使用未分配的局部变量'''
// ...
S s = new S();
s.I = 3; //好吧

所以我假设如果一个结构有一个属性我必须使用new。
但这是错误的。

点p;
pX = 3; // OK

X是Point struct的一个属性。

我什么时候需要使用new什么时候不需要?

TIA。
Sam



Hello!

I want to know when I have to use "new" for struct creation.

//---------Case 1

struct S
{
public int I;
}
//...
S s;
s.I = 3; //OK

//---------Case 2

struct S
{
int i;
public int I
{
get {return i;}
set {i = value;}
}
}
//...
S s;
s.I = 3; //ERROR. Use of unassigned local variable ''s''
//...
S s = new S();
s.I = 3; //OK
So I assumed that if a struct has a property I have to use "new".
But this was wrong.

Point p;
p.X = 3; //OK

X is a property of Point struct.

When do I have to use "new" and when do I not need to?

TIA.
Sam

解决方案

You must explicitly initialize all fields of a struct before you invoke any methods. Properties are, under the covers, just methods (look at the IL). Now the problem is most structs will keep their state private (or at least some of it). You you cannot explicitly initialize the data for the struct other than by running a constructor - which is what new does for a struct. So, in reality, ifd you are creating custom value types (structs) you will pretty much always have to use new to select a constructor to run before you invoke any methods on them.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hello!

I want to know when I have to use "new" for struct creation.

//---------Case 1

struct S
{
public int I;
}
//...
S s;
s.I = 3; //OK

//---------Case 2

struct S
{
int i;
public int I
{
get {return i;}
set {i = value;}
}
}
//...
S s;
s.I = 3; //ERROR. Use of unassigned local variable ''s''
//...
S s = new S();
s.I = 3; //OK
So I assumed that if a struct has a property I have to use "new".
But this was wrong.

Point p;
p.X = 3; //OK

X is a property of Point struct.

When do I have to use "new" and when do I not need to?

TIA.
Sam


Hi,

You can use new, because with struct the operator new just initializes the
field values to zero/null. So you can call new or manually initialize the
fields.

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

Hello!

I want to know when I have to use "new" for struct creation.

//---------Case 1

struct S
{
public int I;
}
//...
S s;
s.I = 3; //OK

//---------Case 2

struct S
{
int i;
public int I
{
get {return i;}
set {i = value;}
}
}
//...
S s;
s.I = 3; //ERROR. Use of unassigned local variable ''s''
//...
S s = new S();
s.I = 3; //OK
So I assumed that if a struct has a property I have to use "new".
But this was wrong.

Point p;
p.X = 3; //OK

X is a property of Point struct.

When do I have to use "new" and when do I not need to?

TIA.
Sam



Thanks for the reply.

I think I understand what you said. However, I still don''t understand why I
don''t get an error in Point case.

I''m supposed to use it like the following.

Point p = new Point();
p.X = 3;

But if I use it like the following, it seems to be ok.

Point p;
p.X = 3;

I''m reading a book - "Programming Windows Application in C#" by Charles
Petzold.
He said that "new" is a necessary in the above case.
Otherwise, an error will occur.
But my test is against it.

Any idea?

Thanks.
Sam

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...

You must explicitly initialize all fields of a struct before you invoke
any methods. Properties are, under the covers, just methods (look at the
IL). Now the problem is most structs will keep their state private (or at
least some of it). You you cannot explicitly initialize the data for the
struct other than by running a constructor - which is what new does for a
struct. So, in reality, ifd you are creating custom value types (structs)
you will pretty much always have to use new to select a constructor to run
before you invoke any methods on them.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hello!

I want to know when I have to use "new" for struct creation.

//---------Case 1

struct S
{
public int I;
}
//...
S s;
s.I = 3; //OK

//---------Case 2

struct S
{
int i;
public int I
{
get {return i;}
set {i = value;}
}
}
//...
S s;
s.I = 3; //ERROR. Use of unassigned local variable ''s''
//...
S s = new S();
s.I = 3; //OK
So I assumed that if a struct has a property I have to use "new".
But this was wrong.

Point p;
p.X = 3; //OK

X is a property of Point struct.

When do I have to use "new" and when do I not need to?

TIA.
Sam



这篇关于结构的新东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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