try { // some code IPEndPoint ipEP = new IPEndPoint(paraIPAddress, iPort); Socket udpSocket = new Socket(ipEP.Address.AddressFamily,SocketType.Dgram,ProtocolType.Udp); // some more code } catch(Exception ex) { //Do something if exception is thrown } finally { if (udpSocket != null) { udpSocket.Close(); } }This code gives me the error that udpSocket does not exist in the namespace, so I moved the declaration statement outside the try..catch..finally block.
Socket udpSocket; try { // some code IPEndPoint ipEP = new IPEndPoint(paraIPAddress, iPort); udpSocket = new Socket(ipEP.Address.AddressFamily,SocketType.Dgram,ProtocolType.Udp); // some more code } catch(Exception ex) { //Do something if exception is thrown } finally { if (udpSocket != null) { udpSocket.Close(); } }Now this code gives me the compile error use of unassigned local variable udpSocket