Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,016 members, 7,818,004 topics. Date: Sunday, 05 May 2024 at 03:37 AM

C# Programmers! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / C# Programmers! (890 Views)

C# Programmers, Solve This Question / C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! / for beginner C++ Programmers (2) (3) (4)

(1) (Reply) (Go Down)

C# Programmers! by lordbenax(m): 6:03pm On Nov 14, 2008
Yo! C# programmers, i have a little problem. Though C# is not my primary programming langauge, I know C# a little. I av also bin able to convert a few C# codes to vb.net and java. But there is a key word i keep coming across and i don't know its function in C#. it is the 'using' keyword. I know like in C++ it is used for class reference but i come across it in cases like this



public void method(){
,
using(Dogder rr = new Dogder){
//implementation
}
,
}


I would like the C# programmers to help me out! I want to know that keyword's function in instances like this!
Thanks!!
Re: C# Programmers! by Kobojunkie: 6:34pm On Nov 14, 2008
the Using Statement applies to objects that implement IDisposable interface, and is similar to the Try_catch Or Try_catch_Finally block in C#. The Using Statement allows for the compiler to handle the creation and disposal of the object, as well as exception issues implicitly.


ie. Code 1 and 2 Below are similar and perform the same function.

Code 1




public void method()
{
using(Dogder rr = new Dogder){
//implementation
}

}



Code 1


public void method(){
Try
{
    Dogder rr = new Dogder();
    // Additional code needs
}
Catch
{
   // Add exception handling code here

}
}

}[/b]
Re: C# Programmers! by lordbenax(m): 7:07pm On Nov 14, 2008
Thanks Kobojunkie! It is so unbelievable that that function is just similar to the TRY CATCH STATEMENT. That means i can use Try Catch and just dispose the object myself. How revealing!
Re: C# Programmers! by Kobojunkie: 11:18pm On Nov 14, 2008
You don't need to dispose of the object yourself as the CLR's garbage collector does a good job of that.

But should you have to create an object that you would want disposed specially, then you can implement the Idisposable interface, use the Using statement and that prompts the GC when the scope is exited. Feel free to use whichever method you want to.

Note: the actual disposal of objects is handled implicity by the GC regardless of which method you choose.

(1) (Reply)

Web Programming: Which Computer Schools Are The Best / Jsps N Servlets Sos / Who Can Help On C++ Programming Information

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.