Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Lesson 9
Learn C#. Series of C# lessons
http://csharp.honcharuk.me/lesson-9
Agenda
• DateTime, DateTimeOffset and TimeSpan
• TimeZone and TimeZoneInfo
• Equality Comparison
• ICloneable
DateTime, DateTimeOffset and TimeSpan
• DateTime and DateTimeOffset are immutable structs for representing a date, and opt...
TimeZone and TimeZoneInfo
• The TimeZone and TimeZoneInfo classes provide information on time zone names UTC offsets,
and ...
Equality Comparison
• Value equality: two values are equivalent in some sense
• Referential equality: two references refer...
Equals
• null-safe equality comparison
• The double type’s == operator enforces that one NaN can never equal anything else...
Override Equals
class A
{
public int N { get; set; }
public bool Equals(A obj)
{
return obj.N == N;
}
public override bool...
ICloneable
class Book: ICloneable
{
public string Author { get; set; }
public string Title { get; set; }
public object Clo...
Thank you!
Questions?
Upcoming SlideShare
Loading in …5
×

Lesson9

114 views

Published on

Learn C#

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

Lesson9

  1. 1. Lesson 9 Learn C#. Series of C# lessons http://csharp.honcharuk.me/lesson-9
  2. 2. Agenda • DateTime, DateTimeOffset and TimeSpan • TimeZone and TimeZoneInfo • Equality Comparison • ICloneable
  3. 3. DateTime, DateTimeOffset and TimeSpan • DateTime and DateTimeOffset are immutable structs for representing a date, and optionally, a time. They have a resolution of 100 ns and a range covering the years 0001 through 9999 • DateTimeOffset was added in Framework 3.5 and is functionally similar to DateTime. Its distinguishing feature is that it also stores a UTC offset; this allows more meaningful results when comparing values across different time zones. • TimeSpan Represents a time interval.
  4. 4. TimeZone and TimeZoneInfo • The TimeZone and TimeZoneInfo classes provide information on time zone names UTC offsets, and daylight saving time rules. TimeZoneInfo is the more powerful of the two and was introduced in Framework 3.5 • The biggest difference between the two types is that TimeZone lets you access the current local time zone, whereas TimeZoneInfo provides access to all the world’s time zones. Further, TimeZoneInfo exposes a richer (although at times, more awkward) rules-based model for describing daylight saving time var bangkokTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time")); Console.WriteLine("Loca time in Bangkok is: {0}", bangkokTime);
  5. 5. Equality Comparison • Value equality: two values are equivalent in some sense • Referential equality: two references refer to exactly the same object int x = 5, y = 5; Console.WriteLine(x == y); A a1 = new A {N = 5}; A a2 = new A { N = 5 }; Console.WriteLine(a1 == a2); // False a1 = a2; Console.WriteLine(a1 == a2); // True
  6. 6. Equals • null-safe equality comparison • The double type’s == operator enforces that one NaN can never equal anything else — even another NaN. This is most natural from a mathematical perspective, and it reflects the underlying CPU behavior var sb1 = new StringBuilder("foo"); var sb2 = new StringBuilder("foo"); Console.WriteLine(sb1 == sb2); // False (referential equality) Console.WriteLine(sb1.Equals(sb2)); // True (value equality) object x = 3, y = 3; Console.WriteLine(object.Equals(x, y)); // True x = null; Console.WriteLine(object.Equals(x, y)); // False y = null; Console.WriteLine(object.Equals(x, y)); // True
  7. 7. Override Equals class A { public int N { get; set; } public bool Equals(A obj) { return obj.N == N; } public override bool Equals(object obj) { return ((A)obj).N == N; } } A a1 = new A { N = 5 }; A a2 = new A { N = 5 }; Console.WriteLine(a1.Equals((object)a2));
  8. 8. ICloneable class Book: ICloneable { public string Author { get; set; } public string Title { get; set; } public object Clone() { return new Book { Author = Author, Title = Title }; } } Book b1 = new Book { Author = "Tom", Title = "Adventures" }; Book b2 = (Book)b1.Clone();
  9. 9. Thank you! Questions?

×