[RESOLVED] Use {} to limit scope?
Hi,
Is it an accepted use of brackets if I do something like this inside a method?
Code:
string[] strs;
{
object SomethingHeavy = new HeavySomething();
strs = HeavyToString(SomethingHeavy);
}
//use strs
I figured I could use this way to get rid off the SomethingHeavy object in stead of using .Dispose() and I wouldn't have a reference to it anymore.
I haven't seen it in other people's C# code so I was just wondering if it would look akward to pro's like you all are ofcourse.
:)
Re: Use {} to limit scope?
Anything wrong with:
Code:
string[] strs = HeavyToString(new HeavySomething());
?
Re: Use {} to limit scope?
Absolutely not :D but I have done it just a few times were a multitude of objects needed to be instanciated for a short time, I wanna know if it makes sence to people.
Edit: bare in mind I'm a total amateur. :bigyello:
Re: Use {} to limit scope?
It was my understanding that objects were destroyed once they went out of scope, or at routine garbage collection, if there were no longer any references to them. If you use the one-liner, you don't actually create any references, so I'd take it to be the same as your code.
Re: Use {} to limit scope?
You should always use Dispose() if an object has it, its good practice as it makes it obvious what is going on. Use of {} to limit scope is perfectly OK. After all you di it after most of your If() statements anyway...
If(whatever)
{
//blah
}
Re: Use {} to limit scope?
if you're talkign about disposing objects when the y go out of scope, then use the "using" statement
Re: Use {} to limit scope?
Quote:
Originally Posted by MrPolite
if you're talkign about disposing objects when the y go out of scope, then use the "using" statement
I thought there was something like that but I couldn't find reference to it in the help. Does "using" only let you specify one object for the block or more?
Re: Use {} to limit scope?
MSDN has all the answers. :D
Re: Use {} to limit scope?
I'm sorry but lol. Pointing MSDN out to jmcilhinney....
..
.
Re: Use {} to limit scope?
Quote:
Originally Posted by wossname
MSDN has all the answers. :D
So it does. Who would have thought? :) I figured that it would be no easier to find on MSDN online than via VS.NET help, but it seems I was wrong. I still can't find it in the help though, which seems strange, although I'm sure it must be there somewhere.
Re: Use {} to limit scope?
Quote:
Originally Posted by grilkip
I'm sorry but lol. Pointing MSDN out to jmcilhinney....
..
.
Ridicule me all you like but... hang on... no... stop ridiculing me... nooooooooooooo!
Re: Use {} to limit scope?
Quote:
Originally Posted by jmcilhinney
Ridicule me all you like but... hang on... no... stop ridiculing me... nooooooooooooo!
hehe n'ing bu' respec' here
Thanks for the answers, I guess it is acceptable but not a common practice.
Re: Use {} to limit scope?
haha I thought for a sec this was chitchat and I was like "jmc posts in chit chat too?:D"
http://msdn.microsoft.com/library/de...pspec_8_13.asp