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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:26:15+00:00 2026-06-08T16:26:15+00:00

I have an already existing generic class public class Foo<T> { private T _item;

  • 0

I have an already existing generic class

public class Foo<T>
{
    private T _item;
    public Foo(T item){ _item = item;}
}

I have to create a method which will return a certain property of T.
I see two solutions here.

  1. Creating an interface :

    public const string TestVar = "bar";
    public interface INamable { string Name { get; } }
    public class Bar : INamable
    {
        public string Name { get { return TestVar; } }
    }
    
    public class Foo<T> where T : INamable
    {
        private T _item;
        public Foo(T item) { _item = item; }
        public string GetName() { return this._item.Name; }
    }
    
    [TestMethod()]
    public void TestInterface()
    {
        var bar = new Bar();
        var foo = new Foo<Bar>(bar);
        Assert.AreEqual(TestVar, foo.GetName());
    }
    
  2. Passing a Func :

    public const string TestVar = "bar";
    public class Bar
    {
        public string Name { get { return TestVar; } }
    }
    
    public class Foo<T>
    {
        private T _item;
        private Func<T, string> _funcName;
        public Foo(T item, Func<T, string> funcName) { _item = item; _funcName = funcName; }
        public string GetName() { return _funcName.Invoke(_item); }
    }
    
    [TestMethod()]
    public void TestFunc()
    {
        var bar = new Bar();
        var foo = new Foo<Bar>(bar, b => b.Name);
        Assert.AreEqual(TestVar, foo.GetName());
    }
    

I go with the second solution, since I don’t have to create an interface, and I’m lazy. Just have to add one parameter to the already call of the foo constructor.

Moreover, the Foo class can still be used with all sort of class, and I prefer it this way.

BUT, I’m not sure if it is the good way to use a Func? Is it still nice and SOLID? This is my first try to use a Func, that’s why I’m asking myself!

A couple of months ago I used the first solution, with classic interface…

  • 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-08T16:26:17+00:00Added an answer on June 8, 2026 at 4:26 pm

    I’m re-phrasing my answer, because from the comments I realized I haven’t been very clear.

    It depends on the intent of your code and on who has the responsibility to give a name to your classes (the client should know and specify how to name the classes vs the classes should inherently have a Name). i’m changing @Scorpi0’s example a little, so that Foo actually does something with the name (it greets it) instead of just returning it.

    Example 1 (interfaces)

    interface INamable { string Name { get; } }
    
    class AThingWithAName : INamable 
    {
        public string Name {get {return "thing";}}
    }
    
    class AnotherThingWithAName : INamable 
    {
        public string Name {get {return "different thing";}}
    }
    
    class Foo<T> where T : INamable
    {
        public string Greet(T item) {return "hi " + item;}
    }
    

    Here Things have a Name regardless of Foo. Foo knows how to Greet Things as long as they have a Name (which is guaranteed by contract by them implementing the INamable interface).
    Here you want to guarantee that Foo only works on things with a Name and attempts to greet anything else should be a type error caught by the compiler.

    Also, note that you’re encapsulating the concrete implementation of Name in each class. Name could depend on its class’s private state.

    Example 2 (Func)

    class AThing {}
    class AnotherUnrelatedThing {}
    
    class Foo<T>
    {
        public string Greet(T item, Func<T, string> namingFunction) 
        {
            return "hi " + namingFunction(item);
        }
    }
    

    Here the emphasis is on Foo. Things don’t necessarily have a Name. I want to build a class Foo that can greet anything. How does it do that? Well, it’s the caller’s responsibility, for any Type, to pass in a function that can give a name to that type.
    In this case, not only we’re not requesting a guarantee that T knows its own Name, but we’re building Foo in a way that it can Greet any type, as long as WE know how to give it a name. For instance this works:

    var foo = new Foo<int>();
    var res=foo.Greet(2, n=>n.ToString());
    

    In exchange for that flexibility, we’re giving up encapsulation. Now Name is no longer something that is the class’s responsibility to implement, we (the caller) are telling Foo how to give a name to each class we use it with.

    So, depending on what you want to express and if you have a hierarchy of objects that have a name regardless of Foo, you may choose one or the other way.

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

Sidebar

Related Questions

I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have an already existing app with alot of database entries . class Foo(models.Model):
I have an already existing stored procedure. But when I am trying to create
I have an already existing order class with many properties and methods. I need
This is an already existing XML request format corresponding to which I have to
I am trying to create a method that will return all subsets of a
We have a table already existing in oracle 11g database which allows read as
I am trying to integrate zxing in my already existing iPhone app. I have
I'm having a bit of trouble loading pages into an already-existing colorbox. I have
I have a DB table that already has an existing value that I don't

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.