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

Code before:
                        if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(hostName, $"{hostName} DNS lookup failed with {errorCode}");
                        throw CreateException(errorCode, nativeErrorCode);
                    }
                }
                else if (isLocalhostSubdomain && addresses.Length == 0)
                {
                    // RFC 6761 Section 6.3: If localhost subdomain returns empty addresses, fall back to plain "localhost".
                    if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(hostName, "RFC 6761: Localhost subdomain returned empty, falling back to 'localhost'");
                    NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: justAddresses ? addresses : (object)new IPHostEntry { AddressList = addresses, HostName = newHostName!, Aliases = aliases }, exception: null);
                    fallbackToLocalhost = true;
                }

                if (!fallbackToLocalhost)
                {
                    result = justAddresses ? (object)
                        addresses :
                        new IPHostEntry
                        {
                            AddressList = addresses,
                            HostName = newHostName!,
                            Aliases = aliases
                        };
                }
                else
                {
                    result = null!; // Satisfies definite assignment; overwritten by fallback below.
                }
            }
            catch (Exception ex) when (LogFailure(hostName, activity, ex))
            {
                Debug.Fail("LogFailure should return false");
                throw;
            }

            if (fallbackToLocalhost)
            {
                return GetHostEntryOrAddressesCore("localhost", justAddresses, addressFamily);
            }

            NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: result);

            return result;
        }

        private static IPHostEntry GetHostEntryCore(IPAddress address, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default) =>
            (IPHostEntry)GetHostEntryOrAddressesCore(address, justAddresses: false, addressFamily, activityOrDefault);

        private static IPAddress[] GetHostAddressesCore(IPAddress address, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default) =>
            (IPAddress[])GetHostEntryOrAddressesCore(address, justAddresses: true, addressFamily, activityOrDefault);

        // Does internal IPAddress reverse and then forward lookups (for Legacy and current public methods).

Review comment (bug):
Nit: You can assign `object? result = null` to begin with instead of the `else` here.
And a `Debug.Assert(result is not null)` before the call to `AfterResolution` below.

Code after:
                        if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(hostName, $"{hostName} DNS lookup failed with {errorCode}");
                        throw CreateException(errorCode, nativeErrorCode);
                    }
                }
                else if (addresses.Length == 0 && IsLocalhostSubdomain(hostName))
                {
                    // RFC 6761 Section 6.3: If localhost subdomain returns empty addresses, fall back to plain "localhost".
                    if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(hostName, "RFC 6761: Localhost subdomain returned empty, falling back to 'localhost'");
                    NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: justAddresses ? addresses : (object)new IPHostEntry { AddressList = addresses, HostName = newHostName!, Aliases = aliases }, exception: null);
                    fallbackToLocalhost = true;
                }

                if (!fallbackToLocalhost)
                {
                    result = justAddresses ? (object)
                        addresses :
                        new IPHostEntry
                        {
                            AddressList = addresses,
                            HostName = newHostName!,
                            Aliases = aliases
                        };
                }
            }
            catch (Exception ex) when (LogFailure(hostName, activity, ex))
            {
                Debug.Fail("LogFailure should return false");
                throw;
            }

            if (fallbackToLocalhost)
            {
                return GetHostEntryOrAddressesCore(Localhost, justAddresses, addressFamily);
            }

            Debug.Assert(result is not null);
            NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: result);

            return result;
        }

        private static IPHostEntry GetHostEntryCore(IPAddress address, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default) =>
            (IPHostEntry)GetHostEntryOrAddressesCore(address, justAddresses: false, addressFamily, activityOrDefault);

        private static IPAddress[] GetHostAddressesCore(IPAddress address, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default) =>
            (IPAddress[])GetHostEntryOrAddressesCore(address, justAddresses: true, addressFamily, activityOrDefault);

        // Does internal IPAddress reverse and then forward lookups (for Legacy and current public methods).
        private static object GetHostEntryOrAddressesCore(IPAddress address, bool justAddresses, AddressFamily addressFamily, NameResolutionActivity? activityOrDefault = default)
        {
            if (OperatingSystem.IsWasi()) throw new PlatformNotSupportedException(); // TODO remove with https://github.com/dotnet/runtime/pull/107185