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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:06:12+00:00 2026-05-31T16:06:12+00:00

I read about in C#, covariance and contravariance enable implicit reference conversion for array

  • 0

I read about in C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments.

I’m wondering can I use the covariance and contravariance for Anonymous Types (which are class types derive directly from object) and how would that works ?

  • 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-31T16:06:13+00:00Added an answer on May 31, 2026 at 4:06 pm

    Can I use covariance and contravariance of generic interfaces and delegates with anonymous types?

    Yes. Anonymous types are reference types; variance only works with reference types.

    How?

    Interface covariance:

    var sequenceOfAnonymous = from c in customers select new {c.Name, c.Age};
    var sequenceOfObject = (IEnumerable<object>)sequenceOfAnonymous;
    

    Array covariance:

    var arrayOfAnonymous = sequenceOfAnonymous.ToArray();
    var arrayOfObject = (object[]) arrayOfAnonymous;
    

    To demonstrate delegate covariance you need to use a generic type inference trick:

    static Func<R> MakeFunc(Func<R> f) { return f; }
    ...
    var funcOfAnonymous = MakeFunc( ()=>new { X = 123 } );
    var funcOfObject = (Func<object>)funcOfAnonymous;
    

    Interface contravariance needs a slightly different trick: casting by example:

    interface IFrobber<in T> { void Frob(T t); }
    class Frobber<T> : IFrobber<T> 
    { 
        public void Frob(T t) { Console.WriteLine(t); }
    }
    ...
    static IFrobber<T> FrobByExample<T>(IFrobber<T> frobber, T example) 
    { return frobber; }
    ...
    var frobberOfObject = new Frobber<object>();
    var frobberOfAnonymous = FrobByExample(frobberOfObject, new { X = 0 });
    

    And similarly for delegate contravariance:

    static Action<A> ActionByExample<A>(Action<A> action, A example) 
    { return action; }
    ...
    var actionOfObject = (Action<object>) x => { Console.WriteLine(x); }
    var actionOfAnonymous = ActionByExample(actionOfObject, new { X = 0 } );
    

    Make sense?

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

Sidebar

Related Questions

I read about implicit conversions being done between different value class types. The book
Today, I read some articles about Covariance, Contravariance (and Invariance) in Java. I read
I read about using <context:component-scan base-package=tld.mydomain.business> <context:include-filter type=annotation expression=org.springframework.stereotype.Service/> </context:component-scan> and annotate my service
First of, I have read many explanations on SO and blogs about covariance and
I read about the Dispatcher Servlet on the Spring Source website. http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html My question
I read about Change the delegate of MGTwitterEngine and don't really get it. I
I read about conversion of ASCII to EBCDIC using this link; Convert String from
I read about Compound property names in the The Spring Framework (2.5) - Reference
I read about this Article I wondering about this part: try { //First try
I read about OpenBTS it's really amazing... but I was wondering if we can

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.