Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception thrown out with string function replace in c#

Tags:

c#

Following code will cause exception:

string IDs = "";
IDs = IDs.Replace("", "");

Why?

like image 488
KentZhou Avatar asked Dec 06 '22 04:12

KentZhou


1 Answers

It's right in the documentation for string.Replace(). If you try to replace with the "oldValue" parameter as an empty string, it throws an exception.

Exception                  Condition
ArgumentException          oldValue is the empty string (""). 

If you think about it, what are you actually trying to do when you try to find an empty string in another string and replace it with something? Conceptually it doesn't make sense.

like image 98
womp Avatar answered Dec 14 '22 22:12

womp