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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:42:58+00:00 2026-06-08T18:42:58+00:00

I am trying to design a class that will be flexible enough to chart

  • 0

I am trying to design a class that will be flexible enough to chart data about different kinds of data. I’m new to OOP in C#, so I’m fumbling around trying to achieve this using some combination of generics, delegates and classes.

Here’s the class that I’ve written so far:

using System;
using System.Collections.Generic;

namespace Charting
{
    public class DataChart<T>
    {
        public Func<T, object> RowLabel { get; set; }
    }
}

And here’s how I’m trying to call it:

var model = new DataChart<MyClass>()
{
    RowLabel = delegate(MyClass row)
    {
        return String.Format("{0}-hello-{1}", row.SomeColumn, row.AnotherColumn);
    }
};

The problem with this approach is that I’d have to explicitly cast the object emitted by RowLabel. I was hoping I could somehow make the output type a generic and add constraints to it, like so:

    public class DataChart<T>
    {
        // The output of the RowLabel method can only be a value type (e.g. int, decimal, float) or string.
        public Func<T, U> RowLabel where U : struct, string { get; set; }
    }

Is this possible? And if so, how do I do it? Thanks in advance!

  • 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-08T18:43:00+00:00Added an answer on June 8, 2026 at 6:43 pm

    You can do some of this.

    First, genericizing the output: just add another type parameter to the class.

    public class DataChart<T, U>
    {
      public Func<T, U> RowLabel  { get; set; }
    }
    

    But those type constraints you mentioned do not make sense. Type constraints are “and”-ed, not “or”-ed. A string is not a struct, so you can’t confine it to that specific combination of types. It will still work if you leave it unconstrained, although you lose some compile-time safety.

    Edit: Also, it turns out you can’t specify string as a type parameter, anyway. It’s a sealed class! Having a generic that only accepts types of a sealed class would be pointless, and the compiler prevents it.

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

Sidebar

Related Questions

I'm trying to design a class hierarchy with a bunch of similar classes that
I am trying to design a subclass that will contain responses to multipart questions
I'm trying to design a PHP object (call it Incident_Collection ) that will hold
I'm trying to design a data-structure around a stack that blocks until the stack
I'm trying to design application that will have UI with database in the backend.
I'm trying to design a class that needs to dynamically allocate some memory.. I
Trying to design simple aspect,that will print word logg to console,when any of public
I am trying to design my Class Diagram. But I get stuck on this
Now I am trying to design a class for a deck of cards. I
I'm trying to design a policy-based class, where a certain interface is implemented by

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.