Make Sure Code Contracts Added In

Make Sure Code Contracts Added In

Page | 1

.NET 4 Demo Script

Setup

Visual Studio 2010 Setup

Make sure Code Contracts added in

Install software GPS – WARM IT UP!

Make sure console is set nicely

Zoomit

Environment demo

New console app

Application properties -> build -> target. talk about x86 vs. x64 vs. any cpu

X64

  1. Console.WriteLine("Is the OS x64? {0}", Environment.Is64BitOperatingSystem);
  2. Console.WriteLine("Is the process x64? {0}", Environment.Is64BitProcess);

Special Folders + Directory + Generics

  1. Console.WriteLine("My documents is in: {0}",
  2. Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
  3. varswOne = Stopwatch.StartNew();
  4. varswTwo = Stopwatch.StartNew();
  5. var path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
  6. var results = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);
  7. swOne.Stop();
  8. varhasFiles = results.Any();
  9. swTwo.Stop();
  10. Console.WriteLine(hasFiles);
  11. Console.WriteLine(swOne.ElapsedTicks);
  12. Console.WriteLine(swTwo.ElapsedTicks);

Flags

  1. class Program
  1. {
  2. static void Main(string[] args)
  3. {
  4. varmyCupboard = Demo.Food | Demo.Food;
  5. Console.WriteLine("Are skeletons in my cupboard? {0}", myCupboard.HasFlag(Demo.Skeletons));
  6. Console.WriteLine("Is their food in my cupboard? {0}", myCupboard.HasFlag(Demo.Food));
  7. }
  8. }
  9. [Flags]
  10. enum Demo : int
  11. {
  12. None = 0,
  13. Teapot = 1,
  14. Food = 2,
  15. Skeletons = 4,
  16. All = 7
  17. }

Code Contracts Demo

Start with ZUNE snippet

  1. public static int YearSince1980(intdaysPast, out intdayInYear)
  2. {
  3. //Contract.Requires(daysPast >= 0);
  4. //Contract.Ensures(Contract.ValueAtReturn(out dayInYear) > 0);
  5. //Contract.Ensures(Contract.ValueAtReturn(out dayInYear) < 25);
  6. var year = 1980;
  7. vardaysLeft = daysPast;
  8. while (daysLeft >= 365)
  9. {
  10. //vardaysStartLoop = daysLeft;
  11. if (DateTime.IsLeapYear(year))
  12. {
  13. if (daysLeft > 366)
  14. {
  15. year++;
  16. daysLeft -= 366;
  17. }
  18. //else
  19. //{
  20. // dayInYear = 366;
  21. // return year;
  22. //}
  23. }
  24. else
  25. {
  26. year++;
  27. daysLeft -= 365;
  28. }
  29. Contract.Assert(daysLeftdaysStartLoop);
  30. }
  31. dayInYear = daysLeft;
  32. return year;
  33. }
  34. }

Memory Cache

USING SYSTEM.RUNTIME.CACHING

  1. class Program
  1. {
  2. static void Main(string[] args)
  3. {
  4. var data = new Data();
  5. varsw = Stopwatch.StartNew();
  6. foreach (var item in data.GetData())
  7. {
  8. Console.WriteLine(item);
  9. }
  10. foreach (var item in data.GetData())
  11. {
  12. Console.WriteLine(item);
  13. }
  14. foreach (var item in data.GetData())
  15. {
  16. Console.WriteLine(item);
  17. }
  18. sw.Stop();
  19. Console.WriteLine(sw.ElapsedMilliseconds);
  20. }
  21. }
  22. class Data
  23. {
  24. string[] Names = new string[] { "Robert", "William", "Rudi" };
  25. ObjectCache cache = MemoryCache.Default;
  26. public IEnumerable<string> GetData()
  27. {
  28. if (cache.Any())
  29. {
  30. foreach (var item in cache)
  31. {
  32. yield return (string)item.Value;
  33. }
  34. }
  35. else
  36. {
  37. Thread.Sleep(1000);
  38. foreach (var item in Names)
  39. {
  40. Thread.Sleep(100);
  41. cache.Add(new CacheItem(item, item), new CacheItemPolicy());
  42. yield return item;
  43. }
  44. }
  45. }
  46. }

System.Device.Location

USING SYSTEM.DEVICES

  1. static GeoCoordinateWatchergps = new GeoCoordinateWatcher();
  1. static GeoCoordinate bbd = new GeoCoordinate(-26.179503, 28.0495713);
  2. static void Main(string[] args)
  3. {
  4. gps.PositionChanged += gps_PositionChanged;
  5. gps.StatusChanged += gps_StatusChanged;
  6. gps.Start();
  7. Console.ReadLine();
  8. gps.Stop();
  9. }
  10. static void gps_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
  11. {
  12. Console.WriteLine(e.Status);
  13. }
  14. static void gps_PositionChanged(object sender, GeoPositionChangedEventArgsGeoCoordinate> e)
  15. {
  16. Console.WriteLine(e.Position.Location);
  17. var resolver = new CivicAddressResolver();
  18. var location = resolver.ResolveAddress(e.Position.Location);
  19. Console.WriteLine(location.AddressLine1);
  20. Console.WriteLine(location.AddressLine2);
  21. Console.WriteLine(location.City);
  22. Console.WriteLine(location.Building);
  23. Console.WriteLine("Distance to work {0}km",
  24. e.Position.Location.GetDistanceTo(bbd)/1000);
  25. }