What did you think I meant when you quoted me as saying
gerrylowry
you can cast null to any reference type
Was it ambiguous? Is it a problem that I did ot bold the word reference? Why are you refering me to something when all you have done is copy what I wrote?
Since nothing to convert, why should we say SomeClass w = (SomeClass)null?
I provided several equivalent ways to assign null to a reference type. Which one you choose is entirely up to you. Choose the one which is clearest for you to understand or that you think other people reading your code will find clearest or that follows your
company standard.
But I just feel very strange——since we can directly assign null to a reference type,why cast convert from null to an instance type itself is valid in syntax of C#?
Don't u think duplicated? And this really makes us feel puzzled that null is every reference's Father, just like object, which can be done through a cast convert.
karthicks's point is correct, ViewState can store many different type data[refer:http://msdn.microsoft.com/en-us/library/ms178198(v=vs.85).aspx]. So if the ViewState["Text"] is not string type, using as operator will return null. But using (string) to convet, we will get exception, it is more secure, that's why Microsoft didn't use as operator.
ToughMan
But if ViewState["Text"] contains a Null value, how it can be directly cast converted to string?
For "how", I think gerry and paul has already made it very clear.
ToughMan
since we can directly assign null to a reference type,why cast convert from null to an instance type itself is valid in syntax of C#?
Don't u think duplicated?
Suppose there is a class User
User u=null;//[u is reference-type variable]
So u's value is null
string temp=(string)u;
This conversion is not convert null to string, it's convert User to string.
User u=null;
string temp=(string)u;
not equal as
User u=null;
string temp=(string)null;
it's should more like
User u = null;
string test =(string)((User)null);//exception will arise
What did you think I meant when you quoted me as saying ...
actually, at first i thought you had erred ... but then i noticed that you had been specific about
reference types ... for that reason, i quickly edited my response http://forums.asp.net/post/5279300.aspx to
ToughMan in order to illustrate the difference when it's a
value type.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
"Gets a dictionary of state information that allows you to save and restore the view state of a server control across multiple requests for the
same page."
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Marked as answer by ToughMan on Jan 25, 2013 05:11 AM
Paul Linton
Star
13421 Points
2535 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 08:19 PM|LINK
What did you think I meant when you quoted me as saying
Was it ambiguous? Is it a problem that I did ot bold the word reference? Why are you refering me to something when all you have done is copy what I wrote?asp.netforum...
Member
604 Points
132 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 08:31 PM|LINK
refer this
http://msdn.microsoft.com/en-us/magazine/ff797918.aspx
http://msdn.microsoft.com/en-in/library/ms972976.aspx
Paul Linton
Star
13421 Points
2535 Posts
Re: Why do a cast convert directly?
Jan 23, 2013 08:35 PM|LINK
ToughMan
Participant
1490 Points
635 Posts
Re: Why do a cast convert directly?
Jan 24, 2013 05:00 AM|LINK
Hello man,
Sorry to make you troubled again……
But I just feel very strange——since we can directly assign null to a reference type,why cast convert from null to an instance type itself is valid in syntax of C#?
Don't u think duplicated? And this really makes us feel puzzled that null is every reference's Father, just like object, which can be done through a cast convert.
Paul Linton
Star
13421 Points
2535 Posts
Re: Why do a cast convert directly?
Jan 24, 2013 05:15 AM|LINK
The cast doesn't make any difference to the result.
If you don't like having the cast, that's OK. Leave it out.
There are many things which are perfectly correct but which make no difference to the result.
x = y + 0;
The '+ 0' doesn't do anything but is perfectly correct c#
if (someBoolVal == true)
The '== true' doesn't do anything but is perfectly correct c#.
Get it out of your head that null is an object or any form of instance, it is not. Null is not every reference's Father. Unfortunately, we are stuck with null, blame Tony Hoare, http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake
Mark - MSFT
Contributor
7071 Points
435 Posts
Microsoft
Re: Why do a cast convert directly?
Jan 24, 2013 08:15 AM|LINK
Hi ToughMan,
For your original post
karthicks's point is correct, ViewState can store many different type data[refer:http://msdn.microsoft.com/en-us/library/ms178198(v=vs.85).aspx]. So if the ViewState["Text"] is not string type, using as operator will return null. But using (string) to convet, we will get exception, it is more secure, that's why Microsoft didn't use as operator.
For "how", I think gerry and paul has already made it very clear.Suppose there is a class User
So u's value is null
This conversion is not convert null to string, it's convert User to string.
not equal as
it's should more like
User u = null; string test =(string)((User)null);//exception will ariseAbout null, please refer: http://msdn.microsoft.com/en-us/library/edakx9da.aspx
Best Regards
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
ToughMan
Participant
1490 Points
635 Posts
Re: Why do a cast convert directly?
Jan 24, 2013 08:42 AM|LINK
Since (string) will let us have exception, why Microsoft invents "as"?
And null means no pointers……So Microsoft should forbid null to be cast converted to a SomeClass type.
Don't u think so?
Mark - MSFT
Contributor
7071 Points
435 Posts
Microsoft
Re: Why do a cast convert directly?
Jan 24, 2013 09:28 AM|LINK
Hi,
as operator can cast type without exception, so we can use it for some other requirement. For example we need null not exceptionViewState["int1"] = 10; string int1as = ViewState["int1"] as string;//int1as is nullI'm afraid I disagree with you, null just a value of reference-type vaiable, we should not separate it from other ordinary values.
Best Regards
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
gerrylowry
All-Star
20515 Points
5713 Posts
Re: Why do a cast convert directly?
Jan 24, 2013 10:51 AM|LINK
@ Paul Linton
Paul, i'm referring (two r's) to you because i'm adding a code example to what you wrote; please see http://forums.asp.net/post/5279308.aspx.
actually, at first i thought you had erred ... but then i noticed that you had been specific about reference types ... for that reason, i quickly edited my response http://forums.asp.net/post/5279300.aspx to ToughMan in order to illustrate the difference when it's a value type.
g.
gerrylowry
All-Star
20515 Points
5713 Posts
Re: Why do a cast convert directly?
Jan 24, 2013 11:43 AM|LINK
@ ToughMan
more information
we're dealing with not only c# but also the .NET Framework in Why do a cast convert directly? so i suggest you look at MSDN:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate(v=vs.100).aspx
"Control.ViewState Property"
"Gets a dictionary of state information that allows you to save and restore the view state of a server control across multiple requests for the same page."
[BrowsableAttribute(false)] protected virtual StateBag ViewState { get; }http://msdn.microsoft.com/en-us/library/system.web.ui.statebag(v=vs.110).aspx
"StateBag Class"
http://msdn.microsoft.com/en-us/library/ms228048(v=vs.85).aspx
"How to: Read Values from View State", .NET Framework 3.0
Robust Programming
Values in view state are typed as String.
In C#, you should always cast to the appropriate type when reading view state values.
ToughMan, returning to your O.P.
get { string str = (string) this.ViewState["Text"]; // could crash if (str != null) return str; return string.Empty; }Your code above, AFAIK will always execute the return str; if it does not crash. AFAIK, you must test for null first:
get { if(this.ViewState["Text"] == null) return String.Empty; string str = (string) this.ViewState["Text"]; return str; }String.Empty is not the same as null:
http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx
See also this interesting post by Phil Haack: http://haacked.com/archive/2006/08/06/TinyTrickForViewStateBackedProperties.aspx.
g.
P.S.:
the following searches return interesting results:
site:stackoverflow.com c# cast null
http://lmgtfy.com/?q=site%3Astackoverflow.com+c%23+cast+null
site:stackoverflow.com c# cast null necessary
http://lmgtfy.com/?q=site%3Astackoverflow.com+c%23+cast+null+necessary
Eric Lippert's answer to http://stackoverflow.com/questions/2215745/conditional-operator-cannot-cast-implicitly/2215959#2215959 is especially interesting.