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 9090095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:17:51+00:00 2026-06-16T22:17:51+00:00

EDITED: Reconstructed the sample code to include a more relevant set of objects. I

  • 0

EDITED: Reconstructed the sample code to include a more relevant set of objects.

I have an interesting situation that I’m having trouble finding a solution for. I have an abstract class with an abstract function that uses generics (see example code below). In an inheriting class, I’m trying to overload the function, but I’m getting

Error CS0030: Cannot convert type ‘Sample.Thingy’ to ‘T’ (CS0030)

Naturally, this isn’t my real code, but this code produces the same result as what I’m getting. If I try casting the return value to (T), I get a similar error. If I try adding where T : BaseThingy or where T : Thingy, then I get

Error CS0460: ‘Sample.Container.GetThingy(Guid)’: Cannot specify constraints for overrides and explicit interface implementation methods (CS0460)

namespace Sample {
    // The abstract base class for thingies
    public abstract class BaseThingy {
        private Guid m_ID;
        private String m_Name;

        public BaseThingy( ) {
            m_ID = Guid.NewGuid( );
        }

        public BaseThingy( Guid id ) {
            m_ID = id;
        }

        public Guid ID {
            get {
                return m_ID;
            }
        }

        public String Name {
            get {
                return m_Name;
            }
            set {
                m_Name = value;
            }
        }
    }

    // The abstract base class for containers
    public abstract class BaseContainer {
        public abstract T GetThingy<T>(Guid id) where T : BaseThingy;
    }

    // Inherits from BaseThingy
    public class RedThingy : BaseThingy {
        private DateTime m_Created;

        public RedThingy( ) : base( ) {
            m_Created = DateTime.Now;
        }

        public RedThingy( Guid id ) : base( id ) {
            m_Created = DateTime.Now;
        }

        public DateTime Created {
            get {
                return m_Created;
            }
        }
    }

    // Inherits from BaseThingy
    public class BlueThingy : BaseThingy {
        public BlueThingy( ) : base( ) {
        }

        public BlueThingy( Guid id ) : base( id ) {
        }
    }

    // Inherits from BaseContainer
    public class Container : BaseContainer {
        private System.Collections.Generic.Dictionary<Guid, RedThingy> m_RedThingies;
        private System.Collections.Generic.Dictionary<Guid, BlueThingy> m_BlueThingies;

        public Container( ) {
            m_Thingies = new System.Collections.Generic.Dictionary<Guid, BaseThingy>();
        }

        public override T GetThingy<T>( Guid id ) where T : BaseThingy {
            if( typeof( T ) == typeof( RedThingy ) {
                if( m_RedThingies.ContainsKey( id ) ) {
                    return m_RedThingies[ id ];
                } else {
                    return null;
                }
            } else if( typeof( T ) == typeof( BlueThingy ) ) {
                if( m_BlueThingies.ContainsKey( id ) ) {
                    return m_BlueThingies[ id ];
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }

        public void AddThing( RedThingy item ) {
            if( item != null && !m_RedThingies.ContainsKey( item.ID ) ) {
                m_RedThingies.Add( item.ID, item );
            }
        }

        public void AddThing( BlueThingy item ) {
            if( item != null && !m_BlueThingies.ContainsKey( item.ID ) ) {
                m_BlueThingies.Add( item.ID, item );
            }
        }
    }
}
  • 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-06-16T22:17:51+00:00Added an answer on June 16, 2026 at 10:17 pm

    The answer is that the type you pass has to inherit from BaseThingy but it doesn’t imply that you can cast a Thingy to it. Let me make an example to simplify it:

    abstract class BaseThingy
    {
    }
    
    class Thingy1 : BaseThingy
    {
        public void DoSomething()
        {
        }
    }
    
    class Thingy2 : BaseThingy
    {
        public void DoSomethingElse()
        {
        }
    }
    
    class Foo
    {
        static T GetItem<T>() where T : BaseThingy
        {
            //Won't compile, Thingy1, while deriving from BaseThingy
            //Could not be the same type of T, derive from it or have
            //An implicit cast operator to T.
            return new Thingy1();
        }
    
        static void Bar()
        {
            var result = GetItem<Thingy2>();
            //But your method is returning a Thingy1,
            //which doesn't have the following method
            result.DoSomethingElse();
        }
    }
    

    Bar() will expect result to be of type Thingy2 but GetItem is trying to return a Thingy1.

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

Sidebar

Related Questions

Edited for a larger tree, for more examples. I have a tree structure that
[EDITED] This issue was originally described as my having trouble with Perl's Socket, but
[EDITED: I left the original question below, with some more context and code to
Edited 05-02-2013. I have updated the curl code. I have build a small class
[EDITED - really sorry, the code I quoted was wrong - have changed the
-EDITED THE MAIN CODE IN THIS BLOCK- This code I am writing is having
[Edited/Updated] Allow me to explain the situation. I have three files: Below is db.class.php.
Edited to include SSCE I have a Proc object which I add to a
*edited 6/17/10 I'm trying to understand how to improve my code (make it more
Edited because I'm a moron. Should have said class originally. I have the code

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.