Repository: dotnet/runtime
File: src/libraries/System.Net.Sockets/src/System/Net/Sockets/DynamicWinsockMethods.cs (C#)

Code before:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;

namespace System.Net.Sockets
{
    internal sealed class DynamicWinsockMethods
    {
        // In practice there will rarely be more than four of these, so it's not worth a complicated
        // hash table structure. Store them in an array and search through it. The array is replaced
        // copy-on-write under s_methodTableLock, so reads always see a consistent, immutable snapshot.
        private static volatile DynamicWinsockMethods[] s_methodTable = [];
        private static readonly Lock s_methodTableLock = new();

        private bool Matches(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) =>
            _addressFamily == addressFamily && _socketType == socketType && _protocolType == protocolType;

        public static DynamicWinsockMethods GetMethods(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            foreach (DynamicWinsockMethods methods in s_methodTable)
            {
                if (methods.Matches(addressFamily, socketType, protocolType))
                {
                    return methods;
                }
            }

            return GetMethodsSlow(addressFamily, socketType, protocolType);
        }

        private static DynamicWinsockMethods GetMethodsSlow(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            lock (s_methodTableLock)
            {
                DynamicWinsockMethods[] methodTable = s_methodTable;
                foreach (DynamicWinsockMethods methods in methodTable)
                {

Review comment (performance):
```suggestion
        private static DynamicWinsockMethods[] s_methodTable = [];
```
Seems unnecessary given how `GetMethodsSlow` works

Code after:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading;

namespace System.Net.Sockets
{
    internal sealed class DynamicWinsockMethods
    {
        // In practice there will rarely be more than four of these, so it's not worth a complicated
        // hash table structure. Store them in an array and search through it. The array is replaced
        // copy-on-write under s_methodTableLock, so reads always see a consistent, immutable snapshot.
        private static DynamicWinsockMethods[] s_methodTable = [];
        private static readonly Lock s_methodTableLock = new();

        private bool Matches(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) =>
            _addressFamily == addressFamily && _socketType == socketType && _protocolType == protocolType;

        public static DynamicWinsockMethods GetMethods(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            foreach (DynamicWinsockMethods methods in s_methodTable)
            {
                if (methods.Matches(addressFamily, socketType, protocolType))
                {
                    return methods;
                }
            }

            return GetMethodsSlow(addressFamily, socketType, protocolType);
        }

        private static DynamicWinsockMethods GetMethodsSlow(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
        {
            lock (s_methodTableLock)
            {
                DynamicWinsockMethods[] methodTable = s_methodTable;
                foreach (DynamicWinsockMethods methods in methodTable)
                {