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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:48:06+00:00 2026-06-13T07:48:06+00:00

In class we have being dealing with generics and were asked to complete an

  • 0

In class we have being dealing with generics and were asked to complete an assignment.

We created an Account<T> class with one property private T _balance; and then had to write methods to credit and debit _balance.

Credit method (partial) called from Main by e.g. acc1.Credit(4.6);:

    public void Credit(T credit)
    {
        Object creditObject = credit;
        Object balanceObject = _balance;

        Type creditType = creditObject.GetType();
        Type balanceType = balanceObject.GetType();

        if(creditType.Equals(balanceType))
        {
            if(creditType.Equals(typeof (double)))
            {
                 balanceObject= (double)balanceObject + (double)creditObject;
            }
       ...WITH more else if's on int,float and decimal.
        }
        _balance = (T)balanceObject;   
    }

I had to condition check and cast as I cannot _balance += (T)balanceObject; as this will give the error "Operator '+' cannot be applied to operand of type 'T'"

During my reading on the subject I discovered the dynamic type. In my new Account class I added a new method and changed the Credit method to: (called from Main by e.g. acc1.Credit(4.6);)

    public void Credit(dynamic credit)
    {
        _balance += ConvertType(credit);
    }
    public T ConvertType(object input)
    {
        return (T)Convert.ChangeType(input, typeof(T));
    }

This is what I don’t understand. The credit method takes in the object as type dynamic and the ConvertType(object input) returns it as type T.
Why does using dynamic type allow me to use operators on generics?

  • 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-13T07:48:07+00:00Added an answer on June 13, 2026 at 7:48 am

    When using dynamic types, resolution is deferred until runtime. If, at runtime, the generic type supports a + operator, your code will work. If not, it will throw an exception.

    From a MSDN article on dynamic:

    At compile time, an element that is typed as dynamic is assumed to
    support any operation.

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

Sidebar

Related Questions

I have a class being used in node.js, and I want to pass various
(I'm targetting iOS 2.0 for the time being.) I have a class which subclasses
I have a class that is in an assembly due to being required for
I have a class that requires a specific method to be called before being
I have a singleton class which is being used throughout the app. I am
I have a Uri object being passed to a constructor of my class. I
I have a lot of comparer classes where the class being compared is simply
i make one class file for jar. and this class have use wurfl. and
I have two buttons in one class and those buttons send you to the
At the company I work for, I have created a Error Logging class to

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.