Tuesday, October 11, 2011

What is a size of DateTime type in C#?

What is a size of DateTime type in C#? - A trivial question, unexcitingly facing few obstacles. Self-explanatory code below describes how you can't get it and how you can (and yes, it's 8 bytes):
using System;

namespace DateTimeSizeExample
{
  public struct TypeSizeProxy<T>
  {
    public T PublicField;
  }

  public static class SizeCalculator
  {
    public static int SizeOf<T>()
    {
    try
    {
      return System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));
    }
    catch (ArgumentException)
    {
      return System.Runtime.InteropServices.Marshal.SizeOf(new TypeSizeProxy<T>());
    }
  }

  public static int GetSize(this object obj)
  {
    return System.Runtime.InteropServices.Marshal.SizeOf(obj);
  }
}

internal class Program
{
private static void Main(string[] args)
{
// Error: 'System.DateTime' does not have a predefined size, therefore sizeof can only be used in an unsafe context
// (consider using System.Runtime.InteropServices.Marshal.SizeOf)
//int s1 = sizeof(DateTime);

// Run time Argument Exception: Type 'System.DateTime' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed
//int s2 = System.Runtime.InteropServices.Marshal.SizeOf(typeof(DateTime));

int dateTimeSize = SizeCalculator.SizeOf<DateTime>(); // 8 bytes
}
}
}

Saturday, January 29, 2011

Dynamic casting in c#

var typesSubset = new[] { typeof(decimal), typeof(Int32), typeof(Int64) };

Type someDynamicType = typeof (int);

int someDynamicValue = 777;

if (typesSubset.Any(t => t == someDynamicType))

{

    object result = Expression.Lambda<Func<object>>(

        Expression.Convert(Expression.Add(

                               Expression.Constant(someDynamicType), Expression.Constant(someDynamicValue)),

                           typeof (object))).Compile()();

 

    Console.WriteLine(result); // 777

}

Pass xml to stored procedure

In C#:

    1 // create XML string

    2 var dataToPass = new StringBuilder("<RowsOfNumbers>");

    3 dataToPass.AppendFormat("<id>{0}</id>", 1);

    4 dataToPass.AppendFormat("<id>{0}</id>", 2);

    5 dataToPass.AppendFormat("<id>{0}</id>", 3);

    6 dataToPass.Append("</RowsOfNumbers>");

    7 

    8 // call SP

    9 myDB.ExecuteStoredProcedure("GetXmlData", new object[] { dataToPass.ToString() });

IN SQL:

CREATE PROCEDURE [dbo].[GetXmlData]    (@dataIdsXml XML)

AS

    DECLARE @dataIdsTable TABLE (ID int)

 

    INSERT INTO @dataIdsTable (ID) SELECT ParamValues.ID.value('.','VARCHAR(20)')

    FROM @dataIdsXml.nodes('/RowsOfNumbers/id') as ParamValues(ID)

 

    SELECT [ID] FROM @dataIdsTable WHERE [ID]=1

GO   

Friday, January 28, 2011

Workforce turnover around me (LinkedIn stats)

Following to LinkedIn data 57 out of my 382 contacts changed jobs in 2010.
This is 15%. Probably actual rate is a bit higher since in some cases this info may be not updated in LinkedIn.

Subset of TED lectures on health

(with bits of practical information)

http://www.ted.com/talks/gregory_petsko_on_the_coming_neurological_epidemic.html – drink coffee, no strikes/box, no avian flue, low pressure, 3 grams of omega-three a day
http://www.ted.com/talks/carl_honore_praises_slowness.html
http://www.ted.com/talks/dan_buettner_how_to_live_to_be_100.html
http://www.ted.com/talks/martin_rees_asks_is_this_our_final_century.html
http://www.ted.com/talks/dean_ornish_says_your_genes_are_not_your_fate.html
http://www.ted.com/talks/juan_enriquez_shares_mindboggling_new_science.html - current crisis and future bionic technology
http://www.ted.com/talks/julian_treasure_the_4_ways_sound_affects_us.html – well once I used nice utility (still downloadable) – Aire Freshner
http://www.ted.com/talks/mark_bittman_on_what_s_wrong_with_what_we_eat.html

Just interesting:
http://www.ted.com/talks/misha_glenny_investigates_global_crime_networks.html

Fallen giant

Few photo subsets from this blog:

“Heroic deeds live in eternity” – are they?