₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,330,985 members, 8,448,104 topics. Date: Sunday, 19 July 2026 at 06:45 PM

Toggle theme

C# Programmers! - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingC# Programmers! (968 Views)

1 Reply (Go Down)

C# Programmers! by lordbenax(op): 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(op): 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

C# Programmers, Solve This QuestionC++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!!for beginner C++ Programmers234

Help In Php,java, Html And C#Ego Trolls Of The Programming SectionHow Rewarding Is App For New Generation Smart Phones To Local Developers?