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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:29:01+00:00 2026-05-31T10:29:01+00:00

I have a C# method something like: private static IEnumerable<T2> SomeMethod<T1, T2>(IEnumerable<T1> arg1) {

  • 0

I have a C# method something like:

private static IEnumerable<T2> SomeMethod<T1, T2>(IEnumerable<T1> arg1)
{

}

and I would like to be able to do some conditional logic so that if T1 is A then T2 is B and so on. This is not a problem if I just have those two constraints. However, what if I also wanted logic such as if T1 is C then T2 is D.

The check can be done in the method of course, but a compile time check would be preferable.
What I would like is to specify this in the generic where statement of the method.

Is there any way to do this in C# or any other .net language?

For more information T1 is basically a data object and T2 is a specifier that pertains to the T1 data object. All valid T1’s are a certain base class while all valid T2’s are a different base class. They are two different classes but are always used together.
The problem I am trying to avoid is having in the code data and a different specifier which doesn’t go with the data passed in together.

  • 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-31T10:29:02+00:00Added an answer on May 31, 2026 at 10:29 am

    It sounds like what you want just isn’t expressible in .NET constraints. It sounds like actually you might be better off with simple overloads… but we don’t really know enough to say.

    If a method is only suitable for a few type arguments – or a few combinations of type arguments – then it might not really be appropriate to be a generic method in the first place.

    EDIT: Given this information, it may make more sense:

    For more information T1 is basically a data object and T2 is a specifier that pertains to the T1 data object. All valid T1’s are a certain base class while all valid T2’s are a different base class.

    It’s not really clear what you mean by a “specifier” here, but it sounds like you might want T2 to be expressed in terms of an interface which relates it to T1. For example, if these are *actually “entity” and “decorator-for-entity” types, you might have:

    private static IEnumerable<T2> SomeMethod<T1, T2>(IEnumerable<T1> arg1)
        where T2 : IDecorator<T1>
    

    … where IDecorator<T> is a new interface. Again, there’s still not really enough information to say, but this sort of thing can be a valid approach.

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

Sidebar

Related Questions

Some existing web services I consume have methods that look something like this: List<Employee>
I have a method like this: private static double ComputePercentage(ushort level, ushort capacity) {
Suppose you have a private static method called Inst() which allows the class to
I have a static C# class that exposes some pre known Guids via properties
Currently, I have a static factory method like this: public static Book Create(BookCode code)
When using something like object.methods.sort.to_yaml I'd like to have irb interpret the \n characters
I have something similar to the following method: public ActionResult Details(int id) { var
I do have something more specific in mind, however: Each web service method needs
I have method in a class that I need to make sure is only
I have class method that returns a list of employees that I can iterate

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.