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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:35:29+00:00 2026-05-14T16:35:29+00:00

How could I make this code more generic in the sense that the Dictionary

  • 0

How could I make this code more generic in the sense that the Dictionary key could be a different type, depending on what the user of the library wanted to implement? For example someone might what to use the extension methods/interfaces in a case where there “unique key” so to speak for Node is actually an “int” not a “string” for example.

public interface ITopology
{
    Dictionary<string, INode> Nodes { get; set; } 
}

public static class TopologyExtns
{
    public static void AddNode(this ITopology topIf, INode node)
    {
        topIf.Nodes.Add(node.Name, node);
    }
    public static INode FindNode(this ITopology topIf, string searchStr)
    {
        return topIf.Nodes[searchStr];
    }
}

public class TopologyImp : ITopology
{
    public Dictionary<string, INode> Nodes { get; set; }

    public TopologyImp()
    {
        Nodes = new Dictionary<string, INode>();
    }
}
  • 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-14T16:35:30+00:00Added an answer on May 14, 2026 at 4:35 pm

    Make the interface generic, then use a Func<INode,T> as a selector for the key. This assumes that you want the key for the dictionary to be extracted from the node. If this isn’t a hard requirement, then you could specify the key itself using the generic type specifier in the signature.

    public interface ITopology<T>
    { 
        Dictionary<T, INode> Nodes { get; set; }  
    } 
    
    public static class TopologyExtns 
    { 
        public static void AddNode<T>(this ITopology<T> topIf, INode node, Func<INode,T> keySelector ) 
        { 
            topIf.Nodes.Add( keySelector(node), node ); 
        } 
        public static INode FindNode<T>(this ITopology<T> topIf, T searchKey ) 
        { 
            return topIf.Nodes[searchKey]; 
        } 
    } 
    
    public class TopologyImp<T> : ITopology<T> 
    { 
        public Dictionary<T, INode> Nodes { get; set; } 
    
        public TopologyImp() 
        { 
            Nodes = new Dictionary<T, INode>(); 
        } 
    }
    

    You might also consider making INode a generic type. That would allow you to specify the Key as a property of the generic type which the implementation could defer to the appropriate “real” key. This would save you from having to supply either the key or a selector for the extension method.

    Alternative:

    public interface INode<T>
    {
         T Key { get; }
         string Name { get; set; }
         int ID { get; set; }
    }
    
    public class StringNode : INode<string>
    {
        public string Key { get { return this.Name; } }
        public string Name { get; set; }
        public int ID { get; set; }
    }
    
    public interface ITopology<T> 
    {  
        Dictionary<T, INode<T>> Nodes { get; set; }   
    }  
    
    public static class TopologyExtns  
    {  
        public static void AddNode<T>(this ITopology<T> topIf, INode<T> node )  
        {  
            topIf.Nodes.Add( node.Key, node );  
        }  
        public static INode<T> FindNode<T>(this ITopology<T> topIf, T searchKey )  
        {  
            return topIf.Nodes[searchKey];  
        }  
    }  
    
    public class TopologyImp<T> : ITopology<T>  
    {  
        public Dictionary<T, INode<T>> Nodes { get; set; }  
    
        public TopologyImp()  
        {  
            Nodes = new Dictionary<T, INode<T>>();  
        }  
    }
    

    Used as:

    var topology = new TopologyImp<string>();
    topology.AddNode( new StringNode { Name = "A", ID = 0 }  );
    var node = topology.FindNode( "A" );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Entity Framework designer is terrible - I've had the… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If by "hijack" you meant sniff the packets then what… May 14, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer If you want two actions to be atomic, embed them… May 14, 2026 at 5:11 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.