this is my class that i want to return my machine network interfaces collection.
i choose to return IEnumerable and i dont know how to do it (i am a new developer).
or maybee there is batter way to build my class ?
public class NetworkAdapter
{
string _name;
string _id;
string _description;
string _ipAddress;
string _gatewayIpAddress;
string _speed;
string _networkInterfaceType;
string _macAddress;
public IEnumerable<NetworkAdapter> getAdapterInfo()
{
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
{
//fetch network configuration properties
var properties = adapter.GetIPProperties();
foreach (var uniCast in properties.UnicastAddresses)
{
//ignore loop-back addresses & IPv6 internet protocol family
if (!IPAddress.IsLoopback(uniCast.Address)
&& uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6)
{
_name = adapter.Name;
_id = adapter.Id;
_description = adapter.Description;
_ipAddress = uniCast.Address.ToString();
_networkInterfaceType = adapter.NetworkInterfaceType.ToString();
_speed = adapter.Speed.ToString("#,##0");
_macAddress = adapter.GetPhysicalAddress().ToString();
var gatewayAddresses = adapter.GetIPProperties().GatewayAddresses;
foreach (var gatewayAddress in gatewayAddresses)
{
_gatewayIpAddress = gatewayAddress.Address.ToString();
}
}
}
}
yield return new NetworkAdapter att;
}
}
You could use linq:
It’s not clear what you’re trying to do with the gateway addresses. At the moment it looks like you want the last one, although you might want to join them into a single string instead. In that case you can use: