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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:21:12+00:00 2026-06-15T04:21:12+00:00

Coming from the Java world, programming with generics and C# is often a headache.

  • 0

Coming from the Java world, programming with generics and C# is often a headache. Like this one:

interface ISomeObject { }
class SomeObjectA : ISomeObject { }
class SomeObjectB : ISomeObject { }


interface ISomething<T> where T : ISomeObject
{
    T GetObject();
}
class SomethingA : ISomething<SomeObjectA>
{
    public SomeObjectA GetObject() { return new SomeObjectA(); }
}
class SomethingB : ISomething<SomeObjectB>
{
    public SomeObjectB GetObject() { return new SomeObjectB(); }
}


class SomeContainer
{

    private ISomething<ISomeObject> Something;

    public void SetSomething<T>(ISomething<T> s) where T : ISomeObject
    {
        Something = (ISomething<ISomeObject>)s;
    }
}


class TestContainerSomething
{
    static public void Test()
    {
        SomeContainer Container = new SomeContainer();
        Container.SetSomething<SomeObjectA>(new SomethingA());
    }
}

Which results into an InvalidCastException at Something = (ISomething<ISomeObject>)s;. In Java, this would work, and I could even use (if all else fails) the generics wildcard <?>. This is not possible in C#.

While this is just an example that I put together to explain the problematic, how can this exception be eliminated? The only main constraint is that SomeContainer cannot be a generic class

** Note ** : there are many questions about this, but none of them (that I could find) address a generic class member inside a non generic class.

** Update **

Inside the method SetSomething, I added these lines :

Console.WriteLine(s.GetType().IsSubclassOf(typeof(ISomething<SomeObjectA>)));
Console.WriteLine(s.GetType().ToString() + " : " + s.GetType().BaseType.ToString());
foreach (var i in s.GetType().GetInterfaces())
{
    Console.WriteLine(i.ToString());
}

which to my surprise output

False
SomeThingA : System.Object
ISomething`1[SomeObjectA]

Is this why I get this exception?

  • 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-15T04:21:13+00:00Added an answer on June 15, 2026 at 4:21 am

    Out keyword will be a fix, if your ISomething only have methods that return T

    interface ISomething<out T> where T : ISomeObject
    

    when creating a generic interface, you can specify whether there is an implicit conversion between interface instances that have different type arguments.

    It is called Covariance and Contravariance

    Eric Lippert have a good series of articles why we need to think about this, here interface variance is used

    Here is my code, which works as expected for me

    interface ISomeObject { }
    class SomeObjectA : ISomeObject { }
    class SomeObjectB : ISomeObject { }
    
    
    interface ISomething<out T> where T : ISomeObject
    {
        T GetObject();
    }
    class SomethingA : ISomething<SomeObjectA>
    {
        public SomeObjectA GetObject() { return new SomeObjectA(); }
    }
    class SomethingB : ISomething<SomeObjectB>
    {
        public SomeObjectB GetObject() { return new SomeObjectB(); }
    }
    
    
    class SomeContainer
    {
    
        private ISomething<ISomeObject> Something;
    
        public void SetSomething<T>(ISomething<T> s) where T : ISomeObject
        {
            Something = (ISomething<ISomeObject>)s;
        }
    }
    
    
    class TestContainerSomething
    {
        static public void Test()
        {
            SomeContainer Container = new SomeContainer();
            Container.SetSomething<SomeObjectA>(new SomethingA());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand new to java, coming from a ruby world. One thing I love
I'm currently learning ObjC and Cocoa programming, coming from the Java world. To test
Coming from the Java world to the iPhone, I would like to see the
I am coming from the Java world and keep hearing this term 'worker' in
I'm coming from the Java world and are building a small c++ program at
Coming from the basic Java world I know there's a way to spawn a
First of all, I'm quite new to the Android and JAVA world (coming from
I'm comming from the java world, and i'd like to know if there's anything
I am coming from Java to Ruby and this -7 mod 3 = 2
Coming from Java background I am guessing this is expected. I would really love

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.