public void Method(BaseClass entry){ if (entry is not ExtendedClass extendedEntry) { return; } // `extendedEntry` is now of type ExtendedClass // if it can't be converted, break out of the function}
{"docsConfig":{"$schema":"https://mintlify.com/docs.json","theme":"mint","name":"adro.codes","colors":{"primary":"#f59e0b","light":"#d97706","dark":"#f59e0b"},"favicon":"/favicon.svg","logo":{"light":"https://mintcdn.com/adrocodes/4BK3kYLBrRFwX4OU/logo/light.svg?fit=max\u0026auto=format\u0026n=4BK3kYLBrRFwX4OU\u0026q=85\u0026s=3e7006b0daf93ea6749653421337e270","dark":"https://mintcdn.com/adrocodes/4BK3kYLBrRFwX4OU/logo/dark.svg?fit=max\u0026auto=format\u0026n=4BK3kYLBrRFwX4OU\u0026q=85\u0026s=58c210ae98d1bde91fca23cf733851f2"},"footer":{"socials":{"github":"https://github.com/adrocodes"}},"integrations":{"ga4":{"measurementId":"G-9CF5MG7673"}},"navigation":{"global":{"anchors":[{"anchor":"Papers","href":"https://papers.adro.codes","icon":{"name":"scroll","style":"duotone"}},{"anchor":"Github","href":"https://github.com/adrocodes","icon":{"name":"github","style":"duotone"}}]},"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"adrocodes","hasAudioNarration":false,"actualSubdomain":"adrocodes","customDomains":["adro.codes"],"gitSource":{"type":"github","owner":"adrocodes","repo":"mintlify-docs","deployBranch":"main","contentDirectory":"","isPrivate":false,"mountPath":"","label":""},"entitlements":{},"buildId":"6ae91dc15d9fe27156017997:success","clientVersion":"0.0.4160","searchFilterCounts":[{"language":"default","tag":"Home","count":21},{"language":"default","tag":"Tiny tips/tricks","count":20},{"language":"default","tag":"The Knowledge Vault","count":14},{"language":"default","tag":"The Vault","count":14},{"language":"default","tag":"Pigeon","count":12},{"language":"default","tag":"Software Design","count":6},{"language":"default","tag":"C#","count":5},{"language":"default","tag":"JavaScript","count":5},{"language":"default","tag":"Azure","count":2},{"language":"default","tag":"Godot","count":1}]},"assistantConfig":{},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[],"googleFontsInlined":false},"customCss":[],"page":{"pagePath":"how-to/csharp/condition-based-type-casting","slug":"/how-to/csharp/condition-based-type-casting","pageMetadata":{"title":"Condition based type casting","description":null,"href":"/how-to/csharp/condition-based-type-casting"},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={code:`code`,pre:`pre`,span:`span`,..._provideComponents(),...props.components},{CodeBlock}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);return _jsx(CodeBlock,{filename:``,numberOfLines:`9`,language:`csharp`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`csharp`,children:_jsxs(_components.code,{language:`csharp`,numberOfLines:`9`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`public`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` void`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` Method`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#4EC9B0`},children:`BaseClass`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` entry`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`{`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` if`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`entry`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` is`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` not`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#4EC9B0`},children:` ExtendedClass`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` extendedEntry`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`) {`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` return`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` }`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:\" // `extendedEntry` is now of type ExtendedClass\"})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // if it can't be converted, break out of the function`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`}`})}),`\n`]})})})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Condition based type casting","description":null,"href":"/how-to/csharp/condition-based-type-casting"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[],"codeExamples":{}},"jsLinks":[],"subdomain":"adrocodes","siteName":"adro.codes","title":"Condition based type casting - adro.codes","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-01-06T23:51:30.066Z","originalFileLocation":"how-to/csharp/condition-based-type-casting.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"application-name\" content=\"adro.codes\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Condition based type casting - adro.codes\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"adro.codes\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://adro.codes/how-to/csharp/condition-based-type-casting\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://adrocodes.mintlify.site/_mintlify/api/og/v2?division=C%23\u0026amp;title=Condition+based+type+casting\u0026amp;theme=5a98e02ff7d6a3cbb90dbd12\u0026amp;signature=0ccc8a5bc49dfd7c5930d0823968f06ef7b54980fc07f364a7f95620691e6f95\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Condition based type casting - adro.codes\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://adrocodes.mintlify.site/_mintlify/api/og/v2?division=C%23\u0026amp;title=Condition+based+type+casting\u0026amp;theme=5a98e02ff7d6a3cbb90dbd12\u0026amp;signature=0ccc8a5bc49dfd7c5930d0823968f06ef7b54980fc07f364a7f95620691e6f95\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/adrocodes/KAEc8dn9Bt9KuuEF/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"adro.codes\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#f59e0b\">\u003clink data-mint-head rel=\"canonical\" href=\"https://adro.codes/how-to/csharp/condition-based-type-casting\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/how-to/csharp/condition-based-type-casting.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://adro.codes/#organization\",\"name\":\"adro.codes\",\"url\":\"https://adro.codes\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/adrocodes/4BK3kYLBrRFwX4OU/logo/light.svg?fit=max\u0026auto=format\u0026n=4BK3kYLBrRFwX4OU\u0026q=85\u0026s=3e7006b0daf93ea6749653421337e270\"}},{\"@type\":\"WebSite\",\"@id\":\"https://adro.codes#website\",\"name\":\"adro.codes\",\"url\":\"https://adro.codes\",\"publisher\":{\"@id\":\"https://adro.codes/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://adro.codes/how-to/csharp/condition-based-type-casting#webpage\",\"url\":\"https://adro.codes/how-to/csharp/condition-based-type-casting\",\"name\":\"Condition based type casting\",\"dateModified\":\"2026-01-06T23:51:30.066Z\",\"isPartOf\":{\"@id\":\"https://adro.codes#website\"},\"breadcrumb\":{\"@id\":\"https://adro.codes/how-to/csharp/condition-based-type-casting#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://adro.codes/how-to/csharp/condition-based-type-casting#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tiny tips/tricks\",\"item\":\"https://adro.codes/how-to/javascript/check-if-date-is-invalid\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C#\",\"item\":\"https://adro.codes/how-to/csharp/class-extensions\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Condition based type casting\",\"item\":\"https://adro.codes/how-to/csharp/condition-based-type-casting\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://adro.codes/how-to/csharp/condition-based-type-casting#article\",\"headline\":\"Condition based type casting\",\"name\":\"Condition based type casting\",\"url\":\"https://adro.codes/how-to/csharp/condition-based-type-casting\",\"mainEntityOfPage\":{\"@id\":\"https://adro.codes/how-to/csharp/condition-based-type-casting#webpage\"},\"dateModified\":\"2026-01-06T23:51:30.066Z\",\"publisher\":{\"@id\":\"https://adro.codes/#organization\"},\"isPartOf\":{\"@id\":\"https://adro.codes#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"adro.codes\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"adro.codes\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/how-to/csharp/condition-based-type-casting.md","siteName":"adro.codes","nav":{"global":{"anchors":[{"anchor":"Papers","href":"https://papers.adro.codes","icon":{"name":"scroll","style":"duotone"}},{"anchor":"Github","href":"https://github.com/adrocodes","icon":{"name":"github","style":"duotone"}}]},"tabs":[{"tab":"Home","groups":[{"group":"adro.codes","pages":[{"title":"Welcome","href":"/welcome"}]},{"group":"Tiny tips/tricks","pages":[{"group":"JavaScript","icon":"code","pages":[{"title":"Check if date is invalid","href":"/how-to/javascript/check-if-date-is-invalid"},{"title":"Check if two dates are the same day","href":"/how-to/javascript/check-if-two-dates-are-the-same-day"},{"title":"Get minutes elapsed since midnight","href":"/how-to/javascript/get-minutes-elapsed-since-midnight"},{"title":"Import JSON into your Typescript project","href":"/how-to/javascript/import-json-into-typescript-file"},{"title":"How to - Merge to SearchURLParams objects","href":"/how-to/javascript/merge-search-url-params"}]},{"group":"NextJS","icon":"triangle","pages":[{"title":"Respond with PDF from API route","href":"/how-to/next/respond-with-pdf-from-api-route"}]},{"group":"React","icon":"react","pages":[{"title":"Configure Context with a useReducer state management pattern.","href":"/how-to/react/context-with-use-reducer"}]},{"group":"C#","icon":"hashtag","pages":[{"title":"C# - Class Extension","description":"Use class extensions to add additional logic to a class without modifying the class itself.","href":"/how-to/csharp/class-extensions"},{"title":"Condition based type casting","href":"/how-to/csharp/condition-based-type-casting"},{"title":"Generics Refresher","description":"Sometimes I just need a reminder","href":"/how-to/csharp/generics-refresher"},{"title":"new gotcha","href":"/how-to/csharp/new-gotcha"},{"title":"TryParse","href":"/how-to/csharp/tryparse"}]},{"group":"Azure","icon":"cloud","pages":[{"title":"Generate a SAS token","href":"/how-to/azure/generate-a-sas-token"},{"title":"VSCode - Can't log into Tenant","href":"/how-to/azure/vscode-cant-log-into-tenant"}]},{"group":"SQL","icon":"database","pages":[{"title":"Get overfiew of table","href":"/how-to/sql/get-overview-of-table"}]},{"group":"Git","icon":"code-branch","pages":[{"title":"Add a tag to a branch","href":"/how-to/git/add-tag-to-branch"}]},{"group":"DatoCMS","icon":"folder-grid","pages":[{"title":"Search indexing - Failed to index","href":"/how-to/datocms/search-index-failed-to-index"}]},{"group":"ContentStack","icon":"folder-grid","pages":[{"title":"Optimising GraphQL queries","href":"/how-to/contentstack/optimising-graphql-queries"}]},{"group":"MacOS","icon":"apple","pages":[{"title":"Macbook - Show symbol explorer","href":"/how-to/macos/show-symbol-explorer"}]},{"group":"Godot","icon":"game-console-handheld","pages":[{"title":"Show custom editor warnings on Nodes","href":"/game-development/godot/godot-show-custom-editor-warnings"}]}]}]},{"tab":"The Knowledge Vault","pages":[{"title":"The Knowledge Vault","href":"/vault"}]},{"tab":"Software Design","pages":[{"title":"Software Design","href":"/software-design"}]},{"tab":"Pigeon","pages":[{"title":"Pigeon","href":"/pigeon"}]}]},"basePath":"","title":"Condition based type casting - adro.codes","buildId":"dpl_GRSyrvXT6FFTpZZTGz5iZPp3D2zE","deploymentId":"dpl_GRSyrvXT6FFTpZZTGz5iZPp3D2zE","dynamics":[],"chunks":[]}