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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:35:37+00:00 2026-05-24T21:35:37+00:00

I’m trying to implement the generic method which is intended for converting objects of

  • 0

I’m trying to implement the generic method which is intended for converting objects of Tuple<Descendant> type to objects of Tuple<Ancestor> type. I’ve stuck with a problem which seems to be a limitation of the C# language.

using System;

namespace Samples
{
    public static class TupleExtensions
    {
        public static Tuple<SuperOfT1> ToSuper<T1, SuperOfT1>(this Tuple<T1> target)
            where T1 : SuperOfT1
        {
            return new Tuple<SuperOfT1>(target.Item1);
        }
    }

    public interface Interface { }

    public class Class : Interface { }

    static class Program
    {
        static void Main()
        {
            var tupleWithClass = new Tuple<Class>(new Class());

            // Next instruction lead the compilation to error. :( The compiler doesn't try to infer types if at least one of generic type arguments is explicitly declared.
            var tupleWithInterfaceIncorrect = tupleWithClass.ToSuper<Interface>();

            // Next instruction is correct. But it looks ugly.
            var tupleWithInterfaceCorrect = tupleWithClass.ToSuper<Class, Interface>();

            // The code I try to write in my software is even worse:
            // I need to declare more types explicitly, because my actual tuple has more dimensions.
            // var myTupleInProduction = tuple.ToSuper<Class1<T>, Class2<T>, Interface1<T>, Interface2<T>>();
            // This code is VB.NET-like (too verbose). The problem is compounded by the fact that such code is used in many places.

            // Can I rewrite my TupleExtensions to provide more laconic code like that:
            // var myTupleInProduction = tuple.ToSuper<Interface1<T>, Interface2<T>>();

            Console.ReadKey();
        }
    }
}

Questions:

  1. Do you have any idea how to make this code working (take in account important comment in the code sample)?
  2. If no, then how would you recommend me to work it around? I want to keep the code simple and clear.
  • 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-24T21:35:37+00:00Added an answer on May 24, 2026 at 9:35 pm

    Assuming I’ve read your question correctly, you want to infer some type parameters but not others. That means you’ll need to already be in some sort of context which has the type parameters that can be inferred, so you can specify the others. You can do it like this:

    using System;
    
    public interface Interface { }
    
    public class Class : Interface { }
    
    
    public sealed class TupleHelper<T1>
    {
        private readonly Tuple<T1> tuple;
    
        internal TupleHelper(Tuple<T1> tuple)
        {
            this.tuple = tuple;
        }
    
        // Unfortunately you can't express the constraint the way 
        // round you want here...
        public Tuple<TSuper1> Super<TSuper1>()
        {
            return new Tuple<TSuper1>((TSuper1) (object) tuple.Item1);
        }
    }
    
    public static class TupleExtensions
    {
        public static TupleHelper<T1> To<T1>(this Tuple<T1> tuple)
        {
            return new TupleHelper<T1>(tuple);
        }
    }
    
    class Test
    {
        static void Main()
        {
    
            Tuple<Class> tupleWithClass = new Tuple<Class>(new Class());
    
            Tuple<Interface> tupleWithInterfaceIncorrect = 
                tupleWithClass.To().Super<Interface>();
        }
    }
    

    … but that doesn’t give you the constraint you want, because you can’t write where T1 : TSuper1.

    On the other hand, you could invert the operation, like this:

    using System;
    using System.Net;
    
    public interface Interface { }
    
    public class Class : Interface { }
    
    public static class TupleHelper<T1>
    {
        public static Tuple<T1> From<TDerived1>(Tuple<TDerived1> tuple)
            where TDerived1 : T1
        {
            return new Tuple<T1>(tuple.Item1);
        }
    }
    
    class Test
    {
        static void Main()
        {
    
            Tuple<Class> tupleWithClass = new Tuple<Class>(new Class());
    
            Tuple<Interface> tupleWithInterfaceIncorrect = 
                TupleHelper<Interface>.From(tupleWithClass);
        }
    }
    

    This looks simpler to me – but it does mean you can’t write it in a “fluent” way.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I used javascript for loading a picture on my website depending on which small
I am currently running into a problem where an element is coming back from
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.