zoukankan      html  css  js  c++  java
  • C#一些实用的,容易被遗忘的特性,经验和技巧【转】

    突然想到,想要通过名字来获得枚举的值,看了不少的资料,发现上面都只是解释枚举的语法而已,对于它的实际应用,一点也不提及,难道只能用switch来判断?不好吧,这样也未免太土了点。后来发现了枚举还有这样的用法,真的很使用,看代码:

    namespace EnumTest
    {
    enum date { sun, mon, tue, wes, thu, fri, sat }

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine(
    "输入星期的名称:");
    string name = Console.ReadLine();

    //Type参数表示要转换成的枚举的类型,true指示忽略大小写
    object obj = Enum.Parse(typeof(date), name, true);
    Console.WriteLine(
    "输出星期的数字:");
    Console.WriteLine(obj
    + "" + (int)obj);
    }
    }
    }

    再来看一下运行结果图:

      怎么样,是不是觉得很实用啊。

      接下来要介绍的是委托(delegate),有对delegate不太了解的人可以看一下我以前写的一个随笔什么是委托(delegate) 。我们平时使用委托都是单个的指定委托的方法,但是如果我们需要通过参数动态的指定呢?难道也是用switch?这样做太麻烦了,还有更好的方法,看代码:


    namespace DelegateTest
    {
    class Person
    {
    public void FirstMethod()
    {
    Console.WriteLine(
    "这是第一个方法!");
    }
    public void SecondMethod()
    {
    Console.WriteLine(
    "这是第二个方法!");
    }
    }

    delegate void dele();

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine(
    "输入方法的名字:");
    string name = Console.ReadLine();
    Person p
    = new Person();

    //Type参数是要转换的委托的类型,p是要调用的委托的实例,true指示忽略大小写
    dele d = Delegate.CreateDelegate(typeof(dele), p, name + "Method", true) as dele;
    d.Invoke();
    Console.WriteLine(
    "输入方法的名字:");
    name
    = Console.ReadLine();
    d
    = Delegate.CreateDelegate(typeof(dele), p, name + "method", true) as dele;
    d.Invoke();
    }
    }
    }

    下面是运行的结果图:

      接下来是一个C#经常被忽略的特性,匿名方法。虽然匿名方法很少用到,但是知道总比不知道的好。以delegate为例,我们平时都是赋予delegate具体的方法,但是有时候有一个方法要委托,但是它要执行的功能实在是很简单,而且可能也就只用一次而已,但是要给它写一个具体的方法实在是麻烦。而C#2.0为我们提供了匿名方法的特性,代码如下:


    namespace Anonymity
    {
    delegate void dele();
    class Program
    {
    static void Main(string[] args)
    {
    dele d
    = delegate() { Console.WriteLine("这是一个匿名方法!"); };
    d.Invoke();
    }
    }
    }

    运行结果如下图:

      接下来是索引器(Indexer)了,写技术文章不像写小说,真的挺累的。要构思,要描述,要写代码,要调试,更重要的是,要敲键盘。好了,抱怨到些为止。索引器(Indexer)能让我们像访问数组一样访问对象,至于它有什么作用呢?只有用到了才知道,看代码:


    namespace IndexerTest
    {
    class PersonNames
    {
    List
    <string> names = new List<string>();
    /// <summary>
    /// 为对象创建索引器,注意它和属性的get/set的不同
    /// </summary>
    /// <param name="index">索引值</param>
    /// <returns></returns>
    public string this[int index]
    {
    get { return names[index]; }
    set { names.Add(value); }
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    PersonNames names
    = new PersonNames();
    names[
    0] = "微软";
    names[
    1] = "谷歌";
    names[
    2] = "百度";
    for (int i = 0; i < 3; i++)
    {
    Console.WriteLine(names[i]);
    }
    }
    }
    }

      遗憾的是,它不支持foreach送代。要实现送代的功能,可以实现IEnumerable接口。

      接下来是。。。?还有接下来?是不是看得很累了,其实我也写得很累了。接下来是哈希表(HashTable),HashTable大家都知道很好用,大家可能会发现,它好像不能使用foreach来送代。其实它是可以送代的,只是稍微有点不同而已,看代码:


    namespace HashTableTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    Hashtable table
    = new Hashtable();
    table[
    "name1"] = "微软";
    table.Add(
    "name2", "谷歌");
    table[
    "name3"] = "百度";
    //注意name的类型名
    foreach (DictionaryEntry name in table)
    {
    Console.WriteLine(name.Key
    + "" + name.Value);
    }
    }
    }
    }

    运行结果如下图:

      大家仔细看一下,它送代返回的类型是DictionaryEntry而不是我们预料中的string。为什么是这样的呢?HashTable不同于其它的Collection,它保存的不仅是值(value),同时还保存着键(key),它们在HashTable中是以DictionaryEntry类型保存着的。再回顾一下上面的索引器(Indexer),我们就能推断出,HashTable可能是一个实现了索引器功能的类(不是别骂我,我也是猜的)。

      好了,终于写完了,希望对大家有所帮助。

    转自:http://www.cnblogs.com/reallypride/archive/2008/09/08/1287095.html

  • 相关阅读:
    (转)python request用法
    QLabel 文本内容自动换行显示
    事件的传递 键盘事件作为例子
    qt中添加Q_OBJECT报错的问题
    Q_OBJECT宏的作用
    C++中的RAII介绍 资源管理
    C++ explicit关键字详解
    信号和槽 带不带参数
    enum 枚举类型默认值
    创建工具条ToolBar
  • 原文地址:https://www.cnblogs.com/flyingskya/p/1498877.html
Copyright © 2011-2022 走看看