Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is casting and how many types of casting and how i can use in my program. i want to. tell me the way to use the casting . please explain. with the suitable example
Posted
Comments
Wonde Tadesse 28-Jan-12 19:40pm    
You can find numerous articles from google.http://lmgtfy.com/?q=types+of+casting+in+c+sharp

I have a hard time believing that you have looked at google for this. Using this wonderful tool, I got 1,820,000 results in seconds. The very first link was this one.
Casting(C#)[^]
 
Share this answer
 
Comments
Uday P.Singh 28-Jan-12 10:45am    
5!
Abhinav S 29-Jan-12 3:56am    
A search would be useful. 5.
Two types of casts, explicit and implicit exist.
E.g. int to float is implicit but vice-versa is explicit.
Explicit is when you are forcing a cast.
Read more about this here[^].
 
Share this answer
 
Comments
Espen Harlinn 31-Jan-12 9:05am    
5'ed!
Abhinav S 31-Jan-12 9:43am    
Thank you.
This is such a broad, and general, question that I suggest you first read-up on the basics of casting, and "casting" compared to "conversion:" and then ask a more specific question here. At the most basic level: using 'casting' you are transforming one Type into another; this is simplistic, but I'd say with "conversion:" you are transforming a value of one Type into a value of another Type.

You might start here with this CP article: [^], and then move on to read the broad content in these MSDN resources: [^], and [^].

You are going to need to learn why this does not work:
string someString = "100.0";
double someDouble = (double)someString; //compile error
But, this does:
double someDouble = Convert.ToDouble(someString);
As you are ready to study the use of the 'as' operator for casting: be sure and read this:[^]. The "as" operator can only be used with Reference Types and Nullable Types: and you need to learn what those are.

When you have a strong foundation in basic understanding of "casting," then I suggest you take on the task of learning to use "casting" to an Interface, which is an advanced topic, but a powerful tool ... necessary for some important techniques like raising "notifications" ... valuable for passing an instance of an object which, because it is cast to an Interface, exposes a limited subset of its internal contents to other objects it is passed to.

good luck, Bill
 
Share this answer
 
Comments
Amir Mahfoozi 29-Jan-12 0:22am    
+5
Abhinav S 29-Jan-12 3:56am    
Good answer. 5.
Espen Harlinn 31-Jan-12 9:06am    
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900