Hello,
I'm new to C#,been a pascal lover until I found C# in Depth.In Delphi there is a try{} statement that is also implemented in C#.
However,I've seen some of you mentioning "Using(){} is better than try{}".
Here's an example:
//This might throw an exception
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ip, port);
//Try statement
try
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ip, port);
catch
{
}
//using(){}
using(sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
{
sock.Connect(ip, port);
}
My questions:
What happens if the exception occurs inside the "using" statement?
When should I use "using" over "try" and when "try" over "using"?
Whats the purpose of "using" statement?
Thanks in advance!