B
B
Boris the Animal2016-12-30 17:35:27
.NET
Boris the Animal, 2016-12-30 17:35:27

.NET sources. Isn't this a sign of bad code that I will provide in the topic?

https://referencesource.microsoft.com/#System.Core...
Here, for example, there are many calls to an array by index in order to access the fields of the same object.

m_slots[index].hashCode = hashCode;
m_slots[index].value = value;
m_slots[index].next = m_buckets[bucket] - 1;

Added later:
Perhaps if T is Value Type, then we can copy the value from the array and not assign anything to the value in the array?
Added later:
Well, in this case, first we get a copy from the array, we work on it, and then we assign the modified copy back at the desired index. For that, we get rid of a bunch of hits through the indexer.
Added later:
Well, yes, as I thought
using System;

namespace TestValueType
{
    class Program
    {
        #region Entry point

        private static Program _program;

        private static void Main(string[] args)
        {
            _program = new Program();
            _program.Run(args);
        }

        #endregion

        private void Run(string[] args)
        {
            _slots = new Slot[]
            {
                new Slot { hashCode = 1, value = 10 },
                new Slot { hashCode = 2, value = 20 },
                new Slot { hashCode = 3, value = 30 },
                new Slot { hashCode = 4, value = 40 },
            };

            //_slots[0].value = 70;

            Slot slot = _slots[0];
            slot.value = 70;
            
            Display(_slots[0]);

            Console.ReadKey();
        }

        private void Display(Slot slot)
        {
            Console.WriteLine(slot.ToString());
        }

        private Slot[] _slots;

        internal struct Slot
        {
            internal int hashCode;      // Lower 31 bits of hash code, -1 if unused
            internal int value;

            /// <summary>
            /// Возвращает полное имя типа этого экземпляра.
            /// </summary>
            /// <returns>
            /// Объект типа <see cref="T:System.String"/>, содержащий полное имя типа.
            /// </returns>
            public override string ToString()
            {
                return $"hashCode = {hashCode}, value = {value}";
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GreyPhantom, 2016-12-30
@GreyPhantom

first we get a copy from the array, we work on it, and then we assign the modified copy back at the desired index. For that, we get rid of a bunch of hits through the indexer.
"There is never too much memory," remember? Why make a copy of an array element when you can read directly from the array? What's wrong with indexing?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question