c# Casting v As operator
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
Looks suspiciously like a programming question.
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
Looks suspiciously like a programming question.
-
Looks suspiciously like a programming question.
Hardly :|
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
Thanks Dave, maybe Viaducting needs to get out more ;)
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
I prefer
as
because it is safer:expression as type
is equivalent to:
expression is type ? (type)expression : (type)null
When you use casting you can get
StoopidTypeException
.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
a. Throw back from C++ programming.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
I prefer
as
because it is safer:expression as type
is equivalent to:
expression is type ? (type)expression : (type)null
When you use casting you can get
StoopidTypeException
.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Good point.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
It depends. (A) will throw if the cast fails while (B) will evaluate to
null
if the cast fails. I use both depending on how I intend to handle the casting failure. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Sorry - wrong. The answer's a, because it's better.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
-
Good point.
www.software-kinetics.co.uk Wear a hard hat it's under construction
Norm .net wrote:
Good point.
Holy cr_p! I'm going to the pub, we'll see what comes out after lunch! :laugh:
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
I prefer
as
because it is safer:expression as type
is equivalent to:
expression is type ? (type)expression : (type)null
When you use casting you can get
StoopidTypeException
.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Which is exactly why this is more of a programming question than a "lifestyle choice" question.
-
For those using c#, what do you prefer? A.
SomeObject obj = (SomeObject) e;
or B.
SomeObject obj = e as SomeObject;
www.software-kinetics.co.uk Wear a hard hat it's under construction
It depends on the situation. Normally, I prefer to cast with the
as
operator because I don't have to catch the exception, and it just feels more natural for me to not have code like this:MyClass c = null;
try
{
c = (MyClass)variable;
}
catch(InvalidCastException)
{
c = new MyClass();
}Of course, there are times where you can't use
as
, so in those cases you use the()
casting form.Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
It depends. (A) will throw if the cast fails while (B) will evaluate to
null
if the cast fails. I use both depending on how I intend to handle the casting failure. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
True, but you should know beforehand if the casting is correct.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
It depends. (A) will throw if the cast fails while (B) will evaluate to
null
if the cast fails. I use both depending on how I intend to handle the casting failure. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
Correct answer.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
No, because the two are not equivalent (see other posts below).
-
I prefer
as
because it is safer:expression as type
is equivalent to:
expression is type ? (type)expression : (type)null
When you use casting you can get
StoopidTypeException
.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
I did not know this. I'm currently working on stuff with a common base interface with multipe derived interfaces and I've been playing about with GetType and typeof to try to get the correct derived interface from a base interface. So reading lounge posts may have been productive today. :-D
Pete
-
Hardly :|
www.software-kinetics.co.uk Wear a hard hat it's under construction
If a noob had asked that question in the Lounge they'd have been toast.
-
True, but you should know beforehand if the casting is correct.
www.software-kinetics.co.uk Wear a hard hat it's under construction
Norm .net wrote:
True, but you should know beforehand if the casting is correct.
"Should" is a dangerous word. :) A plugin discovery mechanism (that makes extensive use of reflection) I recently built assumes nothing regarding the type of object that was loaded. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com