If you are developing a .NET application that needs to validate the IP Addresses at runtime you can use the following code snippet.
First of all import System.Net namespace
using System.Net;
Then use TryParse method of IPAddress class as shown below:
string ipAddress = "20.20.40.50";
IPAddress ipObj = null;
if (IPAddress.TryParse(ipAddress, out ipObj))
{
Console.WriteLine("IP {0} is valid", ipObj);
}