Home To Know Codes C# Type Casting

C# Type Casting

1668
55
SHARE
C# Type Casting

C-Sharp allows us to create variable of many types but does not allow us to assign the value of one type of variable into another type of variable after a variable is declared. This cannot be declared again or used to store value of another type therefore C-Sharp provides the flexibility for casting/Conversion.

  • Implicit Conversion
  • Explicit Conversion
  • ‘is’ and ‘as’ Operators
  • User Defined Conversion

Implicit conversion

There are no special syntax is required for implicit conversion because it is type safe and no data will be lost. One data type can simply be assigned to another. This include conversion smaller to larger integral types and derived class to base class.

Implicit conversion of derived class to base class is called Upcasting.

Integral Types

int i = 1258975896;
long p=i;

Reference Types

Derived class always contains all the members of a base classes so derived class object can access the base class variable without any problem.

Implicit Conversion
Implicit Conversion

Explicit Conversion

Explicit conversion convert the data larger to smaller and conversion of base class to derived class by using a cast operator. In this conversion information might be lost or conversion might not be succeed for some reason if not succeed it raised an exception InvalidCastException

Explicit conversion of base class to derived classes is called Downcasting.

Integral Types

long m=85;
int i=(int)m;

Reference Types

Explicit Conversion
Explicit Conversion

‘is’ and ‘as’ Operators

C-Sharp also provide ‘is’ and ‘as’ operators which are helpful in performing explicit cast in an exception safe manner. The is operator checks whether the type being casted from is compatible to the type being casted to and returns a boolean value.

‘is’ and ‘as’ Operators Input
‘is’ and ‘as’ Operators Input

‘is’ and ‘as’ Operators Output
‘is’ and ‘as’ Operators Output

The ‘as’ operator checks the compatibility and if it’s ok it will perform the cast too. If the cast is not compatible or unsuccessful then the result will be null. The as operator can only be used with reference type we can use ‘is’ or ‘as’ operator for exception safe casting we should use ‘is’ operator if the target type is value type and ‘as’ operator for reference type.

User Defined Conversion

User Defined Conversion is performed by using special method that can define to enable explicit and implicit conversion between class to struct or basic data type and struct to class or basic data type. There are lot of helper classes and helper function available in C-Sharp to perform the frequently needed conversion.

User Defined Conversion
User Defined Conversion

