I just have a quick question to figure out what will be the best practice when creating my own class.
Let's say this class has one private member that is initialized in the constructor, should I then check if this private member is null in another public, non-stationary method? Or can it be saved so that the variable is not null and therefore it is not necessary to add this check?
For example, like the following: zero-need verification.
// Provides Client connections. public TcpClient tcpSocket; /// <summary> /// Creates a telnet connection to the host and port provided. /// </summary> /// <param name="Hostname">The host to connect to. Generally, Localhost to connect to the Network API on the server itself.</param> /// <param name="Port">Generally 23, for Telnet Connections.</param> public TelnetConnection(string Hostname, int Port) { tcpSocket = new TcpClient(Hostname, Port); } /// <summary> /// Closes the socket and disposes of the TcpClient. /// </summary> public void CloseSocket() { if (tcpSocket != null) { tcpSocket.Close(); } }
So, I made some changes based on all of your answers, and I wonder if this might work better:
private readonly TcpClient tcpSocket; public TcpClient TcpSocket { get { return tcpSocket; } } int TimeOutMs = 100;
Thanks.
source share