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

  • Home
  • SEARCH
  • 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 6234803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:33:18+00:00 2026-05-24T10:33:18+00:00

I want a generic function that would work with types that have Top ,

  • 0

I want a generic function that would work with types that have Top, Bottom, Right and Rect read-only properties – I have a lot of such classes in a third-party library.

I wrote this:

internal class MyTemplate<WhatType> {
    internal static void Work( WhatType what )
    {
        int left = what.Left;
    }
};

and I expect it to work – equivalent code in C++ would work okay. However C# objects:

error CS1061: ‘WhatType’ does not contain a definition for ‘Left’ and no extension method ‘Left’ accepting a first argument of type ‘WhatType’ could be found (are you missing a using directive or an assembly reference?)

I don’t get it – why would it try to instantiate the template before I even call it? Of course, type WhatType is not yet known and so no properties can be found.

What am I doing wrong and how do I fix that?

  • 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-24T10:33:18+00:00Added an answer on May 24, 2026 at 10:33 am

    C# generics look similar to C++ templates, but they’re actually quite different. C# generics are strongly typed, so you can’t call a member that is not statically known.

    In order to be able to call Left on an object of type WhatType, you have to specify that WhatType implements an interface or inherits from a class that defines Left, using a generic constraint. For instance:

    interface IHasPosition
    {
        int Left { get; }
        int Top { get; }
    }
    
    internal class MyTemplate<WhatType> where WhatType : IHasPosition
    {
        internal static void Work( WhatType what )
        {
            int left = what.Left;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a generic function for a few types such as long,
I want to make a generic class that accepts only serializable classes, can it
I want to have a generic event that I can fire that will take
I want to have a generic object that implements an interface. I mean if
I want to have a function defined in a superclass that returns an instance
I have a generic function that returns URLs. (It's a plugin function that returns
Say I have a recursive function that I want to know how many times
I want to write a generic function to calculate factorial in C# ... like:
I am passing a generic list to a function , i want to enumerate
I want to implement a generic method on a generic class which would allow

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.