zoukankan      html  css  js  c++  java
  • 用好索引器。

    今天在CSDN上遇到一个问题:

    http://community.csdn.net/Expert/topic/4658/4658047.xml?temp=.6713526

    楼主想用树形的数据,本来是可以用XmlDocument的,可这个东西的效率实在是太差,我们推荐他用Hashtable嵌套,后来说写法太复杂。后来就想办法用递归调用来简化语法。

    搞了半天,忽然想起C++里面常常玩的链式表达式的把戏,用下面的代码实现了功能,而且用的时候写法堪称完美。

     1public class TreeNode
     2  
    {
     3
        Hashtable _dictionary;
     4    string _value = ""
    ;
     5

     6    public
     TreeNode()
     7    
    {
     8      _dictionary = new
     Hashtable();
     9    }

    10
    11    public TreeNode this[string
     key]
    12    
    {
    13      get

    14      {
    15        if ( key == null
     )
    16          throw new ArgumentNullException( "key"
     );
    17

    18
            EnsureChildNode( key );
    19

    20        return
     (TreeNode) _dictionary[key];
    21      }

    22      set
    23      {
    24        if ( key == null
     )
    25          throw new ArgumentNullException( "key"
     );
    26

    27
            EnsureChildNode( key );
    28

    29
            ( (TreeNode) _dictionary[key] ).SetValue( value._value );
    30      }

    31    }

    32
    33    private void EnsureChildNode( string
     key )
    34    
    {
    35      if ( !
    _dictionary.ContainsKey( key ) )
    36        _dictionary.Add( key, new
     TreeNode() );
    37    }

    38
    39    public void SetValue( string
     value )
    40    
    {
    41      if ( value == null
     )
    42        throw new ArgumentNullException( "value"
     );
    43

    44

    45      _value =
     value;
    46    }

    47
    48    public override string
     ToString()
    49    
    {
    50      return
     _value;
    51    }

    52
    53    public static implicit operator TreeNode( string
     value )
    54    
    {
    55      TreeNode node = new
     TreeNode();
    56
          node.SetValue( value );
    57      return
     node;
    58    }

    59  }

    60

  • 相关阅读:
    GitLab的基础使用-汉化配置
    GitLab的基础使用-数据备份与恢复
    Apache Hadoop集群扩容实战案例
    Hadoop 集群-完全分布式模式(Fully-Distributed Mode)
    HDFS参数调优总结
    网站压力测试 工具webbench
    2013年十大必知的大数据分析公司
    做电子商务网上开店应该读的书
    教你用大功率路由器覆盖3平方公里的WiFi广告
    中央推进城镇化建设 六行业分享25万亿蛋糕
  • 原文地址:https://www.cnblogs.com/Ivony/p/365848.html
Copyright © 2011-2022 走看看