473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

casting (best practice?)

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
2 2631
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
5
by: Tom Carroll | last post by:
When assigning or testing equality between a type and a constant, should the constant be casted to the type? I.e. should fork() == -1 be written as fork() == (pid_t)-1? should uid = 45 be...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
41
by: SRR | last post by:
Why is it discouraged to explicitly typecast the void pointer returned by malloc(); function? For example: { int *p; p = (int*)malloc(2*sizeof(int)); /*Explicit casting is done, therfore it...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
5
by: johanatan | last post by:
Does anyone know the reasons for the lack of an implicit casting operator in any greater depth than: A. Automatic conversion is believed to be too error prone. (from the FAQ at the bottom of:...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.