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
I used to use A (C upbringing) now I kinda favor B.
I Haven't Gone To Bed With Any Ugly Women, but I've Sure Woke Up With A Few.
-
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
Depends on the situation. Mostly proper casting,
as
occasionally, particularly when checking for a derived type. -
:beer:
www.software-kinetics.co.uk Wear a hard hat it's under construction
Don't mind if I do. :beer: yourself.
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
-
The hand is quicker than the I eye.
www.software-kinetics.co.uk Wear a hard hat it's under construction
Norm .net wrote:
The hand is quicker than the I eye
Too much hand is bad for the eye.
Und wenn du lange in einen abgrund blickst, blickt der Abgrund auch in dich hinein.
-
It is not about preference. When I want an exception in case of type error I use A., and if I want a null I use B. I don't see a point of something like:
SomeObj obj = e as SomeObject;
if (obj == null)
throw new InvalidCastException();Of course, in good languages casting is almost always a sign of a design error.
I use A and never get an Exception, what are you people doing? :confused:
-
I use A and never get an Exception, what are you people doing? :confused:
PIEBALDconsult wrote:
what are you people doing?
SomeType obj = (SomeType)BloatedUglyUnreadableFrameworkFactory.CreateObject(someXMLStringThatIHopeWorksSometimesButNeverKnowForSure);
-
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
As is for poor programmers. If you know that e always is SomeObject then e should be typed as such using some other method. Otherwise you always have to check the result of the as operation. So then you have the following two scenarios which must always be in case A or case B:
if(e is SomeObject){
SomeObject someObject = (SomeObject)e;
}or
SomeObject someobject = e as SomeObject;
if(someObject != null){}
But then in case by I always here the response ... but I know e is always SomeObject. Really then maybe it should be defined as such. The AS operator is designed solely to support developers that don't have a fundamental concept of type. After all, for all of the time I have seen the is operator used with a subsequent cast, checking for null after the AS is a white rhinoceros.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
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
The first when I need the exception, the second when I need the null. Because face it, e is never what you need it to be.
FILETIME to time_t
| FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy -
As is for poor programmers. If you know that e always is SomeObject then e should be typed as such using some other method. Otherwise you always have to check the result of the as operation. So then you have the following two scenarios which must always be in case A or case B:
if(e is SomeObject){
SomeObject someObject = (SomeObject)e;
}or
SomeObject someobject = e as SomeObject;
if(someObject != null){}
But then in case by I always here the response ... but I know e is always SomeObject. Really then maybe it should be defined as such. The AS operator is designed solely to support developers that don't have a fundamental concept of type. After all, for all of the time I have seen the is operator used with a subsequent cast, checking for null after the AS is a white rhinoceros.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
Thanks Ennis - I'm a poor programmer then because I use
as
, rather than the double cast, which is doing the same work again. What happens internally withas
is that it checks to see if the variable is of the type, and if it is it returns a non-null pointer to that type. With theis
operator, you check to see if it is of the type and then you cast it - which still determines internally whether or not it belongs to that type (this is how it throws anInvalidTypeException
). In any case where you are using code-discovery, such as IoC, then theas
call is more efficient. [Edit]I should add that this relates to our plugin code where the client provides their own logic, and "forgets" to implement the appropriate interfaces.[/Edit]Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
modified on Thursday, June 23, 2011 9:30 AM
-
Richard A. Dalton wrote:
Plug-in tpye code
I don't have such problems with plug-ins.
You have better clients than we do then. You tell them, you must implement this interface in order for this to work, and bam they completely fail to implement the interface.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
The first when I need the exception, the second when I need the null. Because face it, e is never what you need it to be.
FILETIME to time_t
| FoldWithUs! | sighist | WhoIncludes - Analyzing C++ include file hierarchy -
Don't fancy seeing your code if you think b. looks like a great ape :rolleyes:
www.software-kinetics.co.uk Wear a hard hat it's under construction
Somebody didn't read the subject of the post.
Und wenn du lange in einen abgrund blickst, blickt der Abgrund auch in dich hinein.
-
PIEBALDconsult wrote:
what are you people doing?
SomeType obj = (SomeType)BloatedUglyUnreadableFrameworkFactory.CreateObject(someXMLStringThatIHopeWorksSometimesButNeverKnowForSure);
You've been reading java code on the throne again haven't you? :)
¡El diablo está en mis pantalones! ¡Mire, mire! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! Personal 3D projects Just Say No to Web 2 Point Blow
-
Somebody didn't read the subject of the post.
Und wenn du lange in einen abgrund blickst, blickt der Abgrund auch in dich hinein.
:wtf: are you going on about.
www.software-kinetics.co.uk Wear a hard hat it's under construction
-
I'd say more of a debate, but as we all know rules of the lounge, I'd say just go along with it and chill :)
www.software-kinetics.co.uk Wear a hard hat it's under construction
I seem to have upset some voters along the way. :-D
-
:wtf: are you going on about.
www.software-kinetics.co.uk Wear a hard hat it's under construction
Sahir seems to be comparing C# casts with a handler of equine animals.
-
-
Sahir seems to be comparing C# casts with a handler of equine animals.
:) Indeed, at this rate he's going to hit an exception that he may not like.
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 directly throwing. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
:wtf: are you going on about.
www.software-kinetics.co.uk Wear a hard hat it's under construction
Well in brief, if you are in the shower and drop the soap and there is an operator mentioned in the subject line around, there is a good chance he might be a hominid. I wouldn't advise you to try and pick up the soap.
Und wenn du lange in einen abgrund blickst, blickt der Abgrund auch in dich hinein.