.Net display object properties

Display properties of an object:

Thanks to Jay Miller on CodeGuru

using System;
using System.Text;

namespace ObjectPropertyDisplayer
{
    static class ObjectDisplayer
    {
        static void Main()
        {
            Console.WriteLine(DisplayObjectInfo(new DateTime()));
            Console.ReadKey();
        }

        private static string DisplayObjectInfo(Object o)
        {
            var sb = new StringBuilder();
            try
            {
                var type = o.GetType();

                sb.Append("Type: " + type.Name);
                sb.Append("\r\n\r\nFields:");

                var fields = type.GetFields();
                if (fields.Length > 0)
                {
                    foreach (var fieldInfo in fields)
                    {
                        sb.Append("\r\n " + fieldInfo + " = " + fieldInfo.GetValue(o));
                    }
                }
                else
                {
                    sb.Append("\r\n None");
                }

                sb.Append("\r\n\r\nProperties:");

                var properties = type.GetProperties();
                if (properties.Length > 0)
                {
                    foreach (var propertyInfo in properties)
                    {
                        sb.Append("\r\n " + propertyInfo + " = " +
                                  propertyInfo.GetValue(o, null));
                    }
                }
                else
                {
                    sb.Append("\r\n None");
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return sb.ToString();
        }
    }
}

Displays:


Type: DateTime

Fields:
System.DateTime MinValue = 01/01/0001 00:00:00
System.DateTime MaxValue = 31/12/9999 23:59:59

Properties:
System.DateTime Date = 01/01/0001 00:00:00
Int32 Day = 1
System.DayOfWeek DayOfWeek = Monday
Int32 DayOfYear = 1
Int32 Hour = 0
System.DateTimeKind Kind = Unspecified
Int32 Millisecond = 0
Int32 Minute = 0
Int32 Month = 1
System.DateTime Now = 25/05/2015 15:02:04
System.DateTime UtcNow = 25/05/2015 14:02:04
Int32 Second = 0
Int64 Ticks = 0
System.TimeSpan TimeOfDay = 00:00:00
System.DateTime Today = 25/05/2015 00:00:00
Int32 Year = 1

.Net Sieve of Eratosthenes prime finder

An ancient algorithm for finding primes: Sieve of Eratosthenes.

using System;
using System.Collections.Generic;
using System.Linq;

namespace SoEPrimes
{
    static class SoEPrimeFinder
    {
        static void Main()
        {
            var primes = new PrimeFinder(1024).Primes;

            Action<long> print = PrintResults;
            
            primes.ToList().ForEach(print);
            
            Console.ReadKey();
        }

        private static void PrintResults(long result)
        {
            Console.WriteLine(result);
        }
    }

    public class PrimeFinder
    {
        readonly List<long> _primes = new List<long>();

        public PrimeFinder(long seed)
        {
            CalculatePrimes(seed);
        }

        public IEnumerable<long> Primes { get { return _primes; } }

        private void CalculatePrimes(long maxValue)
        {
            for (var checkValue = 3; checkValue <= maxValue; checkValue += 2)
            {
                if (IsPrime(checkValue))
                {
                    _primes.Add(checkValue);
                }
            }
        }

        private bool IsPrime(long checkValue)
        {
            return _primes.All(prime => (checkValue % prime) != 0 || !(prime <= Math.Sqrt(checkValue)));
        }
    }
}