in

SDT Community Server

SDT Forums, Blogs, Photos server.

wego

April 2007 - Posts

  • Hashtable 元素顺序问题

     

    今天用 Hashtable 时发现结果的顺序并非为元素 Add 的顺序. 后改用ListDictionary 得以解决.

    请见如下 code

     

     

    using System.Collections;

                static Hashtable _period;

        

                static frmMain()

                {

                  _period = new Hashtable();

                  _period.Add("Month", 12);

                  _period.Add("Quarter", 4);

                  _period.Add("Half Year", 2);

                  _period.Add("Full Year", 1);

     

                  ICollection period_type = _period.Keys;

                  foreach (string s in period_type)

                  {

                       Console.WriteLine(s);

                  }

                }

     

        output

        --------

        Month

        Half Year

        Full Year

        Quarter

     

    using System.Collections.Specialized;

    static ListDictionary _period;

        

                static frmMain()

                {

                  _period = new ListDictionary();

                  _period.Add("Month", 12);

                  _period.Add("Quarter", 4);

                  _period.Add("Half Year", 2);

                  _period.Add("Full Year", 1);

     

                  ICollection period_type = _period.Keys;

                  foreach (string s in period_type)

                  {

                       Console.WriteLine(s);

                  }

                }

     

        output

        --------

        Month

        Quarter

        Half Year

        Full Year

     

  • C# 日期全球化

          今天用 CultureInfo 结合 ToString("MMM")  硬是不能取得英文月份表示 (如: Mar) , 而是中文 '三月'    (本人OS为中文版本).             

                CultureInfo info = new CultureInfo("en-US", false);
                Calendar calendar = info.Calendar;
                DateTime t = new DateTime(year, month, day, calendar);
                return t.ToString("MMM") + " " + t.Year.ToString();    // return '三月 2007'

         兜了几个圈子, 后改用 DateTimeFormatInfo class 才得以解决. 

                DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;
                DateTime t = new DateTime(year, month, day);
                return myDTFI.GetAbbreviatedMonthName(t.Month) + " " + t.Year.ToString();    // return 'Mar 2007'

         令外,  DateTimeFormatInfo class 还有好多实用转换方法.  如:  

                 myDTFI.GetDayName(t.DayOfWeek) --> Thursday

     

Copyright SDT, 2006-2009. All rights reserved.