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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:42:08+00:00 2026-05-23T01:42:08+00:00

abstract class A<T> where T:A<T> { public event Action<T> Event1; } class B :

  • 0
abstract class A<T> where T:A<T>
{
    public event Action<T> Event1;
}

class B : A<B>
{
    //has a field called Action<B> Event1;
}

Is there a more elegant way to do this? I want stuff (events, etc) in the base class to be able to use the subclass’ type.

  • 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-23T01:42:08+00:00Added an answer on May 23, 2026 at 1:42 am

    The pattern you are using does not actually implement the constraint you want. Suppose you want to model “an animal can only be friendly with something of its own kind”:

    abstract class Animal<T> where T : Animal<T>
    {
        public abstract void GetFriendly(T t);
    }
    
    class Cat : Animal<Cat>
    {
        public override void GetFriendly(Cat cat) {}
    }
    

    Have we succeeded in implementing the desired constraint? No.

    class EvilDog : Animal<Cat>
    {
        public override void GetFriendly(Cat cat) {}
    }
    

    Now an evil dog can be friendly with any Cat, and not friendly with other evil dogs.

    The type constraint you want is not possible in the C# type system. Try Haskell if you need this sort of constraint enforced by the type system.

    See my article on this subject for more details:

    http://blogs.msdn.com/b/ericlippert/archive/2011/02/03/curiouser-and-curiouser.aspx

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

Sidebar

Related Questions

Let's say I have defined the following class: public abstract class Event { public
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have the following code public abstract class Event { public void fire(Object... args)
I have the following class: public abstract class AbstractParent { static String method() {
Here's a simplified version of my class: public abstract class Task { private static
public abstract class Master { public void printForAllMethodsInSubClass() { System.out.println (Printing before subclass method
public abstract class Request { public class Parameters { //Threre are no members here
I have an abstract base class and derived class: type TInterfaceMethod = class public
I have an abstract class: type TInterfaceMethod = class abstract public destructor Destroy; virtual;
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS

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.