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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:14:34+00:00 2026-06-16T01:14:34+00:00

Is it possible to use Generic mechanism within function body? for exammple if (!(someClass

  • 0

Is it possible to use Generic mechanism within function body?

for exammple

if (!(someClass is IClass<T, G> where T : someInterface, G : anotherInterface))
{
    return;
 }

or do casting like this:

var v = (IClass <T, G> where T : someInterface, G: anotherInterface)something;
  • 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-06-16T01:14:36+00:00Added an answer on June 16, 2026 at 1:14 am

    You can do this, but you have to make sure your interface is covariant:

    interface IClass<out T, out S>
    {
        // Methods that can return a T or S but not accept one as input
    }
    

    By marking the type parameters as out, you are basically saying “I will only ever get a T or an S out of this interface”. For example, IEnumerable<out T> as you can only get a T out of it, but only List<T> because you can put a T into a list as well as get one out.

    Having defined your interface as such, an IClass<string, string> is an IClass<object, object>: you know your IClass<string, string> will only ever give you a string, but since a string is an object then that’s fine, and if you assign it to an IClass<object, object> you know it will only ever give you an object.

    (You can’t do this if you interface allows you to put a T or an S into something. If this was the case, and you assigned your IClass<string, string> to an IClass<object, object>, you could try to put an int into it and it would fail, because the underlying class only really accepts a string.)

    What this then lets you do is

    if (!(something is IClass<object, object>))
    {
        return;
    }
    

    or

    var v = (IClass<object, object>)something;
    

    and both will work if something is actually an object that implements, say, IClass<string, string>.

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

Sidebar

Related Questions

Is it possible to use django class based generic views with a ManyToManyField relation?
Is this possible to use Ajax.Beginform with update target inside of ajax form. like
Is it possible to use generic support with single table inheritance, and still be
in .net, if I have a generic class SomeClass<T> , is it possible to
Is it possible to use generic methods with WCF or ADO.NET DS? Or is
This is a two part question. First, is it possible use a generic defined
Is it possible to use Zend_Filter_Input as a generic input filter? I want to
I would like to ask if it is possible to use the entities defined
Is it possible to use a generic model in ASP.NET MVC 3 (w/ Razor)?
I want to come up with a generic schema (if possible) to use for

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.