by Viper
6. March 2009 06:05
Some time back I wrote an article on similar topic How to get IP address of a machine using C#. That article was written in days of .Net1.1. Since then .Net framework has come a long way. Some APIs were deprecated and a lot of new APIs were added. This post is an update on that old article. While I was working on a silverlight socket programming project, I had to deal with lot of Networking APIs. During that process I ended up writing a new method that returns me list of local IP addresses and presenting that to user to decide on what IP address they want to run socket server. Hope this post provides a quick coding tip on how you can get local IP address of your machine.
Private Function GetIPs() As StringCollection
Dim localIP = New StringCollection()
Dim localHostName = Dns.GetHostName()
Dim hostEntry = Dns.GetHostEntry(localHostName)
' Grab all ip addesses.
For Each ipAddr As IPAddress In hostEntry.AddressList
localIP.Add(ipAddr.ToString())
Next
GetIPs = localIP
End Function
Pardon my VB.Net code. I am not very intimate with this language. You can find C# code at old article location
by Viper
24. February 2009 06:38
Auto implemented properties are wonderful thins and save lot of coding and declaration in most of the situations. So a simple auto implemented property will look like as below.
public string Title
{get; set;}
No need to define a private variable to store the value. In some cases you want your property to be immutable meaning you don't want clients to be able to change the value and only allow private or internal access to the setter of this property. The specification of auto implemented properties says that you have to provide both get and set accessors. This does not mean that you have to provide public accessors for both. You can always provide private or inernal accessor for get.
public string Title
{get; internal set;}
9ef4827c-45dd-4d5d-abf3-779cbb4baba4|0|.0
Views: 704
Tags: c#, .net
.Net | CSharp