I am new to C #, was a pascal lover until I found C # in Depth. Delphi has a try {} statement, which is also implemented in C #.
However, I saw that some of you noted that "Using () {} is better than try {}".
Here is an example:
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ip, port);
try
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ip, port);
catch
{
}
using(sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
{
sock.Connect(ip, port);
}
My questions:
What happens if an exception occurs inside the using statement?
When should “use” be used over “try”, and when is “try” over “use”?
What is the purpose of the “use” statement?
Thanks in advance!
source
share