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

  • SEARCH
  • Home
  • 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 4612960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:28:39+00:00 2026-05-22T01:28:39+00:00

Please look at simple code below public class A{} public class B: A{} public

  • 0

Please look at simple code below

public class A{}

  public class B: A{}

  public class G<T> where T : A
  {
    public T GetT()
    {
      return new A();
    }
  }

This code is incorrect – compiler error “Cannot convert A to return type T”.
But A is actually T. If I change

return new A(); 

to

 return new A() as T;

everything is ok. What is the reason of that behavior?
Thanks in advance

UPD: there was an error in initial question. Now fixed

  • 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. Editorial Team
    Editorial Team
    2026-05-22T01:28:40+00:00Added an answer on May 22, 2026 at 1:28 am

    Reworked answer based on update

    Although A meets the generic constraint where T : A, it’s a concrete type. However, your generic class’s GetT() method has a generic return type of T, so you have to cast your concrete type to your generic type to make the returns compatible.

    The old answer holds true for your previous case of returning new B().


    Old answer

    The generic type constraint says that T must inherit from A; however, it does not say that T must be B (or a derivation of it), although B happens to itself inherit from A and meet the constraint.

    So the return type isn’t compatible (B is always B, but T is not necessarily B), and you get the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please look at the simple php webpage code below how can modify my code
I have a query on this. Please look at the sample code below: UIButton
I am trying to achieve simple effects here and there. Please look this code
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Please take a look at the html listed below and let me know why
Please have a look to the following code: <?php $nomeDominio=''; if (isset($_GET['infoDominio'])) { $nomeDominio
I have just started to look at .NET 3.5 so please forgive me if
Please look at the following test page : sample page showing two images .
I am new to C and I am trying to create a simple C
Hi I have this simple array inside my variable with some colors var clr

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.