[.NET] C# 'is' operator(dotnetbutchering.blogspot.com)

submitted by John_IdolJohn_Idol(390) 3 years, 8 months ago

The C# 'is' operator looks like a boring little fella at first, but once you get to know it you will eventually appreciate it - it checks if you can safely cast an instance of an object to a type

2 comments |category: |Views: 22

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by yesthatmcgurkyesthatmcgurk(4063) 3 years, 8 months ago 0

I'm not a fan of 'is' for a couple reasons.

1) Bill Clinton... no, strike that.

1) Using it always ends up with a warning about casting twice... if(foo is Bar) ((Bar)foo).DoSomethingAsBar() essentially casts twice, and you get the compiler warning (or is it code analysis?).
2) Its too often used as a crutch for bad code. Take, for instance, the above example. Have you ever done that? Have you seen anybody do it? Didn't you feel dirty afterwards?

Better var bar = foo as Bar; if(bar != null) bar.DoSomethingAsBar(); Better yet, don't.

Reply

posted by John_IdolJohn_Idol(390) 3 years, 8 months ago 0

I agree - using as is way better

Reply

information Login or create an account to comment on this story