structs and 'using'
-
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
:confused:
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
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
:confused:
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
The problem lies with the fact that structs cant be safely casted, in this case IDisposable. The compiler will typical generate the following for the 'using' block:
IDisposable o = new C();
try
{
C s = o as C; //this is not allowable for structs,
// lets assume it would then it would go out of scope before Dispose can be called
// your code here
}
finally
{
o.Dispose();
}Anyways, my best guess :p top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
The problem lies with the fact that structs cant be safely casted, in this case IDisposable. The compiler will typical generate the following for the 'using' block:
IDisposable o = new C();
try
{
C s = o as C; //this is not allowable for structs,
// lets assume it would then it would go out of scope before Dispose can be called
// your code here
}
finally
{
o.Dispose();
}Anyways, my best guess :p top secret
Download xacc-ide 0.0.3 now!
See some screenshotsHmmmm, but using
as
with structs causeserror CS0077: The as operator must be used with a reference type ('sharptest.C' is a value type)
And my code complains about assigning a value to a member of s. Also, this compiles (and works) fine:
using (C s = new C())
{
Console.WriteLine(s.clan.ToString());
}
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
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
:confused:
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
If U have the struct in the same namespace as ur code the code below works. C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); and if in different namespace -- in that case using can follow the initial using block. I mean immediately after using System; using System.Data; .... using NameSpaceName.C; C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); I hope this help and that i have not missunderstood ur question.:rolleyes: Ketty
-
If U have the struct in the same namespace as ur code the code below works. C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); and if in different namespace -- in that case using can follow the initial using block. I mean immediately after using System; using System.Data; .... using NameSpaceName.C; C c = new C(); c.clan = 1; Console.WriteLine(c.clan.ToString()); I hope this help and that i have not missunderstood ur question.:rolleyes: Ketty
Ketty Avashia wrote: I hope ... that i have not missunderstood ur question. Thanks, but you have :) I asked about using Statement[^], not using Directive[^].
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
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
:confused:
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
According to the C# language spec[^]: Local variables declared in a resource-acquisition are read-only, and must include an initializer. A compile-time error occurs if the embedded statement attempts to modify these local variables (by assignment or the ++ and -- operators) or pass them as ref or out parameters. Wow!
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
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
:confused:
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Structures are allocated on the stack and not on the heap as classes. Write the following code instead, it does the same trick: C s = new C(); using (s) { s.clan = 1; } Ami
Ami Bar wrote: Structures are allocated on the stack and not on the heap as classes. That is correct in this context - I just didn't know that variables declared in using nlock need to be read-only. BTW, what exactly "read-only" really means in C#? It does not have const methods like C++. Whatever... Thanks for your answer :)
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Ami Bar wrote: Structures are allocated on the stack and not on the heap as classes. That is correct in this context - I just didn't know that variables declared in using nlock need to be read-only. BTW, what exactly "read-only" really means in C#? It does not have const methods like C++. Whatever... Thanks for your answer :)
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
C# doesn't have const and its a petty. "The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.", MSDN Here is the link to the full readonly keyword explanation in the MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfreadonlypg.asp