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

The Archive Base Latest Questions

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

Why can’t open generic types be passed as parameters. I frequently have classes like:

  • 0

Why can’t open generic types be passed as parameters. I frequently have classes like:

public class Example<T> where T: BaseClass
{
   public int a {get; set;}
   public List<T> mylist {get; set;}
}

Lets say BaseClass is as follows;

public BaseClass
{
    public int num;
}

I then want a method of say:

public int MyArbitarySumMethod(Example example)//This won't compile Example not closed
{
   int sum = 0;
   foreach(BaseClass i in example.myList)//myList being infered as an IEnumerable
       sum += i.num;
   sum = sum * example.a;
   return sum;
}

I then have to write an interface just to pass this one class as a parameter as follows:

public interface IExample
{
public int a {get; set;}
public IEnumerable<BaseClass> myIEnum {get;}
}

The generic class then has to be modified to:

public class Example<T>: IExample where T: BaseClass
{
   public int a {get; set;}
   public List<T> mylist {get; set;}
   public IEnumerable<BaseClass> myIEnum {get {return myList;} }
}

That’s a lot of ceremony for what I would have thought the compiler could infer. Even if something can’t be changed I find it psychologically very helpful if I know the reasons / justifications for the absence of Syntax short cuts.

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

    I have been in similar situation, but I never had this (very good!) question. Now that I am forced to think about it, here is my answer:

    You expect the following to work:

    void F(Example<> e) {
       Console.WriteLine(e.a); //could work
    }
    

    Yeah, this could theoretically work, but this won’t:

    void F(Example<> e) {
       Console.WriteLine(e.mylist); //?? type unknown
    }
    

    You expect the compiler and CLR to be able to analyze method bodies and proove that really no unsafe access is possible in the first case. That could be made to work. Why wasn’t it? Probably, the design is not really sound and confusing. Also, “all features are unimplemented by default. Somebody must implement, test and document them.”

    Edit: I want to point out that this feature cannot be implemented without cooperation from the CLR. The C# compiler must emit the exact method to be called which is not possible on an open generic type. The type system does not even allow such a variable right now.

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

Sidebar

Related Questions

Can anyone tell me how I can display a status message like 12 seconds
I have a jquery bug and I've been looking for hours now, I can't
Can anyone give me an example of how to return the following json simply
Can I run this in a Windows command prompt like I can run it
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can't figure out how to do this in a pretty way : I have
Can you have submenus with the top level set to checkable in WPF? I
Can someone explain dependency injection with a basic .NET example and provide a few
Can someone help me? I am trying to do something like the following: #include
Can I open my custom developed form [instead of native one] when user opens

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.