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

Code before:

            // Track if this is a localhost subdomain for fallback handling.
            bool isLocalhostSubdomain = IsLocalhostSubdomain(hostName);

            bool fallbackToLocalhost = false;
            object result;
            try
            {
                SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses, addressFamily, out string? newHostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);

                if (errorCode != SocketError.Success)
                {
                    // RFC 6761 Section 6.3: If localhost subdomain fails, fall back to resolving plain "localhost".
                    if (isLocalhostSubdomain)
                    {
                        if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(hostName, "RFC 6761: Localhost subdomain resolution failed, falling back to 'localhost'");
                        NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: null, exception: CreateException(errorCode, nativeErrorCode));
                        fallbackToLocalhost = true;
                    }
                    else
                    {
                        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))
            {

Review comment (suggestion):
```suggestion
                else if (addresses.Length == 0 && IsLocalhostSubdomain(hostName))
```
Presumably if the common case is that DNS is giving us some addresses, we can avoid the (admittedly small) overhead of IsLocalhostSubdomain

Code after:
                NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: null, exception: invalidDomainException);
                throw invalidDomainException!;
            }

            bool fallbackToLocalhost = false;
            object? result = null;
            try
            {
                SocketError errorCode = NameResolutionPal.TryGetAddrInfo(hostName, justAddresses, addressFamily, out string? newHostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode);

                if (errorCode != SocketError.Success)
                {
                    // RFC 6761 Section 6.3: If localhost subdomain fails, fall back to resolving plain "localhost".
                    if (IsLocalhostSubdomain(hostName))
                    {
                        if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(hostName, "RFC 6761: Localhost subdomain resolution failed, falling back to 'localhost'");
                        NameResolutionTelemetry.Log.AfterResolution(hostName, activity, answer: null, exception: CreateException(errorCode, nativeErrorCode));
                        fallbackToLocalhost = true;
                    }
                    else
                    {
                        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;
            }