-1

ToUpper() is working when null reference is assigned to an Object type but failing for a null string. Please clarify the fundamentals of it :

Object obj = null;
string str1 = Convert.ToString(obj).ToUpper(); //No exception 

string str2 = null;
string str3 = Convert.ToString(str2).ToUpper(); // Throws exception
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Mitesh
  • 1
  • 4

1 Answers1

4

This is because Convert.ToString(object) returns string.Empty when object is null and Convert.ToString(string) returns the string unchanged (ie: null). You can not call .ToUpper() on null.

Check this question

Community
  • 1
  • 1
Robert Fricke
  • 3,637
  • 21
  • 34