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

Code before:
                    IPAddress a => GetHostAddressesCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
            else
            {
                return RunAsync(s => s switch
                {
                    string h => GetHostEntryCore(h, AddressFamily.Unspecified),
                    KeyValuePair<string, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    IPAddress a => GetHostEntryCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
        }

        private static bool TryGetAddrInfoWithTelemetryAsync<T>(string hostName, bool justAddresses, AddressFamily addressFamily, CancellationToken cancellationToken, out Task t)
             where T : class
        {
            ValueStopwatch stopwatch = ValueStopwatch.StartNew();
            if (NameResolutionPal.TryGetAddrInfoAsync(hostName, justAddresses, addressFamily, cancellationToken, out Task task))
            {
                //return new Tuple<bool, Task>(true, (Task)InternalGetAddrInfoWithTelemetryAsync(task, hostName));
                t  = InternalGetAddrInfoWithTelemetryAsync(task, hostName, stopwatch);
                return true;
            }

            // If resolution even did not start don't bother with telemetry.
            // We will retry on thread-pool.

            t = Task.CompletedTask;
            return false;

            static async Task<T> InternalGetAddrInfoWithTelemetryAsync(Task task, string hostName, ValueStopwatch stopwatch)
            {
                _  = NameResolutionTelemetry.Log.BeforeResolution(hostName);
                T? result = null;
                try
                {
                    result = await ((Task<T>)task).ConfigureAwait(false);
                    return result;
                }
                finally
                {
                    NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: result is not null);
                }

                return result;
            }

Review comment (suggestion):
```suggestion
                t = InternalGetAddrInfoWithTelemetryAsync(task, hostName, stopwatch);
```

Code after:
                    IPAddress a => GetHostAddressesCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostAddressesCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
            else
            {
                return RunAsync(s => s switch
                {
                    string h => GetHostEntryCore(h, AddressFamily.Unspecified),
                    KeyValuePair<string, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    IPAddress a => GetHostEntryCore(a, AddressFamily.Unspecified),
                    KeyValuePair<IPAddress, AddressFamily> t => GetHostEntryCore(t.Key, t.Value),
                    _ => null
                }, asyncState);
            }
        }

        private static Task<T>? GetAddrInfoWithTelemetryAsync<T>(string hostName, bool justAddresses, AddressFamily addressFamily, CancellationToken cancellationToken)
             where T : class
        {
            ValueStopwatch stopwatch = ValueStopwatch.StartNew();
            Task? task = NameResolutionPal.GetAddrInfoAsync(hostName, justAddresses, addressFamily, cancellationToken);

            if (task != null)
            {
                return CompleteAsync(task, hostName, stopwatch);
            }

            // If resolution even did not start don't bother with telemetry.
            // We will retry on thread-pool.
            return null;

            static async Task<T> CompleteAsync(Task task, string hostName, ValueStopwatch stopwatch)
            {
                _  = NameResolutionTelemetry.Log.BeforeResolution(hostName);
                T? result = null;
                try
                {
                    result = await ((Task<T>)task).ConfigureAwait(false);
                    return result;
                }
                finally
                {
                    NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: result is not null);
                }
            }
        }

        private static Task<TResult> RunAsync<TResult>(Func<object, TResult> func, object arg) =>
            Task.Factory.StartNew(func!, arg, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);