Solved

Casting Vs. Convert To Method

Posted on 2007-04-02
1
202 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 143

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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