I strongly disagree with the provided answer. It surely works, but I can see serious problems with it, starting with readability. In my opinion, readability and maintainability are paramount, and the accepted solution simply won't do. Adding to this, a more generic approach will also solve the problem for IPv6, while the accepted solution will not work.
My proposal is to use the following method:
public static IPAddress AddOne(this IPAddress ipAddress)
{
byte[] data = ipAddress.GetAddressBytes();
IncrementByOneFromRight(data, data.Length - 1);
return new IPAddress(data);
}
private static void IncrementByOneFromRight(byte[] data, int index)
{
if (index < 0)
return;
if (data[index] < byte.MaxValue)
data[index] += 1;
else
{
data[index] = 0;
IncrementByOneFromRight(data, index - 1);
}
}
Place the above in a visible static class, and the AddOne method will work as an extension method to IPAddress. This makes it easier to work with, and you will not expose the nitty-gritty implementation details of adding to the IPAddress in your class, while maintaining and readability. This will have the added benefit of not cluttering the class you are already writing with possibly unrelated methods.
Please vote up so that this is visible to people coming to this question if you agree with my answer and the reasons I disagree with the approved one.