string.Empty vs null.Which one do you use?

前端 未结 5 1915
不思量自难忘°
不思量自难忘° 2020-11-30 23:31

Recently a colleague at work told me not to use string.Empty when setting a string variable but use null as it pollutes the stack?

He says

5条回答
  •  攒了一身酷
    2020-11-30 23:51

    FWIW, I found that mixing "" and String.Empty doesn't work:

    var a = "";
    alert("a " + (a == "") + ", " + (a==String.Empty));   //Yields "a true, false"
    
    var b = String.Empty;
    alert("b " + (b == "") + ", " + (b == String.Empty)); //Yields "b false, true"
    

    In particular, if you use $.trim to get the value of an empty DOM input field, then compare it to String.Empty, you get false. Not sure why that is, but there you go. I now just use "" everywhere for consistency.

提交回复
热议问题