C# 3。0 基本框架

简介: 使用TimeZoneInfo: static void Main(){ TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById ("W.

使用TimeZoneInfo:

static void Main()
{
  TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById
                    ("W. Australia Standard Time");

  Console.WriteLine (wa.Id);                   // W. Australia Standard Time
  Console.WriteLine (wa.DisplayName);          // (GMT+08:00) Perth
  Console.WriteLine (wa.BaseUtcOffset);        // 08:00:00
  Console.WriteLine (wa.SupportsDaylightSavingTime);     // True

  foreach (TimeZoneInfo.AdjustmentRule rule in wa.GetAdjustmentRules())
  {
    Console.WriteLine ("Rule: applies from " + rule.DateStart +
                                      " to " + rule.DateEnd);
  
    Console.WriteLine ("   Delta: " + rule.DaylightDelta);  

    Console.WriteLine ("   Start: " + FormatTransitionTime
                                     (rule.DaylightTransitionStart, false));

    Console.WriteLine ("   End:   " + FormatTransitionTime
                                     (rule.DaylightTransitionEnd, true));
    Console.WriteLine();
  }
}

static string FormatTransitionTime (TimeZoneInfo.TransitionTime tt,
                                    bool endTime)
{
  if (endTime && tt.IsFixedDateRule 
              && tt.Day == 1 && tt.Month == 1 
              && tt.TimeOfDay == DateTime.MinValue)
    return "-";

  string s;
  if (tt.IsFixedDateRule)
    s = tt.Day.ToString();
  else
    s = "The " +
        "first second third fourth last".Split() [tt.Week - 1] +
        " " + tt.DayOfWeek + " in";

  return s + " " + DateTimeFormatInfo.CurrentInfo.MonthNames [tt.Month-1]
           + " at " + tt.TimeOfDay.TimeOfDay;
}

DateTime和daylight保存:

DaylightTime changes = TimeZone.CurrentTimeZone.GetDaylightChanges (2008);
TimeSpan halfDelta = new TimeSpan (changes.Delta.Ticks / 2);

DateTime utc1 = changes.End.ToUniversalTime() - halfDelta;
DateTime utc2 = utc1 - changes.Delta;

// Converting these variables to local times demonstrates why you should use
// UTC and not local time if your code relies on time moving forward:

DateTime loc1 = utc1.ToLocalTime();  // (Pacific Standard Time)
DateTime loc2 = utc2.ToLocalTime(); 
Console.WriteLine (loc1);            // 2/11/2008 1:30:00 AM
Console.WriteLine (loc2);            // 2/11/2008 1:30:00 AM
Console.WriteLine (loc1 == loc2);    // True

Console.Write (loc1.ToString ("o"));  // 2008-11-02T02:30:00.0000000-08:00
Console.Write (loc2.ToString ("o"));  // 2008-11-02T02:30:00.0000000-07:00

Console.WriteLine (loc1.ToUniversalTime() == utc1);   // True
Console.WriteLine (loc2.ToUniversalTime() == utc2);   // True

写一个自定义格式provider:

public class WordyFormatProvider : IFormatProvider, ICustomFormatter
{
  static readonly string[] _numberWords =
   "zero one two three four five six seven eight nine minus point".Split();

  IFormatProvider _parent;   // Allows consumers to chain format providers

  public WordyFormatProvider() : this (CultureInfo.CurrentCulture) { }
  public WordyFormatProvider (IFormatProvider parent)
  {
    _parent = parent;
  }

  public object GetFormat (Type formatType)
  {
    if (formatType == typeof (ICustomFormatter)) return this;
    return null;
  }

  public string Format (string format, object arg, IFormatProvider prov)
  {
    // If it's not our format string, defer to the parent provider:
    if (arg == null || format != "W")
      return string.Format (_parent, "{0:" + format + "}", arg);

    StringBuilder result = new StringBuilder();
    string digitList = string.Format (CultureInfo.InvariantCulture,
                                      "{0}", arg);
    foreach (char digit in digitList)
    {
      int i = "0123456789-.".IndexOf (digit);
      if (i == -1) continue;
      if (result.Length > 0) result.Append (' ');
      result.Append (_numberWords[i]);
    }
    return result.ToString();
  }
}

重载相等语义:

public struct Area : IEquatable <Area>
{
  public readonly int Measure1;
  public readonly int Measure2;

  public Area (int m1, int m2)
  {
    Measure1 = m1;
    Measure2 = m2;
  }

  public override bool Equals (object other)
  {
    if (!(other is Area)) return false;
    return Equals ((Area) other);        // Calls method below
  }

  public bool Equals (Area other)        // Implements IEquatable<Area>
  {
    return Measure1 == other.Measure1 && Measure2 == other.Measure2
        || Measure1 == other.Measure2 && Measure2 == other.Measure1;
  }

  public override int GetHashCode()
  {
    if (Measure1 > Measure2)
      return Measure1 * 37 + Measure2;    // 37 = a prime number
    else
      return Measure2 * 37 + Measure1;
  }

  public static bool operator == (Area a1, Area a2)
  {
     return a1.Equals (a2);
  }

  public static bool operator != (Area a1, Area a2)
  {
    return !a1.Equals (a2);
  }
}

实现IComparable接口:

public struct Note : IComparable<Note>, IEquatable<Note>, IComparable
{
  int _semitonesFromA;

  public Note (int semitonesFromA)
  {
    _semitonesFromA = semitonesFromA;
  }

  public int CompareTo (Note other)            // Generic IComparable<T>
  {
    if (Equals (other)) return 0;              // Fail-safe check
    return _semitonesFromA.CompareTo (other._semitonesFromA);
  }

  int IComparable.CompareTo (object other)     // Nongeneric IComparable
  {
    if (!(other is Note))
      throw new InvalidOperationException ("CompareTo: Not a note");
    return CompareTo ((Note) other);
  }

  public static bool operator < (Note n1, Note n2)
  {
     return n1.CompareTo (n2) < 0;
  }

  public static bool operator > (Note n1, Note n2)
  {
    return n1.CompareTo (n2) > 0;
  }

  public bool Equals (Note other)    // for IEquatable<Note>
  {
    return _semitonesFromA == other._semitonesFromA;
  }

  public override bool Equals (object other)
  {
    if (!(other is Note)) return false;
    return Equals ((Note) other);
  }

  public override int GetHashCode()
  {
    return _semitonesFromA.GetHashCode();
  }

  public static bool operator == (Note n1, Note n2)
  {
     return n1.Equals (n2);
  }

  public static bool operator != (Note n1, Note n2)
  {
     return !(n1 == n2);
  }
}
目录
打赏
0
0
0
0
20
分享
相关文章
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
73 12
基于C#开源、功能强大、灵活的跨平台开发框架 - Uno Platform
基于C#开源、功能强大、灵活的跨平台开发框架 - Uno Platform
129 3
精选2款C#/.NET开源且功能强大的网络通信框架
精选2款C#/.NET开源且功能强大的网络通信框架
130 0
C#/.NET/.NET Core优秀项目和框架2024年10月简报
C#/.NET/.NET Core优秀项目和框架2024年10月简报
143 3
C# 单元测试框架 NUnit 一分钟浅谈
【10月更文挑战第17天】单元测试是软件开发中重要的质量保证手段,NUnit 是一个广泛使用的 .NET 单元测试框架。本文从基础到进阶介绍了 NUnit 的使用方法,包括安装、基本用法、参数化测试、异步测试等,并探讨了常见问题和易错点,旨在帮助开发者有效利用单元测试提高代码质量和开发效率。
222 64
C#/.NET/.NET Core优秀项目和框架2024年9月简报
C#/.NET/.NET Core优秀项目和框架2024年9月简报
Uno Platform 是一个用于构建跨平台应用程序的强大框架,它允许开发者使用 C# 和 XAML 来创建适用于多个平台的应用
Uno Platform 是一个用于构建跨平台应用程序的强大框架,它允许开发者使用 C# 和 XAML 来创建适用于多个平台的应用
463 8
C#/.NET/.NET Core优秀项目框架推荐榜单
C#/.NET/.NET Core优秀项目框架推荐榜单
394 0
Xamarin 与 .NET:解锁现代化移动应用开发的超级武器——深入探讨C#与.NET框架如何赋能跨平台应用,实现高效编码与卓越性能
【8月更文挑战第31天】Xamarin 与 .NET 的结合为开发者提供了强大的平台,用于构建现代化移动应用。通过 C# 和 .NET 框架,Xamarin 可以实现一次编写、多平台运行,覆盖 iOS、Android 和 Windows。这种方式不仅节省了开发时间和成本,还保证了应用的一致性和高质量。Xamarin 是一个开源框架,专为跨平台移动应用开发设计,允许使用 C# 语言和 .NET 核心库构建原生应用,并访问各平台特定功能。微软维护的 Xamarin 是 Visual Studio 生态系统的一部分,极大地提高了开发效率。
115 0