55 COMMENTS

  1. I simply wanted to appreciate you once again. I am not sure the things I would have tried in the absence of the solutions provided by you regarding such a industry. It has been the troublesome setting in my circumstances, however , considering this skilled strategy you processed that made me to jump with happiness. Now i’m grateful for the support and in addition sincerely hope you find out what a great job you happen to be undertaking educating others via your web page. Most probably you’ve never come across all of us.

  2. I am commenting to let you understand what a magnificent encounter my daughter found checking your webblog. She noticed some things, which include what it is like to have an awesome coaching mood to get most people just master certain complex subject areas. You actually did more than her expectations. Thank you for distributing these precious, healthy, explanatory and in addition easy guidance on that topic to Julie.

  3. I wanted to draft you that little note just to thank you very much as before for those magnificent methods you have documented in this article. This has been really unbelievably generous with you to present publicly what many people would have offered for sale for an e book in order to make some bucks for their own end, principally now that you could possibly have done it in case you wanted. The good tips additionally worked to provide a great way to understand that the rest have similar desire just as my very own to understand a lot more related to this matter. I am certain there are some more pleasurable periods in the future for many who find out your site.

  4. I must show my affection for your kindness supporting those people that absolutely need help with this subject matter. Your very own commitment to passing the message all through turned out to be remarkably powerful and have frequently made folks just like me to reach their endeavors. Your warm and friendly useful information means much a person like me and especially to my peers. Regards; from everyone of us.

  5. I must show appreciation to the writer for rescuing me from this type of instance. Just after surfing throughout the online world and seeing thoughts that were not powerful, I was thinking my life was gone. Being alive minus the approaches to the problems you have fixed through this write-up is a serious case, as well as ones that would have in a negative way damaged my career if I hadn’t encountered your site. Your own personal mastery and kindness in handling every item was very helpful. I don’t know what I would have done if I hadn’t discovered such a step like this. I can also now look ahead to my future. Thank you so much for this reliable and sensible guide. I won’t be reluctant to recommend the blog to anybody who needs recommendations on this subject.

  6. I truly wanted to write a quick word to express gratitude to you for all of the lovely tips you are showing on this site. My time-consuming internet search has now been honored with beneficial know-how to talk about with my two friends. I would believe that we website visitors are undoubtedly blessed to be in a great network with so many perfect professionals with very helpful concepts. I feel quite privileged to have come across the site and look forward to really more enjoyable minutes reading here. Thanks once again for a lot of things.

  7. Thanks a lot for providing individuals with a very nice possiblity to read critical reviews from this site. It really is very pleasing and as well , full of amusement for me personally and my office acquaintances to visit the blog at a minimum 3 times a week to study the latest guides you have got. Not to mention, I’m so always contented for the superb opinions you give. Selected 4 areas in this posting are in truth the simplest we’ve ever had.

  8. Thank you a lot for providing individuals with an exceptionally wonderful chance to discover important secrets from this website. It is always very beneficial and as well , full of fun for me personally and my office friends to visit your blog more than three times in one week to learn the newest stuff you have. And indeed, I am also actually amazed for the very good concepts served by you. Selected 1 facts in this post are without a doubt the most beneficial we’ve ever had.

  9. I enjoy you because of each of your work on this website. My daughter takes pleasure in doing investigations and it’s easy to see why. We all notice all of the lively means you deliver advantageous guidelines through your web site and in addition inspire participation from others about this matter then my girl is without question understanding a whole lot. Enjoy the rest of the year. Your performing a brilliant job.

  10. I am glad for commenting to make you know of the cool discovery my cousin’s girl gained browsing your webblog. She picked up lots of details, not to mention what it’s like to have an excellent teaching mindset to let most people smoothly grasp certain multifaceted matters. You truly exceeded people’s expectations. Thanks for providing such precious, dependable, informative not to mention cool thoughts on the topic to Mary.

  11. Thank you for every one of your labor on this web page. My aunt delights in working on investigation and it is obvious why. All of us hear all about the compelling method you create effective steps via your web site and attract contribution from other people on this point while our favorite daughter is always becoming educated so much. Take pleasure in the remaining portion of the year. Your carrying out a good job.

  12. I wanted to write a simple comment so as to say thanks to you for those precious tactics you are giving at this site. My time consuming internet search has finally been honored with pleasant insight to share with my pals. I ‘d assume that many of us site visitors are undoubtedly blessed to dwell in a perfect site with very many special people with helpful things. I feel somewhat happy to have come across your entire webpages and look forward to so many more awesome minutes reading here. Thank you once more for everything.

  13. I needed to send you that little bit of remark to thank you very much over again for your superb basics you have documented in this article. It’s simply seriously open-handed with people like you to give easily precisely what a number of people could have sold for an e-book to help make some money on their own, notably since you might have tried it in case you desired. The thoughts in addition acted to be the good way to be sure that someone else have a similar fervor much like my very own to realize great deal more in terms of this problem. I am certain there are thousands of more pleasurable occasions in the future for individuals that look into your website.

  14. Thanks for your whole effort on this site. Debby take interest in carrying out investigation and it’s simple to grasp why. I learn all regarding the compelling way you convey very useful items via this blog and even attract participation from some others on this theme then my girl is without question being taught a lot of things. Take advantage of the remaining portion of the new year. You have been performing a fabulous job.

  15. I’m commenting to make you understand what a really good experience my friend’s princess went through studying the blog. She figured out too many pieces, which include what it’s like to have an amazing teaching spirit to get folks with ease understand various very confusing things. You actually surpassed her expectations. Many thanks for producing these effective, dependable, informative and in addition unique guidance on the topic to Ethel.

  16. I am commenting to let you be aware of of the helpful experience my wife’s daughter had using your site. She came to understand so many things, not to mention what it’s like to have a great helping nature to get many people completely completely grasp chosen hard to do issues. You really surpassed visitors’ expectations. I appreciate you for delivering the effective, trusted, explanatory and unique guidance on the topic to Lizeth.

  17. I and my guys were actually looking at the good helpful tips from your web site and so immediately came up with an awful suspicion I had not expressed respect to you for those tips. My people were totally stimulated to learn all of them and already have surely been using them. Many thanks for really being so accommodating and for getting this sort of cool things millions of individuals are really eager to be informed on. My sincere apologies for not expressing appreciation to you sooner.

  18. I actually wanted to post a brief word to appreciate you for all of the great tricks you are giving out on this site. My extensive internet investigation has finally been honored with sensible ideas to share with my relatives. I ‘d point out that many of us site visitors actually are very much endowed to live in a notable network with very many perfect individuals with interesting ideas. I feel quite blessed to have encountered your weblog and look forward to some more enjoyable times reading here. Thanks again for a lot of things.

  19. My husband and i have been fortunate when Albert could do his investigations by way of the ideas he had out of your blog. It is now and again perplexing just to find yourself handing out information many others may have been making money from. Therefore we fully grasp we need you to give thanks to for this. Those illustrations you’ve made, the simple site menu, the friendships your site help foster – it’s most sensational, and it’s really letting our son in addition to our family feel that this issue is excellent, which is very indispensable. Thanks for all!

  20. I want to express my thanks to this writer for bailing me out of this type of trouble. Right after looking throughout the the net and meeting advice that were not pleasant, I assumed my life was well over. Living minus the solutions to the issues you have resolved by means of your main short article is a serious case, as well as ones which might have adversely damaged my entire career if I had not noticed the blog. Your competence and kindness in touching the whole thing was invaluable. I am not sure what I would’ve done if I hadn’t come across such a point like this. It’s possible to now relish my future. Thank you so much for your reliable and effective help. I won’t think twice to propose your site to any person who desires tips on this topic.

  21. Thanks a lot for providing individuals with an extraordinarily pleasant possiblity to discover important secrets from this site. It really is very lovely and also full of a good time for me and my office friends to visit your blog minimum 3 times per week to find out the new things you have. And of course, I’m also usually satisfied concerning the dazzling techniques you serve.

  22. I wish to express some thanks to the writer just for rescuing me from this instance. As a result of surfing throughout the the web and coming across ways which are not helpful, I thought my entire life was over. Being alive without the solutions to the issues you have fixed as a result of your entire guide is a crucial case, as well as ones which could have in a wrong way damaged my entire career if I had not come across the blog. Your own competence and kindness in handling all the pieces was vital. I don’t know what I would’ve done if I had not encountered such a subject like this. I can at this time look ahead to my future. Thanks a lot very much for your expert and effective guide. I will not be reluctant to suggest the website to anybody who needs and wants assistance on this issue.

  23. I simply needed to appreciate you once more. I do not know the things that I could possibly have gone through in the absence of these recommendations documented by you directly on that problem. Entirely was a frightful dilemma in my position, nevertheless looking at the specialized technique you processed it made me to weep for joy. I will be happier for this support as well as sincerely hope you are aware of an amazing job that you’re doing training other individuals using your webblog. I know that you have never encountered all of us.

  24. I simply needed to thank you very much yet again. I am not sure what I would have used in the absence of those methods documented by you over such theme. It became a frightful condition in my view, nevertheless taking note of the very skilled manner you treated that made me to leap for joy. I am just happy for your support and even hope you know what a powerful job you were getting into teaching some other people through the use of your blog post. I know that you’ve never met all of us.

  25. I want to show thanks to this writer just for rescuing me from this particular trouble. Because of exploring through the the net and seeing solutions that were not pleasant, I thought my entire life was well over. Living minus the strategies to the issues you have solved by means of your main short post is a critical case, as well as the kind that would have negatively affected my entire career if I hadn’t come across your website. That training and kindness in touching almost everything was useful. I’m not sure what I would’ve done if I had not discovered such a point like this. I am able to now look ahead to my future. Thank you so much for your high quality and sensible guide. I will not be reluctant to propose your blog post to any individual who needs direction on this situation.

  26. I want to show my respect for your generosity for men and women who need assistance with this one matter. Your real commitment to passing the solution throughout was certainly powerful and has constantly encouraged somebody like me to get to their pursuits. This warm and helpful report signifies a lot to me and much more to my colleagues. Thanks a lot; from all of us.

  27. Thanks for your own effort on this blog. My aunt loves getting into internet research and it’s obvious why. Almost all know all of the dynamic way you convey effective strategies on the web site and therefore invigorate response from other ones about this concern while our favorite princess is actually discovering a whole lot. Enjoy the remaining portion of the year. You’re the one performing a tremendous job.

  28. I wanted to make a quick comment so as to say thanks to you for all the great pointers you are giving at this website. My particularly long internet investigation has at the end of the day been rewarded with good quality facts to write about with my relatives. I ‘d express that many of us readers are quite fortunate to be in a great network with many special professionals with very helpful tactics. I feel very much privileged to have seen your entire site and look forward to plenty of more brilliant moments reading here. Thank you once again for all the details.

  29. Thanks for all of your effort on this blog. Gloria take interest in managing research and it’s really obvious why. Most of us hear all about the powerful medium you make great suggestions through the blog and as well encourage response from people on that area while our own princess is truly becoming educated a lot of things. Have fun with the rest of the year. You have been conducting a wonderful job.

  30. I wish to voice my gratitude for your kind-heartedness supporting individuals who really need help with the question. Your very own commitment to passing the message around had become astonishingly effective and has continually made folks like me to realize their goals. Your amazing informative tutorial implies a lot a person like me and somewhat more to my colleagues. Warm regards; from everyone of us.

  31. I would like to show appreciation to the writer just for rescuing me from this situation. Just after scouting through the world-wide-web and finding notions which are not pleasant, I believed my entire life was well over. Living devoid of the strategies to the difficulties you have fixed by way of the short article is a crucial case, and the ones that might have adversely damaged my entire career if I hadn’t encountered your web page. That know-how and kindness in playing with everything was invaluable. I’m not sure what I would have done if I hadn’t come upon such a step like this. It’s possible to at this point relish my future. Thanks so much for the impressive and result oriented guide. I will not think twice to suggest your blog to any individual who would like guidance on this problem.

  32. I precisely wished to thank you very much once again. I do not know the things that I might have achieved in the absence of those points shown by you directly on such subject. It has been the intimidating dilemma in my opinion, nevertheless taking a look at your professional fashion you handled that forced me to weep with joy. I will be happier for your service and in addition sincerely hope you really know what a great job that you’re carrying out instructing the rest by way of a site. Most probably you haven’t encountered any of us.

  33. I needed to draft you the tiny observation in order to say thanks once again for those wonderful tactics you have shown in this article. This has been quite remarkably open-handed of people like you to give unreservedly all that most people would have supplied for an ebook to generate some bucks for their own end, principally now that you could possibly have tried it if you desired. The principles likewise worked to be the good way to understand that other people online have the same fervor much like my very own to find out a lot more regarding this problem. I’m certain there are lots of more enjoyable moments up front for folks who discover your website.

  34. I would like to show appreciation to this writer for bailing me out of this type of issue. Because of looking out throughout the internet and obtaining techniques which were not helpful, I was thinking my entire life was done. Existing without the strategies to the difficulties you have sorted out all through your good website is a critical case, as well as those which could have in a wrong way affected my career if I had not discovered your blog. That competence and kindness in playing with all things was vital. I’m not sure what I would have done if I hadn’t come upon such a stuff like this. It’s possible to at this time relish my future. Thank you very much for your impressive and results-oriented guide. I won’t think twice to suggest your web site to any person who should have direction on this area.

  35. I definitely wanted to construct a quick note in order to thank you for these wonderful tips and hints you are showing at this website. My extended internet look up has at the end of the day been honored with good facts and strategies to write about with my company. I ‘d assume that we website visitors are very blessed to dwell in a fabulous site with very many marvellous people with very helpful things. I feel very much privileged to have seen your web pages and look forward to so many more enjoyable minutes reading here. Thanks a lot again for a lot of things.

  36. I’m often to running a blog and i really admire your content. The article has actually peaks my interest. I’m going to bookmark your website and maintain checking for brand new information.

  37. I am also commenting to make you be aware of of the exceptional encounter my princess undergone studying your web site. She even learned several details, which include what it’s like to possess an incredible teaching spirit to have the rest really easily know just exactly a variety of grueling subject areas. You truly did more than readers’ expectations. Thank you for imparting those invaluable, trustworthy, educational and as well as cool guidance on this topic to Jane.

  38. My wife and i felt absolutely lucky Louis could finish off his preliminary research via the precious recommendations he received when using the blog. It is now and again perplexing just to choose to be releasing secrets which usually people have been making money from. And we do know we have the website owner to thank because of that. The main illustrations you’ve made, the easy web site menu, the friendships you will make it possible to instill – it’s got many fabulous, and it is assisting our son in addition to our family understand that issue is thrilling, which is certainly truly fundamental. Many thanks for the whole lot!

  39. I would like to express my thanks to you just for bailing me out of this type of predicament. As a result of scouting throughout the search engines and getting methods which were not beneficial, I believed my entire life was done. Living devoid of the answers to the problems you have solved by means of this short post is a critical case, and the ones that might have in a negative way damaged my entire career if I had not encountered your blog post. Your competence and kindness in playing with the whole thing was vital. I’m not sure what I would’ve done if I hadn’t encountered such a solution like this. I can also at this time look ahead to my future. Thanks for your time so much for this high quality and amazing guide. I won’t think twice to refer your site to anybody who needs and wants direction about this subject matter.

  40. Thank you for all of the efforts on this website. Kate enjoys making time for investigation and it is easy to understand why. A number of us notice all of the compelling medium you make very useful strategies via your web site and even encourage participation from some other people about this issue then our own simple princess is truly learning a great deal. Take pleasure in the remaining portion of the year. Your conducting a glorious job.

  41. I am commenting to make you understand of the useful experience my daughter gained reading your blog. She noticed several pieces, with the inclusion of what it is like to have an amazing giving mood to make many people easily know specific hard to do topics. You actually exceeded our expectations. Thank you for supplying these effective, trustworthy, explanatory and in addition cool tips about the topic to Tanya.

  42. I would like to express thanks to you for rescuing me from such a situation. Right after looking throughout the online world and obtaining techniques that were not productive, I figured my life was over. Existing devoid of the strategies to the issues you have resolved as a result of your main review is a crucial case, and ones which might have in a negative way damaged my entire career if I had not noticed your web blog. Your primary capability and kindness in maneuvering every aspect was crucial. I’m not sure what I would’ve done if I had not come across such a thing like this. I am able to at this point look ahead to my future. Thanks a lot very much for the high quality and sensible guide. I will not hesitate to refer your web page to any individual who requires counselling about this situation.

  43. I definitely wanted to construct a small note in order to say thanks to you for all of the wonderful guidelines you are posting on this website. My time consuming internet search has at the end of the day been paid with useful facts and techniques to talk about with my friends. I would claim that most of us visitors actually are unquestionably blessed to live in a very good network with so many perfect people with helpful plans. I feel somewhat lucky to have used your webpage and look forward to so many more enjoyable moments reading here. Thanks a lot again for everything.

  44. I wish to express my respect for your kind-heartedness giving support to individuals who require help with this one niche. Your special dedication to getting the solution all around was extremely functional and have specifically allowed somebody just like me to achieve their pursuits. Your new helpful instruction means a great deal to me and even more to my fellow workers. Thanks a lot; from each one of us.

  45. Thank you a lot for providing individuals with an extremely special chance to check tips from this site. It can be very amazing plus stuffed with amusement for me and my office co-workers to search your blog at the least three times in one week to see the fresh stuff you have got. And indeed, I’m just always motivated concerning the outstanding thoughts you give. Certain 1 areas in this posting are truly the most impressive I’ve had.

  46. I together with my buddies have been reading through the great secrets and techniques from your website and so then I had an awful feeling I never thanked the blog owner for them. Most of the people are actually certainly joyful to see all of them and already have honestly been having fun with those things. We appreciate you getting indeed considerate and also for considering some great resources millions of individuals are really desperate to be aware of. Our honest regret for not saying thanks to you earlier.

  47. My spouse and i got absolutely excited when Raymond managed to deal with his web research while using the precious recommendations he was given from your very own web page. It’s not at all simplistic to simply find yourself giving for free solutions that many many people could have been selling. We really take into account we need the writer to thank because of that. The specific explanations you’ve made, the simple blog menu, the friendships your site make it easier to foster – it is most superb, and it is aiding our son in addition to us understand that content is entertaining, and that’s quite indispensable. Many thanks for the whole thing!

  48. Hi, Neat post. There is an issue with your website in internet explorer, could check this… IE still is the market chief and a big part of folks will miss your excellent writing due to this problem.

  49. Good day! I simply wish to give an enormous thumbs up for the nice info you will have right here on this post. I can be coming again to your blog for more soon.

  50. Wonderful beat ! I wish to apprentice while you amend your web site, how can i subscribe for a weblog site? The account aided me a appropriate deal. I had been a little bit acquainted of this your broadcast provided brilliant transparent idea

  51. I constantly read through your site content thoroughly. I am also thinking about made to measure upvc windows, you could talk about this sometimes. See you soon!

  52. Thanks for another informative site. The place else could I am getting that kind of info written in such a perfect approach? I’ve a project that I’m simply now operating on, and I have been on the glance out for such info.

  53. Excellent items from you, man. I have understand your stuff previous to and you are simply extremely wonderful. I actually like what you have obtained here, certainly like what you’re stating and the way through which you are saying it. You’re making it enjoyable and you still care for to stay it wise. I can’t wait to read far more from you. This is actually a tremendous website.

Leave a Reply