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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:55:32+00:00 2026-05-13T19:55:32+00:00

I have some example data: public struct Task { public int INT; public string

  • 0

I have some example data:

public struct Task
{
    public int INT;
    public string STRING;
    public DateTime? NULLABLEDATETIME;
}

And function, which uses it:

public KeyValuePair<Expression<Func<Task, object>>, object> Marry(Expression<Func<Task, object>> key, object value)
{
    return new KeyValuePair<Expression<Func<Task, object>>, object>(key, value);
}

Here is example of function call:

Marry(t => t.INT, 1984);
Marry(t => t.NULLABLEDATETIME, DateTime.Now);
Marry(t => t.STRING, "SomeSting");

This code works, no questions. Unfortunatly we can also call functions like below, because String and int are both inherited from object class:

Marry(t => t.INT, "SomeSting");

I want to say compiler, that first and second parameters have same data type: int -> int, string -> string, DateTime? -> DateTime? and check it during compilation. I tried this:

public KeyValuePair<Expression<Func<Task, T1>>, T2> Marry<T1, T2>(Expression<Func<Task, T1>> key, T2 value)
    where T2 : T1
{
    return new KeyValuePair<Expression<Func<Task, T1>>, T2>(key, value);
}

This almost works, and if I try to put wrong data like this Marry(t => t.INT, "SomeSting"); the compiler reports an error:
The type ‘string’ cannot be used as type parameter ‘T2’ in the generic type or method ‘Task.Marry(System.Linq.Expressions.Expression>, T2)’. There is no implicit reference conversion from ‘string’ to ‘int’.

But this solution does not work with null. When I call Marry(t => t.NULLABLEDATETIME, null); the compiler says:
The type arguments for method ‘Marry(System.Linq.Expressions.Expression>, T2)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

Why? I already know the data type: DateTime?. I don’t want to explicitly call Marry<DateTime?>(t => t.NULLABLEDATETIME, null);. How can I do this – or is there another way to check some function parameter types during compilation?

  • 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-13T19:55:32+00:00Added an answer on May 13, 2026 at 7:55 pm

    Why don’t you simply replace T1 and T2 with a single generic type parameter? Then the second parameters type needn’t be inferred – which it doesn’t have to be anyhow since it must be identical to that of the first parameter.

    That would look like this:

    Variant 1

    public KeyValuePair<Expression<Func<Task, T>>, T> 
        Marry<T>(
           Expression<Func<Task, T>> key, 
           T value) {
        return new KeyValuePair<Expression<Func<Task, T>>, T>(key, value);
    }
    

    I don’t recommend this, but if you want the type-inference to “greedily” determine the type of the expression based solely on the first parameter (so that you get the error “cannot convert from ‘string’ to ‘DateTime?’), you can curry the method:

    Variant 2

    public static Func<T, KeyValuePair<Expression<Func<Task, T>>, T> >
        Marry<T>(
            Expression<Func<Task, T>> key) {
        return (value=>new KeyValuePair<Expression<Func<Task, T>>, T>(key, value));
    }
    //usage:
    Marry(t => t.NULLABLEDATETIME)("test"); 
    //error: Delegate 'System.Func<System.DateTime?,System.Collections.Generic.KeyValuePair<System.Linq.Expressions.Expression<System.Func<UserQuery.Task,System.DateTime?>>,System.DateTime?>>' has some invalid arguments
    //  - Argument 1: cannot convert from 'string' to 'System.DateTime?'
    

    That error message essentially covers it. However, the API isn’t too readable and the usage of heavily nested generic types makes things… less readable. A longer variant with essentially the same behavior but shorter error messages could look as follows:

    Variant 3

    public class MarriagePartner<T> {
        readonly Expression<Func<Task, T>> key;
        public MarriagePartner(Expression<Func<Task, T>> key) { this.key = key; }
        public  KeyValuePair<Expression<Func<Task, T>>, T> With(T value) {
            return new KeyValuePair<Expression<Func<Task, T>>, T>(key, value);
        }
    }
    
    public static MarriagePartner<T> Marry2<T>(Expression<Func<Task, T>> key) { 
        return new MarriagePartner<T>(key);
    }
    
    //Usage:
    Marry2(t => t.NULLABLEDATETIME).With("test"); 
    //error: The best overloaded method match for 'UserQuery.MarriagePartner<System.DateTime?>.With(System.DateTime?)' has some invalid arguments
    //  - Argument 1: cannot convert from 'string' to 'System.DateTime?'
    

    That error message is a little more specific. I think Variant 1’s error message suffices and it’s what I’d use (KISS and all) – but if you Really want that casting error message, Variant 3 has the most understandable (if longer) implementation and most friendly error message.

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

Sidebar

Ask A Question

Stats

  • Questions 422k
  • Answers 422k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The problem is that your createGUI method is not static.… May 15, 2026 at 11:14 am
  • Editorial Team
    Editorial Team added an answer If the quadrilateral is convex, you can use this algorithm:… May 15, 2026 at 11:14 am
  • Editorial Team
    Editorial Team added an answer When you use ORDER BY with ROWNUM the ROWNUM is… May 15, 2026 at 11:14 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.