Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
4.88/5 (9 votes)
See more:
Hi,

Was wondering if someone can explain this:

The following returns null.

C#
Convert.ToString(null)


The following returns null.

C#
Request.QueryString["InvalidQSParamName"]


But the following returns an empty string.

C#
Convert.ToString(Request.QueryString["InvalidQSParamName"])


As does

C#
string myString = Request.QueryString["InvalidQSParamName"];
Convert.ToString(myString)


But this returns null

C#
string myString = null;
Convert.ToString(myString)



Can anyone explain why the difference in behaviour between two?
Posted
Comments
sudipta biswas 20-Jun-13 9:29am    
I am getting null in both cases.

Couple of good explanations in the responses to this stackoverflow post[^]
 
Share this answer
 
Comments
Leo Chapiro 20-Jun-13 9:25am    
LOL, CHill60, we have found the same answer on stackoverflow ^^
Karthik Harve 20-Jun-13 9:25am    
good one. +5!!
Stephen Hewison 20-Jun-13 9:29am    
Thanks
jemaritn80 20-Jun-13 9:40am    
I must be missing something. What you're saying is accurate, but HttpRequest.QueryString is a NameValueCollection. NameValueCollection "Represents a collection of associated String keys and String values that can be accessed either with the key or with the index." So, it would be Convert.ToString(string s) which should return null when s=null and not "". My test is consistent with this, but I am down voted. I'm curious to know what is wrong with my findings.
To add to the two above:

MSDN Convert.ToString(object)[^]

Quote:
Return Value
Type: System.String
The string representation of value, or String.Empty if value is null.


And MSDN Convert.ToString(string)[^]

Quote:
Return Value
Type: System.String
value is returned unchanged.
 
Share this answer
 
As written here http://stackoverflow.com/questions/10355736/why-does-convert-tostringnull-return-a-different-value-if-you-cast-null[^] :

There are 2 overloads of ToString that come into play here

C#
Convert.ToString(object o);
Convert.ToString(string s);


The C# compiler essentially tries to pick the most specific overload which will work with the input. A null value is convertible to any reference type. In this case string is more specific than object and hence it will be picked as the winner.

In the null as object you've solidified the type of the expression as object. This means it's no longer compatible with the string overload and the compiler picks the object overload as it's the only compatible one remaining.
 
Share this answer
 
v2
Comments
Stephen Hewison 20-Jun-13 9:29am    
thanks
Mohancs27 8-Jan-14 4:17am    
Good Explanation...
What test are you using to make this conclusion? If InvalidQSParamName is not a defined query string name, then my testing indicates that
C#
Convert.ToString(Request.QueryString["InvalidQSParamName"])== null

I arrived at this conclusion by creating a page named testpage.aspx.

Markup:
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testpage.aspx.cs" Inherits="TestSpace.testpage" %>



<html>
<head runat="server">
    <title>Null Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:label id="TestLabel" runat="server" xmlns:asp="#unknown" />
    </div>
    </form>
</body>
</html>


Code-Behind:
C#
using System;

namespace TestSpace
{
    public partial class testpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToString(Request.QueryString["emptyquery"]) == null)
            {
                this.TestLabel.Text = "Null";
            }
            else if (Convert.ToString(Request.QueryString["emptyquery"]) == "")
            {
                this.TestLabel.Text = "Empty String";
            }
        }
    }
}


The page output is

Null

Perhaps you have inadvertently created a query string name of InvalidQSParamName with an empty-string value. Try changing the parameter name and see if that gets you the expected results.
 
Share this answer
 
v3
Comments
Stephen Hewison 20-Jun-13 10:50am    
Hi, whilst your test is valid it's my mistake as I should of referenced Session and not QueryString in my question.
it has something to do with the convert.toXX method mechanism , convert method will forcelly change the null object to the default value of XX . But when you use XX.Parse(null) it will run error.
 
Share this answer
 
v2
Both the cases returns null values
 
Share this answer
 
In this case u are using a Parameter in "ToString" Method, so it will return "Empty String"

Convert.ToString(Request.QueryString["InvalidQSParamName"])


but, In this case u are using null as a Parameter in "ToString" Method, so it will return "Null"

C#
string myString = null;
Convert.ToString(myString)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900