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

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: