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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:09:49+00:00 2026-06-03T03:09:49+00:00

I have this function: static void Func1<T>(T x, T y) { dynamic result =

  • 0

I have this function:

static void Func1<T>(T x, T y)
{
    dynamic result = ((dynamic)x + y); //line 1
    dynamic result2 = (x + y);         //line 2
}

This func can be executed as Func(1,2); However, line 1 is OK, while line 2 goes BANG (at compile time).

The exception thrown from line 2 is:

Operator ‘+’ cannot be applied to operands of type ‘T’ and ‘T’

So, we need to create an operator overload. Okay, so far so good.

But what about line 1? Shouldn’t it need a dynamic cast also on y?

((dynamic)x + (dynamic)y);

I understand that it is being evaluated at runtime, but why does the C# compiler accept the + operator in line 1 (i.e. wrongly assume that T can be + to something else)?

  • 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-03T03:09:49+00:00Added an answer on June 3, 2026 at 3:09 am

    In your first example, by making x a dynamic you’ve in effect made the operator+ operation dynamic as well. This gets rid of the type specifier T for x, thereby getting rid of the complaint that T has no valid operator+.

    At run time dynamic binding will occur and evaluate the two operands to ensure that operator+ can be used:

    If an operand of an arithmetic operator has the compile-time type dynamic, then the expression is dynamically bound (§7.2.2). In this case the compile-time type of the expression is dynamic, and the resolution described below will take place at run-time using the run-time type of those operands that have the compile-time type dynamic.

    In your second example, the compiler knows the types for x + y and is simply storing the result into a dynamic variable. Further usages of result2 will be dynamically bound. This makes sense as there are no dynamic operations to the right of the assignment operator:

    When no dynamic expressions are involved, C# defaults to static binding, which means that the compile-time types of constituent expressions are used in the selection process.

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

Sidebar

Related Questions

I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have this function; static public void Print(Object[] arr) { for(int i = 0;
I currently have this code: static void func( uint8_t var ); static volatile uint8_t
Say I have this small function in a source file static void foo() {}
Suppose I have two functions which look like this: public static void myFunction1(int a,
I have this static callback function in MyClass, and I try to call another
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have a function that I use to add vectors, like this: public static
I have this snippet of code: template<typename... Args> class function_helper<void(Args...)>{ public: typedef void(*generic_function)(Args...); static
I have two functions in my class with this signatures, public static TResult Execute<TResult>(Func<T,

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.