Skip to content

Instantly share code, notes, and snippets.

@noqisofon
Created December 1, 2010 02:25
Show Gist options
  • Save noqisofon/722824 to your computer and use it in GitHub Desktop.
Save noqisofon/722824 to your computer and use it in GitHub Desktop.
Linq で IPv4 形式の IP アドレスオブジェクトを抽出する感じ。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Text;
namespace demo.linq.ipaddr {
/// <summary>
///
/// </summary>
class LinqExtractsIPAddressSample {
/// <summary>
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args) {
IPHostEntry host = Dns.GetHostEntry( Dns.GetHostName() );
// Linq で IPv4 形式の IP アドレスオブジェクトを抽出します。
IEnumerable<IPAddress> ret = from address in host.AddressList
where
address.AddressFamily == AddressFamily.InterNetwork
select address;
// 列挙子を取得し、
var e = ret.GetEnumerator();
// カーソルを次(0)に合わせ、
if ( e.MoveNext() )
// 現在、カーソルが指している要素を表示します。
Console.WriteLine( e.Current );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment