Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# put methods inside using statement possible?

Tags:

c#

.net

im trying some different approach. Im not sure if its possible to place using() statement above methods or is there other way around.

public class Main 
{
    public Main() 
    {
      using(Type t = new Type) 
      {
        public void SomeFunction() {
             t.toString()}
      }
    }
}
like image 833
Joshua Belarmino Avatar asked Oct 26 '25 19:10

Joshua Belarmino


1 Answers

That is not possible, but you can do that like this:

 using(Type t = new Type) 
 {
    SomeFunction(t);
 }

 public void SomeFunction(Type tType) 
 {
    tType.ToString();
 }

Note : The t will be transferred to SomeFunction() and will be disposed at } of using block if the Class Type implements IDisposible, Since Using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called

like image 151
sujith karivelil Avatar answered Oct 29 '25 07:10

sujith karivelil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!