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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:00:40+00:00 2026-05-20T05:00:40+00:00

I am (trying) to learn about generics and I thought I understood them. I

  • 0

I am (trying) to learn about generics and I thought I understood them. I have the following code for the generic class:

    /// <summary>
    /// generics
    /// </summary>
    class Point<T> where T : IConvertible
    {
        public T X;


        NumberFormatInfo nf = NumberFormatInfo.CurrentInfo;

        public double Subtract(Point<T> pt)
        {
            return (X.ToDouble(nf) + pt.X.ToDouble(nf));
        }

    }

and in the Main(), I can create the objects fine:

        Point<int> pt = new Point<int>();
        Point<double> pt2 = new Point<double>();
        Point<string> pt3 = new Point<string>();

        pt.X = 10;

        pt2.X = 10;

        pt3.X = "10";

Now, I can run the Subtract Method on two ints, doubles or even strings, but I can’t run on mixed, which I thought I would be able to because of the .ToDouble conversion.

If I try to run Console.WriteLine(pt.Subtract(pt2)); I get the following errors:

Error   1   The best overloaded method match for Date.Point<int>.Subtract(Date.Point<int>)' has some invalid arguments

Error   2   Argument 1: cannot convert from 'Date.Point<double>' to 'Date.Point<int>'   

The code itself is not so important as I am simply trying to understand/learn generics, so I would just like to understand what is wrong here and why it wouldn’t work…. The actual method is not that important/will not be used.

  • 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-20T05:00:41+00:00Added an answer on May 20, 2026 at 5:00 am

    Your method signature:

    public double Subtract(Point<T> pt)
    

    Asks for a Point of generic type T, which matches the generic type in your class definition:

    class Point<T> where T : IConvertible
    

    That means it’s asking for the same type.

    You can specify a different generic type to allow it to mix different types:

    public double Subtract<U>(Point<U> pt) where U : IConvertible
    {
        return (X.ToDouble(nf) + pt.X.ToDouble(nf));
    }
    

    Note, however, that since the actual type of U can be inferred from the Point<U> argument you pass, you can call your method in the same way without needing to specify the type:

    Console.WriteLine(pt.Subtract(pt2)); // As opposed to pt.Subtract<double>(pt2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn about shift-reduce parsing. Suppose we have the following grammar, using
I'm trying to learn about generics, and I'm trying to make my own generic
Trying to learn about php's arrays today. I have a set of arrays like
I'm trying to learn about this feature of javascript I keep seeing in code,
I am currently trying to learn about OpenGL ES for the iPhone and following
I am trying to learn about Google maps API. I have a project that
I'm trying to learn about grails with Google App Engine and JPA by following
I have been trying to learn about classes in PHP and part of my
I have been trying to learn about resources and styles, I want to create
I have trying to learn about resources. In VS when I create the project:

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.