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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:46:35+00:00 2026-05-15T23:46:35+00:00

What I basically wish to do is design a generic interface that, when implemented,

  • 0

What I basically wish to do is design a generic interface that, when implemented, results in a class that can behave exactly like T, except that it has some additional functionality. Here is an example of what I’m talking about:

public interface ICoolInterface<T>
{
    T Value { get; set; }
    T DoSomethingCool();
}

public class CoolInt : ICoolInterface<int>
{
    private int _value;

    public CoolInt(int value)
    {
        _value = value;
    }

    public int Value
    {
        get { return _value; }
        set { _value = value; }
    }

    public int DoSomethingCool()
    {
        return _value * _value;
        // Ok, so that wasn't THAT cool
    }
}

And this is all well and good, but in order to use CoolInt, I need to do something like this:

CoolInt myCoolInt = new CoolInt(5);
int myInt = myCoolInt.Value;

I’d much rather, in terms of assignment at least, that CoolInt works just like int. In other words:

CoolInt myCoolInt = 5;
int myInt = myCoolInt;

To achieve this, I added these two conversion operators to my CoolInt class:

    public static implicit operator CoolInt(int val)
    {
        return new CoolInt(val);
    }

    public static implicit operator int(CoolInt obj)
    {
        return obj.Value;
    }

Works awesomely. Now, I would prefer it if I could add these two overloads to the interface, so that implementers of the interface are forced to implement these operators. The problem is, the prototypes of these operators refer directly to CoolInt.

C# has a lot of “placeholder” names for things that are implicitly defined or have yet to be defined. The T that is conventionally used in generic programming is one example. I suppose the value keyword, used in Properties, is another. The “this” reference could be considered another. I am hoping that there’s another symbol I can use in my interface to denote “the type of the class that is implementing this interface”, e.g. “implementer”.

    public static implicit operator implementer(int val)
    {
        return new IntVal(val);
    }

    public static implicit operator int(implementer obj)
    {
        return obj.Value;
    }

Is this possible?

  • 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-15T23:46:36+00:00Added an answer on May 15, 2026 at 11:46 pm

    Sadly no 🙁

    C# doesn’t do well when it comes to operator overloading (This is one example, another is generic constraints on certain operator types).

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could encrypt sections of your configuration file that contains… May 15, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer This is significantly easier to solve with ranking functions but… May 15, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer To answer your headline: You cannot pass values to a… May 15, 2026 at 11:47 pm

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.