结构和'使用' [英] structs and 'using'

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

问题描述

C#语言专家的问题:


如果我有一个实现IDisposable的结构:


struct C:IDisposable

{

public int clan;

public void Dispose()

{

控制台。 WriteLine(" Disposing");

}

}

稍后在代码中类似:

使用(C s = new C())

{

s.clan = 1;

}

报告编译器错误:

错误CS0131:作业的左侧必须是变量,

属性或索引器


为什么会发生这种情况?

A question for C# language experts:

If I have a struct that implements IDisposable:

struct C : IDisposable
{
public int clan;
public void Dispose()
{
Console.WriteLine("Disposing");
}
}
And later in the code something like:
using (C s = new C())
{
s.clan = 1;
}
A compiler error is reported:
error CS0131: The left-hand side of an assignment must be a variable,
property or indexer

Why does that happen?

推荐答案

结构正在装箱我相信,因此生成错误是因为

你永远无法用该行为原始结构赋值

代码。


" Nemanja Trifunovic"写道:
the struct is being boxed I believe, therefore the error is generated because
you''d never been able to assign value to the original struct with that line
of code.

"Nemanja Trifunovic" wrote:
C#语言专家的问题:

如果我有一个实现IDisposable的结构:

struct C:IDisposable
{public int clan;
public void Dispose()
{
Console.WriteLine(" Disposing");
}
}

稍后在代码中使用(
使用(C s = new C())
{
s.clan = 1;
}

报告编译器错误:
错误CS0131:作业的左侧必须是变量,
属性或索引器

为什么会发生这种情况?
A question for C# language experts:

If I have a struct that implements IDisposable:

struct C : IDisposable
{
public int clan;
public void Dispose()
{
Console.WriteLine("Disposing");
}
}
And later in the code something like:
using (C s = new C())
{
s.clan = 1;
}
A compiler error is reported:
error CS0131: The left-hand side of an assignment must be a variable,
property or indexer

Why does that happen?



当使用时,结构确实会被装箱。声明获得IDisposable接口的引用

。另一方面,变量s表示变量s。应该是

原始的未装箱的结构,因此它应该可以做什么是

。我说这是一个错误。特别是因为这将起作用:


C s =新C();


使用(s)

{

s.clan = 1;

}


问候,

Daniel


Daniel Jin <沓******* @ discussions.microsoft.com>在消息中写道

新闻:9D ********************************** @ microsof t.com ...
The struct will indeed get boxed when the "using" statement gets a reference
to the IDisposable interface. On the other hand, variable "s" should be the
original unboxed struct and therefore it should be possible to do what is
being done. I say it''s a bug. Especially since this will work:

C s = new C();

using (s)
{
s.clan = 1;
}

Regards,
Daniel

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:9D**********************************@microsof t.com...
结构被装箱我相信,因此错误产生
,因为你从来没有能够用$ b为原始结构赋值$ b行代码。

Nemanja Trifunovic写道:
the struct is being boxed I believe, therefore the error is generated because you''d never been able to assign value to the original struct with that line of code.

"Nemanja Trifunovic" wrote:
C#语言专家的问题:

如果我有一个实现IDisposable的结构:

struct C:IDisposable
{public int clan;
public void Dispose()
{
Console.WriteLine(" Disposing");
}
}

稍后在代码中使用(
使用(C s = new C())
{
s.clan = 1;
}

报告编译器错误:
错误CS0131:作业的左侧必须是变量,
属性或索引器

为什么会这样?
A question for C# language experts:

If I have a struct that implements IDisposable:

struct C : IDisposable
{
public int clan;
public void Dispose()
{
Console.WriteLine("Disposing");
}
}
And later in the code something like:
using (C s = new C())
{
s.clan = 1;
}
A compiler error is reported:
error CS0131: The left-hand side of an assignment must be a variable,
property or indexer

Why does that happen?



嗯...


我没有快速回答这个问题。为了测试这个,我写了一个简单的

程序并将其拆解。代码应扩展为以下内容:


C s;

s = new C();

try {s.clan = 1} finally {s.Dispose(); }


如果您手动编码而不是利用使用声明,此代码

编译好。必须有一些事实,即C是一个结构与一个

类(尝试将stuct更改为class以查看一切正常运行

原创)。另外,即使你没有从IDispose继承(或者

其他任何东西,为此而且让结构保持一个简单的vanilla结构),编译器仍然抱怨



我也会对这个回复感兴趣...


John


Nemanja Trifunovic < NT ********* @ hotmail.com>在消息中写道

news:82 ************************** @ posting.google.c om ...
Hmm...

I do not have a quick answer to this. To test this, I wrote a simple
program and disassembled it. The code should expand to the following:

C s;
s = new C();
try { s.clan =1 } finally { s.Dispose(); }

If you hand-code this instead of leveraging the "using" statement, this code
compiles fine. There must be something to the fact that C is a struct vs. a
class (try changing "stuct" to "class" to see everything work fine in your
original). In addition, even if you do not inherit from IDispose (or
anything else, for that matter and leave the struct a plain vanilla struct),
the compiler still complains.

I''d be interested in the reply as well...

John

"Nemanja Trifunovic" <nt*********@hotmail.com> wrote in message
news:82**************************@posting.google.c om...
C#语言专家的问题:

如果我有一个实现IDisposable的结构:

struct C:IDisposable
{
public int clan;
public void Dispose()
{
Console.WriteLine(" Disposing");
}
}

稍后在代码中使用类似的东西:
使用(C s = new C())
{
s.clan = 1;
}

报告编译器错误:
错误CS0131:作业的左侧必须是变量,
属性或索引器

为什么会发生这种情况?
A question for C# language experts:

If I have a struct that implements IDisposable:

struct C : IDisposable
{
public int clan;
public void Dispose()
{
Console.WriteLine("Disposing");
}
}
And later in the code something like:
using (C s = new C())
{
s.clan = 1;
}
A compiler error is reported:
error CS0131: The left-hand side of an assignment must be a variable,
property or indexer

Why does that happen?



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

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