By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

casting (best practice?)

P: n/a
Hi All,

I have a question about who one should go about casting types in C#.
I have heard some writers and speakers call static casting a bad
practice and recommend using "as" and "is". That's fine.

However in most of the production code that I see, programmers use
either static casting OR use the "as" keyword without using the "is"
keyword first to verify that the object actually supports the desired
type.

What gives?

Is this just laziness or is something gained in brevity by only using
the static cast or the "as" keyword.

-Thx

Apr 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Apr 4, 1:15 pm, kilik3...@gmail.com wrote:
I have a question about who one should go about casting types in C#.
I have heard some writers and speakers call static casting a bad
practice and recommend using "as" and "is". That's fine.
It's not fine in my view, without an explanation of *why*.

I use "as" instead of "is + cast" and then only when it's valid for it
not to be true. So for instance, I'd do:

object o = GetSomeObject();
String x = o as String;
if (x != null)
{
// So something with x
}
else
{
// Do something else
}

If it's an error condition for o *not* to be a string, however, I'd
just cast - that way an appropriate exception will be generated in
that error case.
However in most of the production code that I see, programmers use
either static casting OR use the "as" keyword without using the "is"
keyword first to verify that the object actually supports the desired
type.
If you're going to use "as" there's no point in doing "is" first,
really.
Is this just laziness or is something gained in brevity by only using
the static cast or the "as" keyword.
The point is to avoid two runtime checks, which is what you get from is
+cast. "as" gives you one cast and then a cheap nullity check.

Jon

Apr 4 '07 #2

P: n/a
On Apr 4, 8:29 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Apr 4, 1:15 pm, kilik3...@gmail.com wrote:
I have a question about who one should go aboutcastingtypes inC#.
I have heard some writers and speakers call staticcastinga bad
practiceand recommend using "as" and "is". That's fine.

It's not fine in my view, without an explanation of *why*.

I use "as" instead of "is + cast" and then only when it's valid for it
not to be true. So for instance, I'd do:

object o = GetSomeObject();
String x = o as String;
if (x != null)
{
// So something with x}

else
{
// Do something else

}

If it's an error condition for o *not* to be a string, however, I'd
just cast - that way an appropriate exception will be generated in
that error case.
However in most of the production code that I see, programmers use
either staticcastingOR use the "as" keyword without using the "is"
keyword first to verify that the object actually supports the desired
type.

If you're going to use "as" there's no point in doing "is" first,
really.
Is this just laziness or is something gained in brevity by only using
the static cast or the "as" keyword.

The point is to avoid two runtime checks, which is what you get from is
+cast. "as" gives you one cast and then a cheap nullity check.

Jon
Cool.

Yup I meant to write cast + is (not as + is). Thanks for clarifying
this issue for me.
Apr 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.