C#范型默认类型

语法糖,这种方法使用起来和默认范型参数用法是一样的 类似于C++模板特化

HashTable 默认类型就是string 

/*
* Author:  caoshanshan
* Email:   me@dreamyouxi.com

 */
using UnityEngine;
using System.Collections;

public sealed class HashTable
{

    public static HashTable Create()
    {
        HashTable ret = new HashTable();
        return ret;
    }

    public static HashTable CreateWithJson(string json)
    {
        return Json.Decode(json);
    }
    private HashTable() { }
    private Hashtable kv = new Hashtable();

    public int GetInt(string key)
    {
        return int.Parse(this.Get(key));
    }

    public float GetFloat(string key)
    {
        return float.Parse(this.Get(key));
    }

    public void Set(string key, string value)
    {
        if (kv.Contains(key))
        {
            kv[key] = value; return;
        }
        kv.Add(key, value);
    }
    public string this[string key]
    {
        get
        {
            return this.Get(key);
        }

        set
        {
            this.Set(key, value);
        }
    }
    public string Get(string key)
    {
        if (kv.Contains(key))
        {
            return kv[key] as string;
        }
        return "";
    }
    public int Count
    {
        get
        {
            return kv.Count;
        }
    }

    public Hashtable GetHashtable()
    {
        return kv;
    }

    public string ToJson()
    {
        return Json.Encode(this);
    }
}

public sealed class HashTable<T>/// where T : class,new()
{

    public static HashTable<T> Create<T>()
    {
        HashTable<T> ret = new HashTable<T>();
        return ret;
    }

    public static HashTable<T> CreateWithJson<T>(string json)
    {
        return null;// Json.Decode(json);
    }

    private Hashtable kv = new Hashtable();

    public T Get<T>(string key)
    {
        return (T)(this.Get(key)) ;
    }

    public void Set(string key, object value)
    {
        if (kv.Contains(key))
        {
            kv[key] = value; return;
        }
        kv.Add(key, value);
    }
    public object this[string key]
    {
        get
        {
            return this.Get(key);
        }

        set
        {
            this.Set(key, value);
        }
    }
    public object Get(string key)
    {
        if (kv.Contains(key))
        {
            return kv[key];
        }
        return null;
    }
    public int Count
    {
        get
        {
            return kv.Count;
        }
    }

    public Hashtable GetHashtable()
    {
        return kv;
    }

    public string ToJson()
    {
        return "";// Json.Encode(this as hASH);
    }
}

 

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/865532

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值