Nothing = String.Empty (Why are these equal?)

前端 未结 4 980
春和景丽
春和景丽 2020-12-01 15:58

Why does the first if statement evaluate to true? I know if I use \"is\" instead of \"=\" then it won\'t evaluate to true. If I replace String.Empty with \"Foo\" it doesn\

4条回答
  •  星月不相逢
    2020-12-01 16:20

    Try this:

    Console.WriteLine("Is String.Empty equal to Nothing?: {0}", String.Empty.Equals(Nothing))
    

    The = operator doesn't enforce equal types, whereas the .Equals() method of a string object does, as does the Is operator.

提交回复
热议问题