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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:31:47+00:00 2026-05-15T22:31:47+00:00

In the CLR via c# third edition there is an example that I cant

  • 0

In the CLR via c# third edition there is an example that I cant seem to make sense of:

Invariant Meaning that that generic
type parameter cannot be changed. I
have shown only invariant generic type
parameters so far in this chapter. n

Contravariant Meaning that the generic
type parameter can change from a class
to a class derived from it. In C#, you
indicate contravariant generic type
parameters with the in keyword.

Contravariant generic type parameters
can appear only in input positions
such as a method’s argument. n
Covariant Meaning that the generic
type argument can change from a class
to one of its base classes. In C#, you
indicate covariant generic type
parameters with the out keyword.
Covariant generic type parameters can
appear only in output positions such
as a method’s return type.

The author then goes on to give this example:

public delegate TResult Func<in T, out TResult>(T arg);

Here, the generic type parameter T is
marked with the in keyword, making it
contravariant; and the generic type
parameter TResult is marked with the
out keyword, making it covariant

Here is where I run into the issue on the following page(292) he then goes on the say the opposite when using an interface.

When using delegates that take generic arguments and return values, it is recommended to
always specify the in and out keywords for contravariance and covariance whenever >possible,
as doing this has no ill effects and enables your delegate to be used in more scenarios.
Like delegates, an interface with generic type parameters can have its type parameters be
contravariant or covariant. Here is an example of an interface with a contravariant >generic
type parameter:

public interface IEnumerator<out T> : IEnumerator {
Boolean MoveNext();
T Current { get; }
}

Since T is contravariant, it is possible to have the following code compile and run >successfully:

// This method accepts an IEnumerable of any reference type
Int32 Count(IEnumerable<Object> collection) { ... }
...
// The call below passes an IEnumerable<String> to Count
Int32 c = Count(new[] { "Grant" });

In the second example he uses the out key word (IEnumerator<out T>) and then calls it contravariant. Is this correct or am I missing something. Is there a difference defining a contravariant & covariant in an interface? I have been to Oreilly’s website regarding this book and this is not listed.

  • 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-15T22:31:48+00:00Added an answer on May 15, 2026 at 10:31 pm

    out = covariant and in = contravariant.

    Any words to the opposite are a mistake in my book which I’ll correct in a future edition.

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

Sidebar

Related Questions

In chapter eight (p189) of CLR Via C#, Jeffrey Richter states that: A Type
I've got a Clr user defined type that takes a string of values sepertated
In CLR via C# , Jeffrey Richter gives the following example of delegate chaining
CLR profiler does not seem to work with the Silverlight CLR. Does another memory
I have read-only access to a database that was set up for a third-party,
Reading CLR via C# 2.0 (I dont have 3.0 with me at the moment)
I'm re-reading CLR via C# right now and have some questions about garbage collection
Extract from CLR via C# on Boxing / Unboxing value types ... On Boxing:
I am reading CLR via C# by Jeffery Richter and it says a struct
In Jeffrey Richter's CLR via C# (the .net 2.0 edtion page, 353) he says

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.