Solved

Casting Vs. Convert To Method

Posted on 2007-04-02
1
197 Views
Last Modified: 2011-10-03
What is the difference between using this:
(string)value; // typical cast

and this
Convert.ToString(value);

Is one preferred over the other? Why?

0
Comment
Question by:marmaxx
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 18837611
it depends on the context.
usually, casting is only used when you KNOW that the variable (value) is of type string under the hood (for example a string assigned to the generic object data type).
convert is used when the variable is not of type string.
there is a third thing, most objects have a .ToString method, which is, in case of strings, the preferred method.

for other data types, like numerical/dates etc, only your 2 methods are possible, but again, it depends on the context
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now