Click here to Skip to main content
15,433,989 members
Home / Discussions / C#
   

C#

 
QuestionHow to convert Persian date representation? Pin
Alex Dunlop15-Sep-21 20:31
MemberAlex Dunlop15-Sep-21 20:31 
AnswerRe: How to convert Persian date representation? Pin
BillWoodruff15-Sep-21 21:31
mveBillWoodruff15-Sep-21 21:31 
GeneralRe: How to convert Persian date representation? Pin
Alex Dunlop15-Sep-21 22:05
MemberAlex Dunlop15-Sep-21 22:05 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff15-Sep-21 22:22
mveBillWoodruff15-Sep-21 22:22 
GeneralRe: How to convert Persian date representation? Pin
Alex Dunlop16-Sep-21 1:11
MemberAlex Dunlop16-Sep-21 1:11 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming16-Sep-21 1:26
mveRichard Deeming16-Sep-21 1:26 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff16-Sep-21 4:23
mveBillWoodruff16-Sep-21 4:23 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming16-Sep-21 4:53
mveRichard Deeming16-Sep-21 4:53 
I was simply referring to the fact that a DateTime is a value type, whereas String is a reference type. Allocation of value types tends to be "cheaper" than reference types.

Jon's workaround is good, but could be improved in .NET Core 2.1, or even in .NET Framework 4.7.2+ with a reference to the System.Memory package and a couple of helpers defined. Smile | :)
C#
#if NETSTANDARD2_0
namespace System.Buffers
{
    public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg);
}
#endif

static class StringHelpers
{
    public static string Create<TArg>(int length, TArg state, SpanAction<char, TArg> action)
    {
        if (length < 0) throw new ArgumentOutOfRangeException(nameof(length));
        if (action is null) throw new ArgumentNullException(nameof(action));
        if (length == 0) return string.Empty;

#if NETSTANDARD2_0
        unsafe
        {
            var str = new string('\0', length);
            fixed (char* chars = str)
            {
                var span = new Span<char>(chars, length);
                action(span, state);
            }

            return str;
        }
#else
        return string.Create(length, state, action);
#endif
    }
}
C#
public static string FormatDateTime(DateTime dt) => StringHelpers.Create(21, dt, static (buffer, date) =>
{
    int value = date.Day;
    buffer[0] = (char)('0' + (value / 10));
    buffer[1] = (char)('0' + (value % 10));
    
    buffer[2] = '.';
    
    value = date.Month;
    buffer[3] = (char)('0' + (value / 10));
    buffer[4] = (char)('0' + (value % 10));
    
    buffer[5] = '.';
    
    value = date.Year;
    buffer[6] = (char)('0' + ((value / 10) % 10));
    buffer[7] = (char)('0' + (value % 10));
    
    buffer[8] = ' ';
    
    value = date.Hour;
    buffer[9] = (char)('0' + (value / 10));
    buffer[10] = (char)('0' + (value % 10));
    
    buffer[11] = ':';
    
    value = date.Minute;
    buffer[12] = (char)('0' + (value / 10));
    buffer[13] = (char)('0' + (value % 10));
    
    buffer[14] = ':';
    
    value = date.Second;
    buffer[15] = (char)('0' + (value / 10));
    buffer[16] = (char)('0' + (value % 10));
    
    buffer[17] = ':';
    
    value = date.Millisecond;
    buffer[18] = (char)('0' + ((value / 100) % 10));
    buffer[19] = (char)('0' + ((value / 10) % 10));
    buffer[20] = (char)('0' + (value % 10));
});
Rather than allocating a new 21-character char array on every call, and then having the string constructor copy that array to its internal storage, this new version writes the values directly to the string's internal storage. It does have to allocate a delegate for the SpanAction, but since it's a static lambda, that only happens once per AppDomain. Smile | :)

If you're using .NET Core 2.1 or later, or .NET 5, you can drop the helpers and just use string.Create directly.

Creating Strings with No Allocation Overhead Using String.Create - Steve Gordon - Code with Steve[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


modified 22-Sep-21 5:23am.

GeneralRe: How to convert Persian date representation? Pin
BillWoodruff16-Sep-21 5:01
mveBillWoodruff16-Sep-21 5:01 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff21-Sep-21 8:52
mveBillWoodruff21-Sep-21 8:52 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming21-Sep-21 21:45
mveRichard Deeming21-Sep-21 21:45 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff21-Sep-21 23:21
mveBillWoodruff21-Sep-21 23:21 
GeneralRe: How to convert Persian date representation? Pin
Richard Deeming21-Sep-21 23:24
mveRichard Deeming21-Sep-21 23:24 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff22-Sep-21 0:53
mveBillWoodruff22-Sep-21 0:53 
GeneralRe: How to convert Persian date representation? Pin
BillWoodruff16-Sep-21 1:28
mveBillWoodruff16-Sep-21 1:28 
QuestionC# Confusion about LINQ Grouping Pin
Mou_kol14-Sep-21 23:11
MemberMou_kol14-Sep-21 23:11 
AnswerRe: C# Confusion about LINQ Grouping Pin
Richard Deeming14-Sep-21 23:42
mveRichard Deeming14-Sep-21 23:42 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol14-Sep-21 23:51
MemberMou_kol14-Sep-21 23:51 
GeneralRe: C# Confusion about LINQ Grouping Pin
Richard Deeming15-Sep-21 0:41
mveRichard Deeming15-Sep-21 0:41 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 3:51
MemberMou_kol15-Sep-21 3:51 
GeneralRe: C# Confusion about LINQ Grouping Pin
Richard Deeming15-Sep-21 4:15
mveRichard Deeming15-Sep-21 4:15 
AnswerRe: C# Confusion about LINQ Grouping Pin
BillWoodruff15-Sep-21 19:35
mveBillWoodruff15-Sep-21 19:35 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol15-Sep-21 20:22
MemberMou_kol15-Sep-21 20:22 
GeneralRe: C# Confusion about LINQ Grouping Pin
Dave Kreskowiak16-Sep-21 2:25
mveDave Kreskowiak16-Sep-21 2:25 
GeneralRe: C# Confusion about LINQ Grouping Pin
Mou_kol16-Sep-21 6:30
MemberMou_kol16-Sep-21 6:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.