What's the worst problem in C or. NET? [closed]

I recently used the DateTime object and wrote something like this:

DateTime dt = DateTime.Now;
dt.AddDays(1);
return dt; // still today's date! WTF?

The intellisense document for AddDays() says it added a day to the date, but it doesn't - it actually returns the day it added, so you have to write it as:

DateTime dt = DateTime.Now;
dt = dt.AddDays(1);
return dt; // tomorrow's date

This has bitten me many times, so I think it's useful to make the worst C trap.

#1 building

DateTime.ToString ("dd / MM / yyyy"); this doesn't always give you dd / MM / yyyy, but instead takes into account the locale and replaces your date separator based on where you are. So you might get dd-MM-yyyy or something like that.

The correct way is to use DateTime.ToString ("dd'/'MM'/'yyyy");

DateTime.ToString ("r") should be converted to RFC1123 using GMT. GMT is only a fraction of a second from UTC, but the "r" format specifier Will not convert to UTC , even if the problematic DateTime is specified as Local.

This causes the following problems (depending on the distance between your local time and UTC):

DateTime.Parse("Tue, 06 Sep 2011 16:35:12 GMT").ToString("r")
>              "Tue, 06 Sep 2011 17:35:12 GMT"

Ouch!

#2 building

Today, I made a mistake of confiscating it for a long time. The error is in the generic class used in the multithreaded scenario, while the static int field is used to provide lockless synchronization using Interlocked. This error is caused by the fact that each instance of a generic class of type has its own static state. As a result, each thread has its own static fields and does not use locks as expected.

class SomeGeneric<T>
{
    public static int i = 0;
}

class Test
{
    public static void main(string[] args)
    {
        SomeGeneric<int>.i = 5;
        SomeGeneric<string>.i = 10;
        Console.WriteLine(SomeGeneric<int>.i);
        Console.WriteLine(SomeGeneric<string>.i);
        Console.WriteLine(SomeGeneric<int>.i);
    }
}

This print 5 10 5

#3 building

I'm a little late for the party, but I've had two problems recently:

Date time resolution

The Ticks attribute measures one millionth of a second (100 nanosecond block), but the resolution is not 100 nanoseconds, about 15 milliseconds.

This Code:

long now = DateTime.Now.Ticks;
for (int i = 0; i < 10; i++)
{
    System.Threading.Thread.Sleep(1);
    Console.WriteLine(DateTime.Now.Ticks - now);
}

You will be given an output (for example):

0
0
0
0
0
0
0
156254
156254
156254

Similarly, if you look at DateTime.Now.Millisecond, you'll get 15.625ms of round block values: 15, 31, 46, and so on.

This special behavior System specific , but in this date / time API There are other resolution related issues .

Path.Combine

A good way to combine file paths, but it doesn't always run the way you want it to.

If the second parameter starts with the \ \ character, it will not give you the full path:

This Code:

string prefix1 = "C:\\MyFolder\\MySubFolder";
string prefix2 = "C:\\MyFolder\\MySubFolder\\";
string suffix1 = "log\\";
string suffix2 = "\\log\\";

Console.WriteLine(Path.Combine(prefix1, suffix1));
Console.WriteLine(Path.Combine(prefix1, suffix2));
Console.WriteLine(Path.Combine(prefix2, suffix1));
Console.WriteLine(Path.Combine(prefix2, suffix2));

Here's the output:

C:\MyFolder\MySubFolder\log\
\log\
C:\MyFolder\MySubFolder\log\
\log\

#4 building

[Serializable]
class Hello
{
    readonly object accountsLock = new object();
}

//Do stuff to deserialize Hello with BinaryFormatter
//and now... accountsLock == null ;)

Moral of the story: field initializers don't run when deserializing objects

#5 building

MS SQL Server cannot process dates before 1753. Importantly, this is not synchronized with the. NET DateTime.MinDate constant (1 / 1 / 1). So if you try to save an idea, a malformed date (recently in my data import), or just William the conqueror's date of birth, you're in trouble. There is no built-in solution; if you may need to use a date before 1753, you need to write your own solution.

Tags: Attribute SQL

Posted on Fri, 14 Feb 2020 01:57:32 -0800 by gcussi