Archive | August, 2008

C# and Null

15 Aug

Hi Friends!

Can you please tell me what should be the output of the following c# code block.

public string testNull()
{

string name = null;

try
{

name = name + “Satyadev”;
Response.Write(name);
}
catch (Exception ex)
{
throw ex;
}
return name;
}

Will it throw an error or return “satyadev” ???

If you are thinking it will throw an ERROR then you are wrong… yes it will return “satyadev”.

you can try it yourself

Now you guys are thinking why I have asked this question to all of you, I am asking this because

while doing a code review one of my colleague pointed a method in which a developer wrote a similar kind of code and this becomes an issue… just because of everyone is thinking this code will throw an error.

So What is the moral of the story ?

Sinning Off …

Satyadev Singh