warning CS0618: 'IPAddress.Address' is obsolete: 'This property has been deprecated

warning CS0618: 'IPAddress.Address' is obsolete: 'This property has been deprecated. It is address family dependent. Please use IPAddress.Equals method to perform comparisons.

Converts the octal representation of an IP address to an unsigned integer (contained in a long).

public static long CastIp(string ip)
{
    return (long)(uint)IPAddress.NetworkToHostOrder((int)IPAddress.Parse(ip).Address);
}

It gives me this warning; what would I do to accomplish the same thing without IPAddress in the above code?

Jon Skeet
people
quotationmark

The documentation for IPAddress.Address says:

This property is obsolete. Use GetAddressBytes.

So I suggest you do that:

public static long CastIp(string ip)
{
    IPAddress address = IPAddress.Parse(ip);
    byte[] addressBytes = address.GetAddressBytes();
    // This restriction is implicit in your existing code, but
    // it would currently just lose data...
    if (addressBytes.Length != 4)
    {
        throw new ArgumentException("Must be an IPv4 address");
    }
    int networkOrder = BitConverter.ToInt32(addressBytes, 0);
    return (uint) IPAddress.NetworkToHostOrder(networkOrder);
}

That appears to give the same result, in the tests I've tried... you should check that it does exactly what you want though, as it's not clear exactly what you expect.

people

See more on this question at Stackoverflow