Search This Blog

Showing posts with label .NET Framework. Show all posts
Showing posts with label .NET Framework. Show all posts

Monday, October 28, 2013

.NET Framework

The .NET Framework is a development framework build by Microsoft for building apps for Windows, Windows Phone, Windows Server, and Windows Azure.

Components
  • common language runtime (CLR)
  • .NET Framework class library
The .NET Framework is a managed execution environment.





NET Framework Garbage Collector

.NET Framework > Garbage Collector

The .NET Framework garbage collector manages the allocation and release of memory for applications.
When you create a new object the CLR allocates memory for the object in a managed heap. 
The runtime continues to allocate space for new objects.
Because the memory is not infinite the garbage collector performs a collection in order to free some memory. 

Advantages:
  • don't have to free memory
  • effectively allocate objects  on the managed heap
  • clears memory of the objects no longer being used
  •  memory safety

Tuesday, March 5, 2013

System.Runtime.InteropServices Namespace

System.Runtime.InteropServices Namespace

Provides a list of members that support COM interop and platform invoke services.

Thursday, December 27, 2012

Common Language Specification (CLS)

Common Language Specification is a set of base rules to which any language targeting the CLI should conform in order to interoperate with other CLS-compliant languages.

Common Language Specification (CLS) is a set of basic language features needed by many applications.

To fully interact with other objects regardless of the language they were implemented in, objects must expose to callers only those features that are common to all the languages they must interoperate with.

UInt32 Structure

UInt32 Structure represents a 32-bit unsigned integer.

The UInt32 type is not CLS-compliant.
The CLS-compliant alternative type is Int64.

Saturday, August 18, 2012

Convert .NET DateTime.Ticks to SQL datetime

SQL Server > Convert .NET DateTime.Ticks to SQL datetime

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.





C#

var t = DateTime.Now.ToUniversalTime().Ticks; // ToUniversalTime : Converts the value of the current DateTime object to Coordinated Universal Time (UTC).

SQL Server Function to convert


create function TicksToDateTime
(@Ticks bigint)
RETURNS datetime AS
BEGIN
        -- First, we will convert the ticks into a datetime value with UTC time
         DECLARE @BaseDate datetime;
         SET @BaseDate = '01/01/1900';
         DECLARE @NetFxTicksFromBaseDate bigint;
         SET @NetFxTicksFromBaseDate = @Ticks - 599266080000000000;
-- The numeric constant is the number of .Net Ticks between the System.DateTime.MinValue (01/01/0001) and the SQL Server datetime base date (01/01/1900)
         DECLARE @DaysFromBaseDate int;
         SET @DaysFromBaseDate = @NetFxTicksFromBaseDate / 864000000000; -- The numeric constant is the number of .Net Ticks in a single day.
         DECLARE @TimeOfDayInTicks bigint;
         SET @TimeOfDayInTicks = @NetFxTicksFromBaseDate - @DaysFromBaseDate * 864000000000;
        DECLARE @TimeOfDayInMilliseconds int;
        SET @TimeOfDayInMilliseconds = @TimeOfDayInTicks / 10000; -- A Tick equals to 100 nanoseconds which is 0.0001 milliseconds
        DECLARE @UtcDate datetime;
        SET @UtcDate = DATEADD(ms, @TimeOfDayInMilliseconds, DATEADD(d,@DaysFromBaseDate, @BaseDate)); -- The @UtcDate is already useful. If you need the time in UTC, just return this value.
      -- Now, some magic to get the local time
         RETURN @UtcDate + GETDATE() - GETUTCDATE();
END
GO
select dbo.TicksToDateTime(634808765429446556)

result:

2012-08-18 11:49:02.947