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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:57:32+00:00 2026-05-20T00:57:32+00:00

I have a class hierarchy like this public abstract class CalendarEventBase{} public class TrainingEvent

  • 0

I have a class hierarchy like this

public abstract class CalendarEventBase{}

public class TrainingEvent : CalendarEventBase{}

public class AuditEvent : CalendarEventBase{}

I wanted to create an action Action lamda that had a generic type paramater of type CalendarEventBase that I could assign to the following different methods:

public void EmailCancelation(TrainingEvent trainingEvent)

public void EmailCancelation(AuditEvent auditEvent)

I created the following illegal assignment:

Action<CalendarEventBase> emailCancelation = _trainingService.EmailTrainingCancellation;

The compiler complains that it was expecting a method with void(CalendarEventBase) as a signature. I was surprised by this as I thought it would accept a more derived type.

To get round this, I created the following delegate that allows me to complete my task:

public delegate void EmailCancelation<in T>(T calendarEvent) where T : CalendarEventBase;

My question is, could I have completed the task without having to create an additional delegate? I thought I could just create an Action instance.

Any help or pointers, greatly appreciated.

  • 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-20T00:57:33+00:00Added an answer on May 20, 2026 at 12:57 am

    The line:

    Action<CalendarEventBase> emailCancelation = _trainingService.EmailTrainingCancellation;
    

    is actually expecting covariance, not contravariance. But that logically doesn’t make sense; the method expects a TrainingEvent as input – how can you pass a more general type (CalendarEventBase) to it?

    This isn’t legal:

    // What if the method wants to make the lion roar but you pass in a goat?
    Action<Mammal> mammalAction = MethodThatTakesALion; 
    

    but this is fine:

    // Anything that you want to with an animal, you can do with a mammal.
    Action<Mammal> mammalAction = MethodThatTakesAnAnimal; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# I have an intrusive tree structure that looks like this: public abstract
I have a class hierarchy like this: @Entity @Table (name=call_distribution_policies) @Inheritance (strategy=InheritanceType.JOINED) public class
I have a class hierarchy, this one: type TMatrix = class protected //... public
I had a class hierarchy setup like so: public abstract class GameController public abstract
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
Here's the deal. I have a big class hierarchy and I have this one
I have hierarchy: public class Parameter { public string Name { get; set; }
I have following classes: public abstract class CustomerBase { public long CustomerNumber { get;
I need to create an object in one class hierarchy that wraps an object
I have a class hierarchy as such: +-- VirtualNode | INode --+ +-- SiteNode

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.