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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:02:55+00:00 2026-06-04T16:02:55+00:00

I know the feature doesn’t exist in C#, but PHP recently added a feature

  • 0

I know the feature doesn’t exist in C#, but PHP recently added a feature called Traits which I thought was a bit silly at first until I started thinking about it.

Say I have a base class called Client. Client has a single property called Name.

Now I’m developing a re-usable application that will be used by many different customers. All customers agree that a client should have a name, hence it being in the base-class.

Now Customer A comes along and says he also need to track the client’s Weight. Customer B doesn’t need the Weight, but he wants to track Height. Customer C wants to track both Weight and Height.

With traits, we could make the both the Weight and the Height features traits:

class ClientA extends Client use TClientWeight
class ClientB extends Client use TClientHeight
class ClientC extends Client use TClientWeight, TClientHeight

Now I can meet all my customers’ needs without adding any extra fluff to the class. If my customer comes back later and says “Oh, I really like that feature, can I have it too?”, I just update the class definition to include the extra trait.

How would you accomplish this in C#?

Interfaces don’t work here because I want concrete definitions for the properties and any associated methods, and I don’t want to re-implement them for each version of the class.

(By “customer”, I mean a literal person who has employed me as a developer, whereas by “client” I’m referring a programming class; each of my customers has clients that they want to record information about)

  • 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-04T16:02:56+00:00Added an answer on June 4, 2026 at 4:02 pm

    You can get the syntax by using marker interfaces and extension methods.

    Prerequisite: the interfaces need to define the contract which is later used by the extension method. Basically the interface defines the contract for being able to “implement” a trait; ideally the class where you add the interface should already have all members of the interface present so that no additional implementation is required.

    public class Client {
      public double Weight { get; }
    
      public double Height { get; }
    }
    
    public interface TClientWeight {
      double Weight { get; }
    }
    
    public interface TClientHeight {
      double Height { get; }
    }
    
    public class ClientA: Client, TClientWeight { }
    
    public class ClientB: Client, TClientHeight { }
    
    public class ClientC: Client, TClientWeight, TClientHeight { }
    
    public static class TClientWeightMethods {
      public static bool IsHeavierThan(this TClientWeight client, double weight) {
        return client.Weight > weight;
      }
      // add more methods as you see fit
    }
    
    public static class TClientHeightMethods {
      public static bool IsTallerThan(this TClientHeight client, double height) {
        return client.Height > height;
      }
      // add more methods as you see fit
    }
    

    Use like this:

    var ca = new ClientA();
    ca.IsHeavierThan(10); // OK
    ca.IsTallerThan(10); // compiler error
    

    Edit: The question was raised how additional data could be stored. This can also be addressed by doing some extra coding:

    public interface IDynamicObject {
      bool TryGetAttribute(string key, out object value);
      void SetAttribute(string key, object value);
      // void RemoveAttribute(string key)
    }
    
    public class DynamicObject: IDynamicObject {
      private readonly Dictionary<string, object> data = new Dictionary<string, object>(StringComparer.Ordinal);
    
      bool IDynamicObject.TryGetAttribute(string key, out object value) {
        return data.TryGet(key, out value);
      }
    
      void IDynamicObject.SetAttribute(string key, object value) {
        data[key] = value;
      }
    }
    

    And then, the trait methods can add and retrieve data if the “trait interface” inherits from IDynamicObject:

    public class Client: DynamicObject { /* implementation see above */ }
    
    public interface TClientWeight, IDynamicObject {
      double Weight { get; }
    }
    
    public class ClientA: Client, TClientWeight { }
    
    public static class TClientWeightMethods {
      public static bool HasWeightChanged(this TClientWeight client) {
        object oldWeight;
        bool result = client.TryGetAttribute("oldWeight", out oldWeight) && client.Weight.Equals(oldWeight);
        client.SetAttribute("oldWeight", client.Weight);
        return result;
      }
      // add more methods as you see fit
    }
    

    Note: by implementing IDynamicMetaObjectProvider as well the object would even allow to expose the dynamic data through the DLR, making the access to the additional properties transparent when used with the dynamic keyword.

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

Sidebar

Related Questions

I know that this feature will be deprecated in C++0x, but for me as
I want to implement a feature in my app but I don't know the
Currently, I have this Rake::Task['FEATURE=features/Authentication.feature cucumber'].invoke but id doesn't work.. says Don't know how
I know it's possible to do that with PHP. But how to do that
I've just added a feature to my site which I followed from the FortySeven
I don't know if this feature that shows list of pages that user has
Can anyone let me know how to implement this feature specified at http://vimeo.com/24475654 Specially
I see some apps like 'Lock Calendar' that include this feature, and I know
Know this might be rather basic, but I been trying to figure out how
I wonder why .NET framework doesn't have pair (Observer/Observable) interfaces similar to Java's feature?

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.