{"id":5958,"date":"2022-07-27T04:37:24","date_gmt":"2022-07-27T04:37:24","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5958"},"modified":"2022-07-27T04:37:24","modified_gmt":"2022-07-27T04:37:24","slug":"type-casting-in-c","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/type-casting-in-c\/","title":{"rendered":"Type Casting in C#"},"content":{"rendered":"<p>Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a variable, we cannot declare it again. The value of a variable cannot be assigned to another type of variable until we change its type. <\/p>\n<p>Suppose we have a variable whose type is an integer and we need to pass it to a method parameter whose type is double. Such operations are called Type Conversion.<\/p>\n<p><strong><em>We have different types of conversions in C# i.e.,<\/em><\/strong><\/p>\n<ul>\n<li>\n<h2>IMPLICIT CONVERSION:<\/h2>\n<p> In this type of conversion, there is no loss of data. In this, the variables with smaller data types are converted to larger data types implicitly.<br \/>\nLet\u2019s look at an example of Implicit type conversion<br \/>\n<code>using System;<br \/>\nnamespace ImplicitConversion<br \/>\n{<br \/>\n    class Program<br \/>\n    {<br \/>\n      static void Main(string[] args)<br \/>\n            {<br \/>\n                int x = 530;<br \/>\n                int y = 461;<br \/>\n                long sum;<br \/>\n                sum = x + y;<br \/>\n                Console.WriteLine(\"The sum is = \" + sum);<br \/>\n                Console.ReadLine();<br \/>\n            }<br \/>\n    }<br \/>\n}<\/code><br \/>\n<strong>OUTPUT<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/code34.png\" alt=\"code34\"\/><\/p>\n<p>In this example, we have two variables of type integer i.e., x and y and we store the value of the sum of the two in a variable sum which is of type long. In this case, our output comes out to be fine as the data type integer gets implicitly converted to the larger data type i.e., long.<\/li>\n<li>\n<h3>EXPLICIT CONVERSION<\/h3>\n<p>This type of conversion is done when there is a loss of data. This is done with the help of the type cast operator(). This is done in case when we have to convert the larger data type to a smaller one. <\/p>\n<p><em>Let\u2019s look at an example of an Explicit Type of Conversion:<\/em><br \/>\n<code>using System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Linq;<br \/>\nusing System.Text;<br \/>\nusing System.Threading.Tasks;<br \/>\nnamespace ExplicitConversion<br \/>\n{<br \/>\n    class Program<br \/>\n    {<br \/>\n      static void Main(string[] args)<br \/>\n            {<br \/>\n                double d = 92.75;<br \/>\n                int z;<br \/>\n                \/\/ here we do the cast double to int.<br \/>\n                z = (int)d;<br \/>\n                Console.WriteLine(\"The value of z=\" + z);<br \/>\n                Console.ReadKey();<br \/>\n            }<br \/>\n    }<br \/>\n}<br \/>\n<\/code><\/p>\n<p><strong>OUTPUT<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/code35.png\" alt=\"code35\"\/>\n<\/li>\n<\/ul>\n<p>Here in the example the variable d which is of long data type is converted to an integer and is rounded off to the closest figure.<\/p>\n<p><small><em>Knowledge of .NET is quite rewarding in the IT industry. If you have got some skills in the .NET framework then a <a href=\"https:\/\/www.studysection.com\/c-sharp-net-web-developer-advanced-diploma\">.NET certification<\/a> from StudySection can prove to be a good attachment with your resume. You can go for a foundation level certificate as well as an advanced level certificate in the .NET framework.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a<\/p>\n","protected":false},"author":1,"featured_media":5959,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[142,711],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Type Casting in C - StudySection Blog<\/title>\n<meta name=\"description\" content=\"Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a variable.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/type-casting-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Type Casting in C - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a variable.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/type-casting-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-27T04:37:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/Type-Casting.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/type-casting-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/type-casting-in-c\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Type Casting in C#\",\"datePublished\":\"2022-07-27T04:37:24+00:00\",\"dateModified\":\"2022-07-27T04:37:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/type-casting-in-c\/\"},\"wordCount\":326,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"C\",\"Casting\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/type-casting-in-c\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/type-casting-in-c\/\",\"url\":\"https:\/\/studysection.com\/blog\/type-casting-in-c\/\",\"name\":\"Type Casting in C - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-07-27T04:37:24+00:00\",\"dateModified\":\"2022-07-27T04:37:24+00:00\",\"description\":\"Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a variable.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/type-casting-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/type-casting-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/type-casting-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Type Casting in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Type Casting in C - StudySection Blog","description":"Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a variable.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/type-casting-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Type Casting in C - StudySection Blog","og_description":"Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a variable.","og_url":"https:\/\/studysection.com\/blog\/type-casting-in-c\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-07-27T04:37:24+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/07\/Type-Casting.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/type-casting-in-c\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/type-casting-in-c\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Type Casting in C#","datePublished":"2022-07-27T04:37:24+00:00","dateModified":"2022-07-27T04:37:24+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/type-casting-in-c\/"},"wordCount":326,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["C","Casting"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/type-casting-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/type-casting-in-c\/","url":"https:\/\/studysection.com\/blog\/type-casting-in-c\/","name":"Type Casting in C - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-07-27T04:37:24+00:00","dateModified":"2022-07-27T04:37:24+00:00","description":"Conversion of the data type of a variable to another type is called Type Casting. After the declaration of a variable.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/type-casting-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/type-casting-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/type-casting-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Type Casting in C#"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":187,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5958"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5958"}],"version-history":[{"count":2,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5958\/revisions"}],"predecessor-version":[{"id":5963,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5958\/revisions\/5963"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5959"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}