Can Ürek

Keep walking...

Convert DateTime to Unix time in C#

3
4
5
6
7
8
9
10
11
/// <summary>
/// Convert a date time object to Unix time representation.
/// </summary>
/// <param name="datetime">The datetime object to convert to Unix time stamp.</param>
/// <returns>Returns a numerical representation (Unix time) of the DateTime object.</returns>
public static long ConvertToUnixTime(DateTime datetime)
{
    DateTime sTime = new DateTime(1970, 1, 1,0,0,0,DateTimeKind.Utc);
 
    return (long)(datetime - sTime).TotalSeconds;
}

2
3
4
5
6
7
8
9
10
/// <summary>
/// Convert Unix time value to a DateTime object.
/// </summary>
/// <param name="unixtime">The Unix time stamp you want to convert to DateTime.</param>
/// <returns>Returns a DateTime object that represents value of the Unix time.</returns>
public static DateTime UnixTimeToDateTime(long unixtime)
{
    DateTime sTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return sTime.AddSeconds(unixtime);
}

c# lock and deadlocks (basics)

            object A = new object();
            object B = new object();
 
            Task.Factory.StartNew(() =>
            {
                lock (A)
                {
                    Thread.Sleep(1000);
                    lock (B) Console.WriteLine("locked A, then B");
                }
            });
 
            Task.Factory.StartNew(() =>
            {
                lock (B)
                {
                    lock (A) Console.WriteLine("locked B, then A");
                }
            });

https://csharphardcoreprogramming.wordpress.com/2013/12/10/lock-and-deadlocks-basics/

When you comment Thread.Sleep line you can easily understand whats happening.

A deadlock will only occur if you have more than one lock. You need a situation where both threads hold a resource that the other needs (which means there has to be a least two resources, and the two threads have to attempt to acquire them in a different order)


What is the difference between String and string in C#?

Everything in C# language is an Object, there are no primitive types in the classic sense. That means string is not a truly primitive types, it is an object instance of a Class. System.String and string are compiled to System.String in Intermediate Language (IL), so there is no difference in the performance, and it's purely a personal choice. Also; string is an alias for System.String. They compile to the same code, so at execution time there is no difference . This is just one of the aliases in C#. The complete list of alias is:

object:  System.Object
string:  System.String
bool:    System.Boolean
byte:    System.Byte
sbyte:   System.SByte
short:   System.Int16
ushort:  System.UInt16
int:     System.Int32
uint:    System.UInt32
long:    System.Int64
ulong:   System.UInt64
float:   System.Single
double:  System.Double
decimal: System.Decimal
char:    System.Char