Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 55963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:27:36+00:00 2026-05-10T17:27:36+00:00

I have a class that has a Generic type G In my class model

  • 0

I have a class that has a Generic type ‘G’

In my class model i have

public class DetailElement : ElementDefinition 

Let’s say i have a method like this

        public void DoSomething<G>(G generic)             where G : ElementDefinition         {             if (generic is DetailElement)             {                 ((DetailElement)generic).DescEN = 'Hello people'; //line 1                 //////                 ElementDefinition element = generic;                 ((DetailElement)element).DescEN = 'Hello again'; //line 3                 //////                 (generic as DetailElement).DescEN = 'Howdy'; //line 5             }             else             {                 //do other stuff             }         } 

Compiler reports one error in line 1:

Cannot convert type 'G' to 'DetailElement' 

But line 3 works fine. I can workaround this issue by doing the code written in line 5.

What i would like to know is why does the compiler reports the error in line 1 and not the one in line 3, given that, as far as i know, they are identical.

edit: I am afraid i might be missing some important piece of the framework logic

edit2: Although solutions for the compiler error are important, my question is about why the compiler reports an error on line 1 and not in line 3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:27:37+00:00Added an answer on May 10, 2026 at 5:27 pm

    If G was constrained to be a DetailElement (where G : DetailElement) then you can go ahead and cast G to ElementDefinition, i.e., ‘(ElementDefinition) generic‘. But because G might be another subclass of ElementDefinition other than DetailElement at run-time it won’t allow it at compile-time where the type is unknown and unverifiable.

    In line 3 the type you cast from is known to be an ElementDefinition so all you’re doing is an up-cast. The compiler doesn’t know if it will be a succcesful cast at run-time but it will trust you there. The compiler is not so trusting for generics.

    The as operator in line 5 might also return null and the compiler doesn’t statically check the type to see if it’s safe in that case. You can use as with any type, not just ones that are compatible with ElementDefinition.

    From Can I Cast to and from Generic Type Parameters? on MSDN:

    The compiler will only let you implicitly cast generic type parameters to object, or to constraint-specified types.

    Such implicit casting is of course type safe, because any incompatibility is discovered at compile-time.

    The compiler will let you explicitly cast generic type parameters to any interface, but not to a class:

       interface ISomeInterface {...}    class SomeClass {...}    class MyClass<T>      {       void SomeMethod(T t)        {          ISomeInterface obj1 = (ISomeInterface)t;//Compiles          SomeClass      obj2 = (SomeClass)t;     //Does not compile        }     } 

    However, you can force a cast from a generic type parameter to any other type using a temporary object variable

     void SomeMethod<T>(T t)    { object temp = t;     MyOtherClass obj = (MyOtherClass)temp;     } 

    Needless to say, such explicit casting is dangerous because it may throw an exception at run-time if the concrete type used instead of the generic type parameter does not derive from the type you explicitly cast to.

    Instead of risking a casting exception, a better approach is to use the is or as operators. The is operator returns true if the generic type parameter is of the queried type, and as will perform a cast if the types are compatible, and will return null otherwise.

    public void SomeMethod(T t)  {    if(t is int) {...}     string str = t as string;    if(str != null) {...}  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are often two reasons why they would use strings… May 11, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer UPDATE `group` SET `group`.`count` = ( SELECT COUNT(*) FROM `group_person`… May 11, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer This test will fail because there is no expectation that… May 11, 2026 at 8:26 pm

Related Questions

I have a problem with WCF. I think I understand what the issue is,
Suppose I have an interface for a service: public interface IFooService { void DoSomething();
It looks like I had a fundamental misunderstanding about C++ :< I like the
Here's what I'm trying to do, and failing... I have a File model which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.